-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cpp
More file actions
78 lines (61 loc) · 1.99 KB
/
Utility.cpp
File metadata and controls
78 lines (61 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "Utility.h"
#include "Evaluator.h"
#include "Parser.h"
#include "SValue.h"
#include <fstream>
#include <iostream>
#include <sstream>
// v contains the string path
SValue* evalLoad( Environment& e, SValue* v )
{
REQUIRE( v, v->size() == 1, "load requires one argument" );
Cells& cells = v->cellsRequired();
std::unique_ptr< SValue > file = cells.takeFront();
REQUIRE( v, file->isType< std::string >(), "load requires a string argument" );
std::ifstream reader( file->get< std::string >() );
if ( !reader.good() )
{
return error( v, "Could not read file" );
}
std::string text( ( std::istreambuf_iterator< char >( reader ) ), std::istreambuf_iterator< char >() );
std::unique_ptr< SValue > script = parse( text.cbegin(), text.cend() );
Cells& scriptExpressions = script->cellsRequired();
while ( !scriptExpressions.isEmpty() )
{
std::unique_ptr< SValue > v = scriptExpressions.takeFront();
std::ostringstream ss;
show( ss, *v );
std::string exprString = ss.str();
SValue* result = evaluate( e, v.get() );
if ( result->isError() )
{
std::cout << exprString << '\n';
std::cout << *result << '\n';
}
}
return empty( v );
}
SValue* evalPrint( Environment& e, SValue* v )
{
v->foreachCell( []( const SValue& v ) { show( std::cout, v ) << ' '; } );
std::cout << '\n';
return empty( v );
}
SValue* evalError( Environment& e, SValue* v )
{
REQUIRE( v, v->size() == 1, "error requires one argument" );
Cells& cells = v->cellsRequired();
std::unique_ptr< SValue > errorMessage = cells.takeFront();
REQUIRE( v, errorMessage->isType< std::string >(), "error requires a string argument" );
return error( v, errorMessage->get< std::string >() );
}
SValue* evalShow( Environment& e, SValue* v )
{
REQUIRE( v, v->size() == 1, "show requires one argument" );
Cells& cells = v->cellsRequired();
std::unique_ptr< SValue > arg = cells.takeFront();
std::ostringstream ss;
show( ss, *arg );
v->value = ss.str();
return v;
}