-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvaluator.cpp
More file actions
352 lines (318 loc) · 10.1 KB
/
Evaluator.cpp
File metadata and controls
352 lines (318 loc) · 10.1 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* ___ __ __ _
* / __> ___ ._ _ _ | \ \ ___ _ _ ._ _ ___ _ _ _| |
* \__ \<_> || ' ' | | |<_> || | || ' |<_> || '_>/ . |
* <___/<___||_|_|_| |_|_|_|<___|`_. ||_|_|<___||_| \___|
* <___'
* swm668, samwmaynard@gmail.com
* ___ ___ _ _ _
* / \ _ _ _ _ __ _ __ __ o O O / __| __ _ ___ | |_ _ _ | || |
* | - | | '_| | ' \ / _` | \ V / o \__ \ / _` | (_-< | _| | '_| \_, |
* |_|_| _|_|_ |_||_| \__,_| _\_/_ TS__[O] |___/ \__,_| /__/_ _\__| _|_|_ _|__/
* _|"""""|_|"""""|_|"""""|_|"""""|_|"""""| {======|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_| """"|
* "`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'
* as67648, arnavsastry@gmail.com
*/
#include <functional>
#include "Evaluator.h"
#include "ast/expression.h"
/*
* This skeleton currently only contains code to handle integer constants, print and read.
* It is your job to handle all of the rest of the L language.
*/
/*
* Call this function to report any run-time errors
* This will abort execution.
*/
void report_error(Expression* e, const string & s) {
cout << "Run-time error in expression " << e->to_value() << endl;
cout << s << endl;
exit(1);
}
Evaluator::Evaluator() {
sym_tab.push();
c = 0;
}
Expression* Evaluator::eval_unop(AstUnOp* b) {
Expression* e = b->get_expression();
Expression* eval_e = eval(e);
switch (b->get_unop_type()) {
case HD:
{
if (eval_e->get_type() == AST_LIST) {
AstList* l = static_cast<AstList*>(eval_e);
return l->get_hd();
} else {
return eval_e;
}
}
case TL:
{
if (eval_e->get_type() == AST_LIST) {
AstList* l = static_cast<AstList*>(eval_e);
return l->get_tl();
} else {
return AstNil::make();
}
}
case ISNIL:
{
if (eval_e->get_type() == AST_NIL) {
return AstInt::make(1);
} else {
return AstInt::make(0);
}
}
case PRINT:
{
if(eval_e->get_type() == AST_STRING) {
AstString* s = static_cast<AstString*>(eval_e);
cout << s->get_string() << endl;
} else {
cout << eval_e->to_value() << endl;
}
return AstInt::make(0);
}
default:
assert(false);
}
}
Expression* eval_binop_int(Expression* b, Expression* eval_e1, Expression* eval_e2, string x, const std::function<int(int, int)>& lambda) {
if (eval_e1->get_type() == AST_INT && eval_e2->get_type() == AST_INT) {
AstInt* i1 = static_cast<AstInt*>(eval_e1);
AstInt* i2 = static_cast<AstInt*>(eval_e2);
int c1 = i1->get_int();
int c2 = i2->get_int();
return AstInt::make(lambda(c1, c2));
} else {
if (eval_e1->get_type() == AST_LIST || eval_e2->get_type() == AST_LIST) {
report_error(b, "Binpo @ is the only legal binop for lists");
} else if (eval_e1->get_type() == AST_NIL || eval_e2->get_type() == AST_NIL) {
report_error(b, "Nil can only be used with binop @");
} else if (eval_e1->get_type() != eval_e2->get_type()) {
report_error(b, "Binop can only be applied to expressions of same type");
} else if (eval_e1->get_type() == AST_STRING && eval_e2->get_type() == AST_STRING) {
report_error(b, "Binop " + x + " cannot be applied to strings");
}
assert(false);
}
}
Expression* eval_binop_int_or_string(Expression* b, Expression* eval_e1, Expression* eval_e2, const std::function<int(int, int)>& int_lambda, const std::function<bool(string, string)>& string_lambda) {
if (eval_e1->get_type() == AST_INT && eval_e2->get_type() == AST_INT) {
AstInt* i1 = static_cast<AstInt*>(eval_e1);
AstInt* i2 = static_cast<AstInt*>(eval_e2);
int c1 = i1->get_int();
int c2 = i2->get_int();
return AstInt::make(int_lambda(c1, c2));
} else if (eval_e1->get_type() == AST_STRING && eval_e2->get_type() == AST_STRING){
AstString* s1 = static_cast<AstString*>(eval_e1);
AstString* s2 = static_cast<AstString*>(eval_e2);
string ss1 = s1->get_string();
string ss2 = s2->get_string();
return AstInt::make(string_lambda(ss1, ss2));
} else {
if (eval_e1->get_type() == AST_LIST || eval_e2->get_type() == AST_LIST) {
report_error(b, "Binpo @ is the only legal binop for lists");
} else if (eval_e1->get_type() == AST_NIL || eval_e2->get_type() == AST_NIL) {
report_error(b, "Nil can only be used with binop @");
} else if (eval_e1->get_type() != eval_e2->get_type()) {
report_error(b, "Binop can only be applied to expressions of same type");
}
assert(false);
}
}
Expression* Evaluator::eval_binop(AstBinOp* b) {
Expression* e1 = b->get_first();
Expression* e2 = b->get_second();
Expression* eval_e1 = eval(e1);
Expression* eval_e2 = eval(e2);
switch (b->get_binop_type()) {
case PLUS:
{
if (eval_e1->get_type() == AST_INT && eval_e2->get_type() == AST_INT) {
AstInt* i1 = static_cast<AstInt*>(eval_e1);
AstInt* i2 = static_cast<AstInt*>(eval_e2);
int c1 = i1->get_int();
int c2 = i2->get_int();
return AstInt::make(c1 + c2);
} else if (eval_e1->get_type() == AST_STRING && eval_e2->get_type() == AST_STRING){
AstString* s1 = static_cast<AstString*>(eval_e1);
AstString* s2 = static_cast<AstString*>(eval_e2);
string ss1 = s1->get_string();
string ss2 = s2->get_string();
return AstString::make(ss1 + ss2);
} else {
if (eval_e1->get_type() == AST_LIST || eval_e2->get_type() == AST_LIST) {
report_error(b, "Binpo @ is the only legal binop for lists");
} else if (eval_e1->get_type() == AST_NIL || eval_e2->get_type() == AST_NIL) {
report_error(b, "Nil can only be used with binop @");
} else if (eval_e1->get_type() != eval_e2->get_type()) {
report_error(b, "Binop can only be applied to expressions of same type");
}
assert(false);
}
break;
}
case MINUS:
return eval_binop_int(b, eval_e1, eval_e2, "-", [](int x, int y) { return x - y; });
case TIMES:
return eval_binop_int(b, eval_e1, eval_e2, "*", [](int x, int y) { return x * y; });
case DIVIDE:
return eval_binop_int(b, eval_e1, eval_e2, "/", [](int x, int y) { return x / y; });
case AND:
return eval_binop_int(b, eval_e1, eval_e2, "&", [](int x, int y) { return x && y; });
case OR:
return eval_binop_int(b, eval_e1, eval_e2, "|", [](int x, int y) { return x || y; });
case EQ:
return eval_binop_int_or_string(b, eval_e1, eval_e2, [](int x, int y) { return x == y; }, [](string x, string y) { return x == y; });
case NEQ:
return eval_binop_int_or_string(b, eval_e1, eval_e2, [](int x, int y) { return x != y; }, [](string x, string y) { return x != y; });
case LT:
return eval_binop_int(b, eval_e1, eval_e2, "<", [](int x, int y) { return x < y; });
case LEQ:
return eval_binop_int(b, eval_e1, eval_e2, "<=", [](int x, int y) { return x <= y; });
case GT:
return eval_binop_int(b, eval_e1, eval_e2, ">", [](int x, int y) { return x > y; });
case GEQ:
return eval_binop_int(b, eval_e1, eval_e2, ">=", [](int x, int y) { return x >= y; });
case CONS:
{
if (eval_e2->get_type() != AST_NIL) {
return AstList::make(eval_e1, eval_e2);
} else {
return eval_e1;
}
}
default:
{
assert(false);
return AstNil::make();
}
}
}
Expression* Evaluator::eval(Expression* e) {
Expression* res_exp = NULL;
switch (e->get_type()) {
case AST_INT:
{
res_exp = e;
break;
}
case AST_STRING:
{
res_exp = e;
break;
}
case AST_NIL:
{
res_exp = e;
break;
}
case AST_LIST:
{
res_exp = e;
break;
}
case AST_IDENTIFIER:
{
AstIdentifier* id = static_cast<AstIdentifier*>(e);
Expression* found = sym_tab.find(id);
if (found == NULL) {
string id_s = id->get_id();
report_error(e, "Identifier " + id_s + " is not bound in current context ");
}
res_exp = sym_tab.find(id);
break;
}
case AST_LET:
{
AstLet* let = static_cast<AstLet*>(e);
Expression* e1_prime = eval(let->get_val());
sym_tab.push();
sym_tab.add(let->get_id(), e1_prime);
Expression* e2_prime = eval(let->get_body());
sym_tab.pop();
res_exp = e2_prime;
break;
}
case AST_BRANCH:
{
AstBranch* branch = static_cast<AstBranch*>(e);
Expression* pred_prime = eval(branch->get_pred());
if (pred_prime->get_type() != AST_INT) {
report_error(e, "Predicate in conditional must be an integer");
}
AstInt* pred_int = static_cast<AstInt*>(pred_prime);
if (pred_int->get_int() != 0) {
res_exp = eval(branch->get_then_exp());
} else {
res_exp = eval(branch->get_else_exp());
}
break;
}
case AST_UNOP:
{
AstUnOp* b = static_cast<AstUnOp*>(e);
res_exp = eval_unop(b);
break;
}
case AST_BINOP:
{
AstBinOp* b = static_cast<AstBinOp*>(e);
res_exp = eval_binop(b);
break;
}
case AST_LAMBDA:
{
res_exp = e;
break;
}
case AST_IDENTIFIER_LIST:
{
assert(false);
break;
}
case AST_EXPRESSION_LIST:
{
AstExpressionList* el = static_cast<AstExpressionList*>(e);
const vector<Expression*> expressions = el->get_expressions();
Expression* eval_e1 = eval(expressions[0]);
// Evaluate expression lists of size 1
if (expressions.size() == 1) {
return eval_e1;
}
if (eval_e1->get_type() != AST_LAMBDA) {
report_error(e, "Only lambda expressions can be applied to other expressions");
}
AstLambda* lambda = static_cast<AstLambda*>(eval_e1);
AstIdentifier* identifier = lambda->get_formal();
Expression* body = lambda->get_body();
Expression* sub = body->substitute(identifier, expressions[1]);
Expression* sub_eval = eval(sub);
if (expressions.size() > 2) {
vector<Expression*> new_expressions = vector<Expression*>(expressions.begin() + 2, expressions.end());
new_expressions.insert(new_expressions.begin(), sub_eval);
AstExpressionList* new_el = AstExpressionList::make(new_expressions);
res_exp = eval(new_el);
} else {
res_exp = sub_eval;
}
break;
}
case AST_READ:
{
AstRead* r = static_cast<AstRead*>(e);
string input;
getline(cin, input);
if (r->read_integer()) {
res_exp = AstInt::make(string_to_int(input));
} else {
res_exp = AstString::make(input);
}
break;
}
default:
assert(false);
}
return res_exp;
}