-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests-interpreter.cpp
225 lines (203 loc) · 6.12 KB
/
tests-interpreter.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <catch2/catch.hpp>
#include <iostream>
#include "Interpreter.h"
TEST_CASE("Interpreter", "[interpreter]"){
std::istringstream is;
std::ostringstream os;
Lexer lexer({
[&is]{ return is.peek(); },
[&is]{ return is.get(); },
[&is]{ return is.peek() == decltype(is)::traits_type::eof(); }
});
Parser parser{lexer};
Interpreter interpreter;
interpreter.globalEnvironment() = var{{
{"console", {{
{"log", var(
[&os](auto args){
std::copy(std::begin(args), std::end(args), std::ostream_iterator<var>(os));
os << '\n';
return var{};
})
}
}}},
{"exit", var(
[](auto args)->var{
exit(args.size() >= 1 ? static_cast<int>(args[0].to_double()) : 0);
})
}
}};
SECTION("Variable declaration"){
is.str("var x = 3;");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
3
)Interpreter");
}
SECTION("Variable declaration"){
is.str("var x = 3; var y; var z = x;");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
3
)Interpreter");
}
SECTION("JsonObject simple"){
is.str("var x = {}; var y = {'abc':'nooo', 34:42, x};");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
{"x":{},"abc":"nooo","34":42}
)Interpreter");
}
SECTION("MemberAccess Assignment"){
is.str("var x = 'foo'; x = {a:4}; x.b = x.a; x.a = 5; x;");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
{"b":4,"a":5}
)Interpreter");
}
SECTION("Addition"){
is.str("var x = {a: 1 + 1, b: 'coucou' + false, c: 2 + true, d: true + false};");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
{"c":3,"a":2,"d":1,"b":"coucoufalse"}
)Interpreter");
}
SECTION("Basic arithmetic operations"){
is.str("var x = (1 + 19)*5/4%7 - 1;");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
3
)Interpreter");
}
SECTION("MemberAccess Arithmetic Assignments"){
is.str("var x = {a:1}; x.a += 19; x.a *= 5; x.a /= 4; x.a %= 7; x.a -= 1;");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
3
)Interpreter");
}
SECTION("Call operation with arguments"){
is.str("console.log(2+2, '='+4);");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
4=4
undefined
)Interpreter");
}
SECTION("Call operation with undefined arguments"){
is.str("console.log(x);");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
undefined
undefined
)Interpreter");
}
SECTION("Compare operations"){
is.str("console.log(1 < 1, 1 < 2, 2 > 1, 2 > 3, 3 <= 2, 3 <= 3, 3 <= '4', 5 >= 3, 5 >= 6, '5' >= '5');");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
falsetruetruefalsefalsetruetruetruefalsetrue
undefined
)Interpreter");
}
SECTION("Function operation and Call"){
is.str("var g = function(x){ x += 2; return x; }; g(3);");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
5
)Interpreter");
}
SECTION("Call and VarDecl"){
is.str("var g = function(){ return 42; }; var h = g(); console.log(h);");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
42
undefined
)Interpreter");
}
SECTION("Function generator"){
is.str("var g = function(){ return function(){ return 42; }; }; var h = g(); h();");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
42
)Interpreter");
}
SECTION("Function capture"){
is.str("var g = function(x){ return function(y){ return x + y; }; }; g(3)(4);");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
7
)Interpreter");
}
SECTION("If-Else"){
is.str("var a = 35; if(a > 30){ console.log(a, ' greater than 30'); } if(a > 40){ console.log(a, ' greater than 40'); } else { console.log(a, ' less than or eq to 40'); }");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
35 greater than 30
35 less than or eq to 40
undefined
)Interpreter");
}
SECTION("While"){
is.str("var a = 35; while(a > 0){ a -= 5; console.log(a); }");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
30
25
20
15
10
5
0
undefined
)Interpreter");
}
SECTION("DoWhile"){
is.str("var a = 35; do{ a -= 5; console.log(a); }while(a > 0);");
auto tree = parser.parse();
interpreter.feed(tree);
os << '\n' << interpreter.execute() << '\n';
CHECK(os.str() == R"Interpreter(
30
25
20
15
10
5
0
undefined
)Interpreter");
}
}