-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreter.cpp
418 lines (335 loc) · 10.9 KB
/
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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <cstdio>
#include <cstdint>
#include "Interpreter.h"
#include "String.h"
#include "Parse.h"
// Parsing funtion prototypes
parse_tree_node * create_tree_node ();
parse_tree_node * create_tree_node_string (String);
parse_tree_node * create_tree_node_num (int32_t);
// Evaluation function prototypes
return_value eval_void (parse_tree_node *, symbol_table&, symbol_table&);
int32_t eval_op (parse_tree_node *, symbol_table&, symbol_table&);
template <typename T = int32_t> const T& eval_value (parse_tree_node *);
template <typename T = int32_t> T * eval_var (parse_tree_node *, symbol_table&, symbol_table&);
// Let's begin with the functions
return_value run (parse_tree_node * node, symbol_table& local, symbol_table& global)
{
return_value result;
while (node->type != FIN) {
result = eval_void(node, local, global);
if (!result.is_void)
return result;
node = node->jump_to;
}
// result.is_void must be true if the function has come this far
return result;
}
// Constructs parse tree nodes and pushes them into the parse tree
void parse (parse_tree * out)
{
while (out->last()->type != FIN) {
parse_tree_node * new_node = create_tree_node();
out->push_back (new_node);
}
}
// Creates a tree node of type MAGIC_STR
parse_tree_node * create_tree_node_string (String s)
{
parse_tree_node * new_node = new parse_tree_node;
new_node->type = MAGIC_STR;
new_node->value = new String (s);
new_node->value_type = STRING_VAL;
return new_node;
}
// Creates a tree node of type MAGIC_NUM
parse_tree_node * create_tree_node_num (int32_t n)
{
parse_tree_node * new_node = new parse_tree_node;
new_node->type = MAGIC_NUM;
new_node->value = new int32_t (n);
new_node->value_type = NUM_VAL;
return new_node;
}
// Creates a tree node of any type other than MAGIC_STR
parse_tree_node * create_tree_node ()
{
read_next_token ();
parse_tree_node * new_node = new parse_tree_node;
if (next_token_type == END) {
new_node->type = FIN;
}
else if (next_token_type == NAME) {
String name_type {next_token()};
new_node->type = STATEMENT;
new_node->value = new String (name_type);
new_node->value_type = STRING_VAL;
if (name_type == "text") {
read_next_token();
new_node->args.push_back(create_tree_node_string(next_token()));
}
else if (name_type == "output" || name_type == "return") {
new_node->args.push_back(create_tree_node());
}
else if (name_type == "var" || name_type == "set") {
read_next_token();
new_node->args.push_back(create_tree_node_string(next_token()));
new_node->args.push_back(create_tree_node());
}
else if (name_type == "if") {
parse_tree_node * condition = create_tree_node();
while (1) {
new_node->args.push_back(condition);
parse_tree block;
parse(&block);
new_node->args.push_back(block.begin());
if (*(String*)(block.last()->value) == "fi") {
block.strip();
break;
}
// We're leaving open the possibility here for "elif" type statements
if (*(String*)(block.last()->value) == "else")
condition = create_tree_node_num (1);
else
condition = create_tree_node();
block.strip();
}
}
else if (name_type == "do") {
parse_tree_node * condition = create_tree_node();
new_node->args.push_back(condition);
parse_tree block;
parse(&block);
new_node->args.push_back(block.begin());
block.strip();
}
else if (name_type == "defun") {
read_next_token();
parse_tree_node * fun_name = create_tree_node_string(next_token());
new_node->args.push_back(fun_name);
read_next_token(); // Skip "params" text
read_next_token();
while (String{next_token()} != "smarap") {
new_node->args.push_back(create_tree_node_string(next_token()));
read_next_token();
}
parse_tree block;
parse(&block);
new_node->args.push_back(block.begin());
block.strip();
}
else if (name_type == "call") {
read_next_token();
parse_tree_node * fun_name = create_tree_node_string(next_token());
new_node->args.push_back(fun_name);
read_next_token(); // Skip "args" text
while (String{peek_next_token()} != "sgra")
new_node->args.push_back(create_tree_node());
read_next_token(); // Skip "sgra" text
}
else if (name_type == "fi" || name_type == "else" || name_type == "od" || name_type == "nufed")
new_node->type = FIN;
else // A variable is being referenced.
new_node->type = VARIABLE;
}
else if (next_token_type == NUMBER) {
delete new_node;
new_node = create_tree_node_num (token_number_value);
}
else if (next_token_type == SYMBOL) {
String sym {next_token()};
new_node->type = OPERATOR;
new_node->value = new String (sym);
new_node->value_type = STRING_VAL;
// If we have reached a comment, just ignore it and move on
if (sym.size() >= 2 && sym.c_str()[0] == '/' && sym.c_str()[1] == '/') {
skip_line();
delete new_node;
return create_tree_node();
}
new_node->args.push_back(create_tree_node());
if (sym != "!" && sym != "~")
new_node->args.push_back(create_tree_node());
}
return new_node;
}
// Evaluates instructions that have no stored return value
return_value eval_void (parse_tree_node * node, symbol_table& table, symbol_table& global_table)
{
return_value result = {true};
switch (node->type) {
case STATEMENT:
{
String stmnt_type {eval_value<String> (node)};
if (stmnt_type == "text") {
printf ("%s", eval_value<String> (node->args[0]).c_str());
}
else if (stmnt_type == "output") {
printf ("%d", eval_op (node->args[0], table, global_table));
}
else if (stmnt_type == "var") {
String var_name = eval_value<String> (node->args[0]);
int32_t var_value = eval_op (node->args[1], table, global_table);
if (table.get(var_name)) {
printf("variable %s incorrectly re-initialized\n", var_name.c_str());
*table.get(var_name) = var_value;
break;
}
table.insert(var_name, var_value);
}
else if (stmnt_type == "set") {
String var_name = eval_value<String> (node->args[0]);
int32_t var_value = eval_op (node->args[1], table, global_table);
int32_t * local = table.get(var_name);
int32_t * global = global_table.get(var_name);
if (!local && !global) {
printf("variable %s not declared\n", var_name.c_str());
table.insert(var_name, var_value);
}
else if (local)
*local = var_value;
else
*global = var_value;
}
else if (stmnt_type == "if") {
for (int i = 0; i < node->args.size(); i += 2) {
if (eval_op(node->args[i], table, global_table)) {
result = run (node->args[i+1], table, global_table);
break;
}
}
}
else if (stmnt_type == "do")
while (eval_op(node->args[0], table, global_table) && result.is_void)
result = run (node->args[1], table, global_table);
// Functions are stored in the symbol table, just like any other variable
else if (stmnt_type == "defun") {
String fun_name = eval_value<String> (node->args[0]);
function new_fun;
int i = 1;
for (; i < node->args.size() - 1; i++)
new_fun.args.push_back(eval_value<String>(node->args[i]));
new_fun.block = node->args[i];
table.insert(fun_name, new_fun, FUN_VAL);
}
else if (stmnt_type == "return") {
result.is_void = false;
result.value = eval_op(node->args[0], table, global_table);
}
// Should never be reached
else
printf("Error: unknown statement in eval_void()\n");
break;
}
case NOP: case FIN: case MAGIC_NUM: case MAGIC_STR: case OPERATOR:
break;
default:
printf ("Error: unknown parse_tree_node_type in eval_void()\n");
break;
}
return result;
}
// Returns the "value" parameter of a node
template <typename T>
const T& eval_value (parse_tree_node * node)
{
return * ((T *) (node->value));
}
// Returns the value of a variable from the symbol table
template <typename T>
T * eval_var (parse_tree_node * node, symbol_table& local_table, symbol_table& global_table)
{
T * local = local_table.get<T>(eval_value<String> (node));
if (local)
return local;
return global_table.get<T>(eval_value<String> (node));
}
// Returns the value of a calculation resulting from the use of an operator
int32_t eval_op (parse_tree_node * node, symbol_table& table, symbol_table& global_table)
{
switch (node->type) {
case MAGIC_NUM:
return eval_value (node);
case VARIABLE:
return * eval_var (node, table, global_table);
case STATEMENT:
{
// A function has been called
function fun {*eval_var<function>(node->args[0], table, global_table)};
symbol_table local_table;
for (int i = 0; i < fun.args.size(); i++)
local_table.insert(fun.args[i], eval_op(node->args[i+1], table, global_table));
return_value result = run (fun.block, local_table, global_table);
if (result.is_void)
result.value = 0;
return result.value;
}
case OPERATOR:
{
String optype {eval_value<String> (node)};
if (optype == "+")
return eval_op (node->args[0], table, global_table) + eval_op (node->args[1], table, global_table);
if (optype == "-")
return eval_op (node->args[0], table, global_table) - eval_op (node->args[1], table, global_table);
if (optype == "*")
return eval_op (node->args[0], table, global_table) * eval_op (node->args[1], table, global_table);
if (optype == "/")
return eval_op (node->args[0], table, global_table) / eval_op (node->args[1], table, global_table);
if (optype == "%")
return eval_op (node->args[0], table, global_table) % eval_op (node->args[1], table, global_table);
if (optype == "&&") {
if (eval_op (node->args[0], table, global_table) && eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == "||") {
if (eval_op (node->args[0], table, global_table) || eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == "<") {
if (eval_op (node->args[0], table, global_table) < eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == ">") {
if (eval_op (node->args[0], table, global_table) > eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == "==") {
if (eval_op (node->args[0], table, global_table) == eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == "!=") {
if (eval_op (node->args[0], table, global_table) != eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == "<=") {
if (eval_op (node->args[0], table, global_table) <= eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == ">=") {
if (eval_op (node->args[0], table, global_table) >= eval_op (node->args[1], table, global_table))
return 1;
return 0;
}
if (optype == "!") {
if (eval_op (node->args[0], table, global_table))
return 0;
return 1;
}
if (optype == "~")
return -eval_op (node->args[0], table, global_table);
// Should never be reached
printf("Error: unknown operator in eval_op(): %s\n", optype.c_str());
return 0;
}
default:
printf("Error: unknown type in eval_op(): %d\n", node->type);
}
return 0;
}