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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#include "interpreter.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "hashmap.h"
typedef void *YY_BUFFER_STATE;
extern FILE *yyin;
extern ASTNode *ast_root;
extern int yyparse(void);
extern void yyrestart (FILE*);
extern YY_BUFFER_STATE yy_scan_string(const char*);
extern void yy_delete_buffer(YY_BUFFER_STATE);
typedef struct Context{
hashmap_t var_table;
hashmap_t proc_table;
} Context;
static ASTNode *context_get_proc(Context *context, const char *name) {
ASTNode **procdecl = (ASTNode**)hashmap_get(context->proc_table, name);
if (procdecl) return *procdecl;
return NULL;
}
static void context_set_proc(Context *context, const char *name, ASTNode *procdecl) {
hashmap_insert(context->proc_table, name, (void*)procdecl);
}
Context *context_create(void) {
Context *context = malloc(sizeof(Context));
assert(context);
context->var_table = hashmap_create();
context->proc_table = hashmap_create();
return context;
}
void context_free(Context *context) {
hashmap_free(context->var_table);
hashmap_keys_iter_t iter = hashmap_keys_iter_create(context->proc_table);
const char *key;
while ((key = hashmap_keys_iter_next(iter)) != NULL) {
ASTNode *procdecl = context_get_proc(context, key);
assert(procdecl);
ast_free(procdecl);
}
hashmap_keys_iter_free(iter);
hashmap_free(context->proc_table);
free(context);
}
int context_get_var(Context *context, const char *name) {
int *val = (int*)hashmap_get(context->var_table, name);
if (val) return *val;
return 0;
}
void context_set_var(Context *context, const char *name, int val) {
hashmap_insert(context->var_table, name, (void*)val);
}
void context_print_var_table(Context *context) {
hashmap_keys_iter_t iter = hashmap_keys_iter_create(context->var_table);
const char *key;
while ((key = hashmap_keys_iter_next(iter)) != NULL) {
int val = context_get_var(context, key);
printf("%s = %d\n", key, val);
}
hashmap_keys_iter_free(iter);
}
void context_print_proc_table(Context *context) {
hashmap_keys_iter_t iter = hashmap_keys_iter_create(context->proc_table);
const char *key;
while ((key = hashmap_keys_iter_next(iter)) != NULL) {
ASTNode *procdecl = context_get_proc(context, key);
printf("%s(", key);
ASTNodeList *args = procdecl->u.d_procdecl.args;
while (args) {
printf("%s", args->node->u.d_var.name);
args = args->next;
if (args) printf(", ");
}
printf("; ");
ASTNodeList *vargs = procdecl->u.d_procdecl.vargs;
while (vargs) {
printf("%s", vargs->node->u.d_var.name);
vargs = vargs->next;
if (vargs) printf(", ");
}
printf(")\n");
}
hashmap_keys_iter_free(iter);
}
void ast_print(ASTNode *node, int depth) {
int indent = depth * 2;
switch (node->type) {
case NT_SKIP: {
printf("%*sSKIP\n", indent, "");
break;
}
case NT_ASSIGN: {
printf("%*sASSIGN %s=%d\n", indent, "",
node->u.d_assign.var->u.d_var.name,
node->u.d_assign.aexp->u.d_int.val);
break;
}
case NT_SEQ: {
ast_print(node->u.d_seq.stm1, depth);
printf("%*sSEQ\n", indent, "");
ast_print(node->u.d_seq.stm2, depth);
break;
}
case NT_IF: {
printf("%*sIF (", indent, "");
ast_print(node->u.d_if.bexp, 0);
printf(")\n");
ast_print(node->u.d_if.stm1, depth + 1);
printf("%*sELSE\n", indent, "");
ast_print(node->u.d_if.stm2, depth + 1);
break;
}
case NT_WHILE: {
printf("%*sWHILE (", indent, "");
ast_print(node->u.d_while.bexp, 0);
printf(")\n");
ast_print(node->u.d_while.stm, depth + 1);
break;
}
case NT_INT: {
printf("%d", node->u.d_int.val);
break;
}
case NT_VAR: {
printf("%s", node->u.d_var.name);
break;
}
case NT_AOP: {
printf("(");
ast_print(node->u.d_aop.aexp1, 0);
switch (node->u.d_aop.aop) {
case AOP_ADD: printf(" + "); break;
case AOP_SUB: printf(" - "); break;
case AOP_MUL: printf(" * "); break;
default: assert(0);
}
ast_print(node->u.d_aop.aexp2, 0);
printf(")");
break;
}
case NT_BOP: {
printf("(");
ast_print(node->u.d_bop.bexp1, 0);
switch (node->u.d_bop.bop) {
case BOP_AND: printf(" && "); break;
case BOP_OR: printf(" || "); break;
default: assert(0);
}
ast_print(node->u.d_bop.bexp2, 0);
printf(")");
break;
}
case NT_NOT: {
printf("!");
ast_print(node->u.d_not.bexp, 0);
break;
}
case NT_ROP: {
printf("(");
ast_print(node->u.d_rop.aexp1, 0);
switch (node->u.d_rop.rop) {
case ROP_EQ: printf(" == "); break;
case ROP_NE: printf(" != "); break;
case ROP_LT: printf(" < "); break;
case ROP_LE: printf(" <= "); break;
case ROP_GT: printf(" > "); break;
case ROP_GE: printf(" >= "); break;
default: assert(0);
}
ast_print(node->u.d_rop.aexp2, 0);
printf(")");
break;
}
case NT_LET: {
printf("%*sLET %s = ", indent, "", node->u.d_let.var->u.d_var.name);
ast_print(node->u.d_let.aexp, 0);
printf("\n");
ast_print(node->u.d_let.stm, depth + 1);
break;
}
case NT_PROCDECL: {
printf("%*sPROC %s(", indent, "", node->u.d_procdecl.name);
ASTNodeList *args = node->u.d_procdecl.args;
while (args) {
printf("%s", args->node->u.d_var.name);
args = args->next;
if (args) printf(", ");
}
printf("; ");
ASTNodeList *vargs = node->u.d_procdecl.vargs;
while (vargs) {
printf("%s", vargs->node->u.d_var.name);
vargs = vargs->next;
if (vargs) printf(", ");
}
printf(")\n");
ast_print(node->u.d_procdecl.stm, depth + 1);
break;
}
case NT_PROCCALL: {
printf("%*sCALL %s(", indent, "", node->u.d_proccall.name);
ASTNodeList *args = node->u.d_proccall.args;
while (args) {
printf("%s", args->node->u.d_var.name);
args = args->next;
if (args) printf(", ");
}
printf("; ");
ASTNodeList *vargs = node->u.d_proccall.vargs;
while (vargs) {
printf("%s", vargs->node->u.d_var.name);
vargs = vargs->next;
if (vargs) printf(", ");
}
printf(")\n");
break;
}
default: assert(0);
}
}
static int eval_aexpr(Context *context, ASTNode *node) {
switch (node->type) {
case NT_INT: return node->u.d_int.val;
case NT_VAR: return context_get_var(context, node->u.d_var.name);
case NT_AOP: {
int aexp1_val = eval_aexpr(context, node->u.d_aop.aexp1);
int aexp2_val = eval_aexpr(context, node->u.d_aop.aexp2);
switch (node->u.d_aop.aop) {
case AOP_ADD: return aexp1_val + aexp2_val;
case AOP_SUB: return aexp1_val - aexp2_val;
case AOP_MUL: return aexp1_val * aexp2_val;
default: assert(0);
}
}
default: assert(0);
}
}
static int eval_bexpr(Context *context, ASTNode *node) {
switch (node->type) {
case NT_BOP: {
int bexp1_val = eval_bexpr(context, node->u.d_bop.bexp1);
int bexp2_val = eval_bexpr(context, node->u.d_bop.bexp2);
switch (node->u.d_bop.bop) {
case BOP_AND: return bexp1_val && bexp2_val;
case BOP_OR: return bexp1_val || bexp2_val;
default: assert(0);
}
}
case NT_NOT: return !eval_bexpr(context, node->u.d_not.bexp);
case NT_ROP: {
int aexp1_val = eval_aexpr(context, node->u.d_rop.aexp1);
int aexp2_val = eval_aexpr(context, node->u.d_rop.aexp2);
switch (node->u.d_rop.rop) {
case ROP_EQ: return aexp1_val == aexp2_val;
case ROP_NE: return aexp1_val != aexp2_val;
case ROP_LT: return aexp1_val < aexp2_val;
case ROP_LE: return aexp1_val <= aexp2_val;
case ROP_GT: return aexp1_val > aexp2_val;
case ROP_GE: return aexp1_val >= aexp2_val;
default: assert(0);
}
}
default: assert(0);
}
}
static int interp_proccall(Context *context, ASTNode *node) {
char *name = node->u.d_proccall.name;
ASTNode *procdecl = context_get_proc(context, name);
if (!procdecl) {
fprintf(stderr, "Error: procedure %s not defined\n", name);
return -1;
}
ASTNodeList *caller_args = node->u.d_proccall.args;
ASTNodeList *callee_args = procdecl->u.d_procdecl.args;
Context *proc_context = context_create();
hashmap_keys_iter_t iter = hashmap_keys_iter_create(context->proc_table);
const char *key;
while ((key = hashmap_keys_iter_next(iter)) != NULL) {
ASTNode *proc = context_get_proc(context, key);
context_set_proc(proc_context, key, ast_clone(proc));
}
hashmap_keys_iter_free(iter);
while (caller_args && callee_args) {
char *caller_arg_name = caller_args->node->u.d_var.name;
char *callee_arg_name = callee_args->node->u.d_var.name;
context_set_var(proc_context, callee_arg_name, context_get_var(context, caller_arg_name));
caller_args = caller_args->next;
callee_args = callee_args->next;
}
if (caller_args || callee_args) {
fprintf(stderr, "Error: procedure %s called with wrong number of value arguments\n", name);
context_free(proc_context);
return -1;
}
if (interp_ast(proc_context, procdecl->u.d_procdecl.stm)) {
context_free(proc_context);
return -1;
}
ASTNodeList *caller_vargs = node->u.d_proccall.vargs;
ASTNodeList *callee_vargs = procdecl->u.d_procdecl.vargs;
while (caller_vargs && callee_vargs) {
char *caller_varg_name = caller_vargs->node->u.d_var.name;
char *callee_varg_name = callee_vargs->node->u.d_var.name;
context_set_var(context, caller_varg_name, context_get_var(proc_context, callee_varg_name));
caller_vargs = caller_vargs->next;
callee_vargs = callee_vargs->next;
}
if (caller_vargs || callee_vargs) {
fprintf(stderr, "Error: procedure %s called with wrong number of variable arguments\n", name);
context_free(proc_context);
return -1;
}
context_free(proc_context);
return 0;
}
int interp_ast(Context *context, ASTNode *node) {
switch (node->type) {
case NT_SKIP: return 0;
case NT_ASSIGN: {
char *name = node->u.d_assign.var->u.d_var.name;
int val = eval_aexpr(context, node->u.d_assign.aexp);
context_set_var(context, name, val);
return 0;
}
case NT_SEQ:
if (interp_ast(context, node->u.d_seq.stm1)) return -1;
if (interp_ast(context, node->u.d_seq.stm2)) return -1;
return 0;
case NT_IF:
if (eval_bexpr(context, node->u.d_if.bexp)) return interp_ast(context, node->u.d_if.stm1);
else return interp_ast(context, node->u.d_if.stm2);
case NT_WHILE:
while (eval_bexpr(context, node->u.d_while.bexp)) {
if (interp_ast(context, node->u.d_while.stm)) return -1;
}
return 0;
case NT_LET: {
char *name = node->u.d_let.var->u.d_var.name;
int old_val = context_get_var(context, name);
int new_val = eval_aexpr(context, node->u.d_let.aexp);
context_set_var(context, name, new_val);
int ret = interp_ast(context, node->u.d_let.stm);
context_set_var(context, name, old_val);
return ret;
}
case NT_PROCDECL: {
char *name = node->u.d_procdecl.name;
ASTNode *procdecl_old = context_get_proc(context, name);
if (procdecl_old) {
fprintf(stderr, "Error: procedure %s already defined\n", name);
return -1;
}
ASTNode *procdecl = ast_clone(node);
context_set_proc(context, name, procdecl);
return 0;
}
case NT_PROCCALL: {
return interp_proccall(context, node);
}
default: assert(0);
}
}
int interp_file (Context *context, const char *path) {
yyin = fopen(path, "r");
if (!yyin) {
return -1;
}
yyrestart(yyin);
if (yyparse()) {
ast_free(ast_root);
fclose(yyin);
return -1;
}
if (interp_ast(context, ast_root)) {
ast_free(ast_root);
fclose(yyin);
return -1;
}
ast_free(ast_root);
fclose(yyin);
return 0;
}
int interp_str (Context *context, const char *str) {
YY_BUFFER_STATE buf = yy_scan_string(str);
if (yyparse()) {
ast_free(ast_root);
yy_delete_buffer(buf);
return -1;
}
if (interp_ast(context, ast_root)) {
ast_free(ast_root);
yy_delete_buffer(buf);
return -1;
}
yy_delete_buffer(buf);
return 0;
}
int print_ast_file (const char *path) {
yyin = fopen(path, "r");
if (!yyin) {
return -1;
}
yyrestart(yyin);
if (yyparse()) {
ast_free(ast_root);
fclose(yyin);
return -1;
}
ast_print(ast_root, 0);
ast_free(ast_root);
fclose(yyin);
return 0;
}
|