blob: cf078a726b665aabe656b4f47800691e4870c6cf (
plain) (
blame)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include "ASTInterpreter.h"
#include "Expression.h"
#include <iostream>
#include <math.h>
#undef NOT_IMPLEMENTED
#define NOT_IMPLEMENTED (void)e; assert(0 && "not-implemented")
struct LValueExtractor : public ExpressionVisitor
{
public:
LValueExtractor(ASTInterpreter* interpreter)
: m_Interpreter(interpreter)
{}
~LValueExtractor() {}
ASTInterpreter::value_type* Run(Expression* program)
{
m_LValue = nullptr;
program->Accept(*this);
return m_LValue;
}
virtual void Visit(LiteralNull* e) override { NOT_IMPLEMENTED; }
virtual void Visit(LiteralUndefined* e) override { NOT_IMPLEMENTED; }
virtual void Visit(LiteralString* e) override { NOT_IMPLEMENTED; }
virtual void Visit(LiteralNumber* e) override { NOT_IMPLEMENTED; }
virtual void Visit(LiteralBoolean* e) override { NOT_IMPLEMENTED; }
virtual void Visit(LiteralObject* e) override { NOT_IMPLEMENTED; }
virtual void Visit(BinaryExpression* e) override { NOT_IMPLEMENTED; }
virtual void Visit(UnaryExpression* e) override { NOT_IMPLEMENTED; }
virtual void Visit(IdentifierExpression* e) override { m_LValue = &m_Interpreter->ModifyVariable(e->GetName()); }
virtual void Visit(ListExpression* e) override { NOT_IMPLEMENTED; }
virtual void Visit(VariableDefinitionExpression* e) override { NOT_IMPLEMENTED; }
virtual void Visit(BlockStatement* e) override { NOT_IMPLEMENTED; }
virtual void Visit(LabeledStatement* e) override { NOT_IMPLEMENTED; }
virtual void Visit(IfStatement* e) override { NOT_IMPLEMENTED; }
virtual void Visit(SwitchStatement* e) override { NOT_IMPLEMENTED; }
virtual void Visit(CaseStatement *e) override { NOT_IMPLEMENTED; }
virtual void Visit(WhileStatement* e) override { NOT_IMPLEMENTED; }
virtual void Visit(ForStatement* e) override { NOT_IMPLEMENTED; }
virtual void Visit(FunctionDeclaration* e) override { NOT_IMPLEMENTED; }
virtual void Visit(TopStatements* e) override { NOT_IMPLEMENTED; }
virtual void Visit(EmptyExpression* e) override { NOT_IMPLEMENTED; }
virtual void Visit(CallExpression* e) override { NOT_IMPLEMENTED; }
ASTInterpreter* m_Interpreter;
ASTInterpreter::value_type* m_LValue;
};
struct VariableScope
{
VariableScope(ASTInterpreter* interpreter)
: m_Interpreter(interpreter)
{
m_Interpreter->EnterScope();
}
~VariableScope() {
m_Interpreter->LeaveScope();
}
VariableScope(VariableScope&) = delete;
VariableScope(VariableScope&&) = delete;
VariableScope& operator=(VariableScope&) = delete;
VariableScope& operator=(VariableScope&&) = delete;
ASTInterpreter* m_Interpreter;
};
struct StackScope
{
StackScope(ASTInterpreter* interpreter)
: m_Interpreter(interpreter)
, m_StackSize(m_Interpreter->m_Evaluation.size())
{
}
~StackScope() {
m_Interpreter->m_Evaluation.resize(m_StackSize);
}
StackScope(StackScope&) = delete;
StackScope(StackScope&&) = delete;
StackScope& operator=(StackScope&) = delete;
StackScope& operator=(StackScope&&) = delete;
ASTInterpreter* m_Interpreter;
size_t m_StackSize;
};
ASTInterpreter::ASTInterpreter()
{
// Allow globals
EnterScope();
}
ASTInterpreter::~ASTInterpreter()
{}
ASTInterpreter::ValueStack ASTInterpreter::Run(Expression* program)
{
program->Accept(*this);
ValueStack result;
m_Evaluation.swap(result);
return result;
}
void ASTInterpreter::RunExpression(const ExpressionPtr& e)
{
if (e) {
e->Accept(*this);
}
}
bool ASTInterpreter::EvalToBool(const ExpressionPtr& e)
{
e->Accept(*this);
auto result = m_Evaluation.back();
m_Evaluation.pop_back();
return result != 0.0;
}
void ASTInterpreter::EnterScope()
{
m_Scopes.push(Scope{});
}
void ASTInterpreter::LeaveScope()
{
const auto& scope = m_Scopes.top();
for (auto& var : scope) {
auto values = m_Variables.find(var);
values->second.pop_back();
if (values->second.empty()) {
m_Variables.erase(values);
}
}
m_Scopes.pop();
}
ASTInterpreter::value_type& ASTInterpreter::ModifyVariable(const IPLString& name)
{
auto values = m_Variables.find(name);
if (values != m_Variables.end()) {
return values->second.back();
}
else {
assert(0 && "not-defined");
}
static value_type undefined;
return undefined;
}
bool ASTInterpreter::HasVariable(const IPLString& name)
{
return m_Variables.find(name) != m_Variables.end();
}
void ASTInterpreter::Visit(LiteralNull* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralUndefined* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralString* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralObject* e)
{
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(LiteralNumber* e) {
m_Evaluation.push_back(e->GetValue());
}
void ASTInterpreter::Visit(LiteralBoolean* e) {
m_Evaluation.push_back(e->GetValue());
}
void ASTInterpreter::Visit(BinaryExpression* e) {
e->GetLeft()->Accept(*this);
e->GetRight()->Accept(*this);
auto right = m_Evaluation.back();
m_Evaluation.pop_back();
auto left = m_Evaluation.back();
m_Evaluation.pop_back();
switch (e->GetOperator()) {
case TokenType::Plus:
m_Evaluation.push_back(left + right);
break;
case TokenType::Minus:
m_Evaluation.push_back(left - right);
break;
case TokenType::Star:
m_Evaluation.push_back(left * right);
break;
case TokenType::Division:
m_Evaluation.push_back(left / right);
break;
case TokenType::Less:
m_Evaluation.push_back(left < right);
break;
case TokenType::Comma:
m_Evaluation.push_back(right);
break;
case TokenType::EqualEqual:
m_Evaluation.push_back(fabs(left - right) < 0.0001);
break;
default:
NOT_IMPLEMENTED;
}
}
void ASTInterpreter::Visit(UnaryExpression* e) {
LValueExtractor extractor(this);
auto lvalue = extractor.Run(e->GetExpr().get());
if (e->GetSuffix())
{
m_Evaluation.push_back(*lvalue);
}
switch (e->GetOperator())
{
case TokenType::PlusPlus:
++(*lvalue);
break;
case TokenType::MinusMinus:
--(*lvalue);
break;
default:
NOT_IMPLEMENTED;
}
if (!e->GetSuffix())
{
m_Evaluation.push_back(*lvalue);
}
}
void ASTInterpreter::Visit(IdentifierExpression* e) {
m_Evaluation.push_back(ModifyVariable(e->GetName()));
}
void ASTInterpreter::Visit(ListExpression* e) {
for (const auto& stmt: e->GetValues()) {
stmt->Accept(*this);
}
}
void ASTInterpreter::Visit(VariableDefinitionExpression* e) {
e->GetValue()->Accept(*this);
auto& name = e->GetName();
m_Variables[name].push_back(m_Evaluation.back());
m_Scopes.top().push_back(name);
m_Evaluation.pop_back();
}
void ASTInterpreter::Visit(BlockStatement* e) {
for (const auto& stmt: e->GetValues()) {
stmt->Accept(*this);
}
}
void ASTInterpreter::Visit(LabeledStatement* e) {
RunExpression(e->GetStatement());
}
void ASTInterpreter::Visit(IfStatement* e) {
VariableScope scope(this);
if (EvalToBool(e->GetCondition())) {
VariableScope ifScope(this);
RunExpression(e->GetIfStatement());
} else {
VariableScope elseScope(this);
RunExpression(e->GetElseStatement());
}
}
void ASTInterpreter::Visit(SwitchStatement* e) { NOT_IMPLEMENTED;}
void ASTInterpreter::Visit(CaseStatement *e) { NOT_IMPLEMENTED;}
void ASTInterpreter::Visit(WhileStatement* e) {
VariableScope scope(this);
if (e->GetDoWhile()) {
VariableScope bodyScope(this);
RunExpression(e->GetBody());
}
while (EvalToBool(e->GetCondition())) {
VariableScope bodyScope(this);
RunExpression(e->GetBody());
}
}
void ASTInterpreter::Visit(ForStatement* e) {
VariableScope scope(this);
{
StackScope stackScope(this);
RunExpression(e->GetInitialization());
}
while (EvalToBool(e->GetCondition()))
{
{
VariableScope bodyScope(this);
RunExpression(e->GetBody());
}
{
StackScope stackScope(this);
RunExpression(e->GetIteration());
}
}
}
void ASTInterpreter::Visit(FunctionDeclaration* e) { NOT_IMPLEMENTED;}
void ASTInterpreter::Visit(TopStatements* e) {
for (const auto& stmt: e->GetValues()) {
stmt->Accept(*this);
}
}
void ASTInterpreter::Visit(EmptyExpression* e) {
NOT_IMPLEMENTED;
}
void ASTInterpreter::Visit(CallExpression* e)
{
NOT_IMPLEMENTED;
}
|