-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
552 lines (493 loc) · 11.5 KB
/
ast.c
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#include "headers/ast.h"
#include "headers/lexer.h"
#include "headers/symbol.h"
extern bool SHOWTREE;
static bool brk = false;
static bool cont = false;
static bool retrn = false;
double getValue(astNode *root) {
switch (root->type) {
case NUM_nd:
return root->value;
break;
case ADD_nd:
return getValue(root->op.left) + getValue(root->op.right);
break;
case SUB_nd:
return getValue(root->op.left) - getValue(root->op.right);
break;
case MUL_nd:
return getValue(root->op.left) * getValue(root->op.right);
break;
case DIV_nd:
{
double rval = getValue(root->op.right);
if (rval == 0) {
fprintf(stderr, "Divison by 0.\n");
abort();
}
return getValue(root->op.left) / rval;
}
break;
case EXP_nd:
return pow(getValue(root->op.left), getValue(root->op.right));
break;
case MOD_nd:
return (int) getValue(root->op.left) % (int) getValue(root->op.right);
break;
case NEG_nd:
return -getValue(root->next);
break;
case NOT_nd:
return !getValue(root->next);
case GT_nd:
return (double) getValue(root->op.left) > getValue(root->op.right);
case LT_nd:
return (double) getValue(root->op.left) < getValue(root->op.right);
case GE_nd:
return (double) getValue(root->op.left) >= getValue(root->op.right);
case LE_nd:
return (double) getValue(root->op.left) <= getValue(root->op.right);
case EQ_nd:
return (double) getValue(root->op.left) == getValue(root->op.right);
case NEQ_nd:
return (double) getValue(root->op.left) != getValue(root->op.right);
case AND_nd:
if (!getValue(root->op.left))
return 0;
if (!getValue(root->op.right))
return 0;
return 1;
case OR_nd:
if (getValue(root->op.left))
return 1;
if (getValue(root->op.right))
return 1;
return 0;
case VAR_nd:
if (root->sym->type == UNDEF) {
fprintf(stderr, "Variable '%s' is undefined.\n", root->sym->name);
abort();
}
return root->sym->val;
case BLTIN_nd:
return root->sym->ptr(getValue(root->next));
case FNC_nd:
{
Frame *frm = callFunc(root);
if (retrn) {
retrn = false;
if (frm->retrn.type == NUMBER) {
return frm->retrn.val;
} else {
fprintf(stderr, "Function returns string, can't get value\n");
abort();
}
} else {
fprintf(stderr, "Nothing returned!\n");
abort();
}
return 0;
}
default:
fprintf(stderr, "Unable to get ast node value\n");
abort();
}
return 0;
}
astNode *newNode(nodeType typ) {
astNode *node = (astNode *) malloc(sizeof(astNode));
if (node == NULL) {
fprintf(stderr, "Failed to allocate memory creating astNode\n");
abort();
}
node->type = typ;
node->value = 0;
node->str = NULL;
node->op.left = node->op.right = node->next = NULL;
return node;
}
astNode *opNode(nodeType typ, astNode *left, astNode *right) {
astNode *opNd = newNode(typ);
opNd->op.left = left;
opNd->op.right = right;
return opNd;
}
astNode *unopNode(nodeType typ, astNode *next) {
astNode *unopNd = newNode(typ);
unopNd->next = next;
return unopNd;
}
astNode *numNode(double value) {
astNode *numNd = newNode(NUM_nd);
numNd->value = value;
return numNd;
}
astNode *varNode(Lexval *lval) {
astNode *varNd = newNode(VAR_nd);
varNd->sym = lval->sym;
return varNd;
}
astNode *strNode(Lexval *lval) {
astNode *strNd = newNode(STR_nd);
strNd->str = (char *) malloc(strlen(lval->str) + 1);
strcpy(strNd->str, lval->str);
return strNd;
}
void execute(astNode *root) {
if (SHOWTREE) { // Set by cmd line option
printTree(root, 0, false);
}
if (root == NULL) return;
if (root->type == ASGN_nd) {
// The symbol we're assigning to
Symbol *s = root->op.left->sym;
if (s->cnst) {
fprintf(stderr, "Cannot assign to constant value '%s'\n", s->name);
abort();
}
if (root->op.right->type == STR_nd) {
if (s->varType == STRING)
free(s->str);
s->type = VAR;
s->varType = STRING;
s->str = (char *) malloc(strlen(root->op.right->str)+1);
if (s->str == NULL) {
fprintf(stderr, "Str memory allocation failed\n");
abort();
}
strcpy(s->str, root->op.right->str);
// s->str = root->op.right->str;
} else if (root->op.right->type == VAR_nd &&
root->op.right->sym->varType == STRING) {
if (s->varType == STRING)
free(s->str);
s->type = VAR;
s->varType = STRING;
char *rstr = root->op.right->sym->str;
s->str = (char *) malloc(strlen(rstr)+1);
if (s->str == NULL) {
fprintf(stderr, "Str memory allocation failed\n");
abort();
}
strcpy(s->str, rstr);
// s->str = root->op.right->sym->str;
} else {
s->type = VAR;
s->varType = NUMBER;
if (root->next != NULL) {
switch (root->next->type) {
case ADD_nd:
s->val += getValue(root->op.right);
break;
case SUB_nd:
s->val -= getValue(root->op.right);
break;
case MUL_nd:
s->val *= getValue(root->op.right);
break;
case DIV_nd:
s->val /= getValue(root->op.right);
break;
case MOD_nd:
s->val = (int) s->val % (int) getValue(root->op.right);
break;
case EXP_nd:
s->val = pow(s->val, getValue(root->op.right));
break;
default:
// Shouldn't happen
break;
}
} else {
s->val = getValue(root->op.right);
}
}
} else if (root->type == FUNCDEC_nd) {
Symbol *s = root->op.left->sym;
if (s->cnst) {
fprintf(stderr, "Cannot create function with constant variable name '%s'\n", s->name);
abort();
}
s->type = FUNCTION;
s->code = root->op.right;
s->templt->args = root->next;
for (astNode *n=root->next; n != NULL; n=n->next) {
s->templt->nargs++;
}
// printf("%d arguments\n", s->templt->nargs);
} else if (root->type == FNC_nd) {
callFunc(root);
} else if (root->type == IF_nd) {
if (getValue(root->next) != 0) {
execute(root->op.left);
} else if (root->op.right != NULL) {
execute(root->op.right);
}
} else if (root->type == WHILE_nd) {
while (getValue(root->op.left) != 0) {
execute(root->op.right);
if (cont) {
cont = false;
continue;
}
if (brk) {
brk = false;
break;
}
}
} else if (root->type == STATEMENT_nd) {
if (root->op.left != NULL) {
execute(root->op.left);
}
if (!cont && !brk && !retrn) {
if (root->next != NULL) {
execute(root->next);
}
}
} else if (root->type == BREAK_nd) {
brk = true;
} else if (root->type == CONTINUE_nd) {
cont = true;
} else if (root->type == RETRN_nd) {
if (root->next->type == STR_nd) {
setReturn(STRING, 0, root->next->str);
} else {
setReturn(NUMBER, getValue(root->next), NULL);
}
retrn = true;
} else if (root->type == PRINT_nd) {
if (root->next != NULL) {
for (astNode *p = root->next; p != NULL; p = p->next) {
if (p->op.left != NULL) {
if (p->op.left->type == STR_nd) {
printf("%s", p->op.left->str);
} else {
if (p->op.left->type == VAR_nd) {
if (p->op.left->sym->varType == NUMBER) {
printf("%g", getValue(p->op.left));
} else {
printf("%s", p->op.left->sym->str);
}
} else if (p->op.left->type == FNC_nd) {
Frame *frm = callFunc(root);
if (retrn) {
retrn = false;
if (frm->retrn.type == NUMBER) {
printf("%g", frm->retrn.val);
} else {
printf("%s", frm->retrn.str);
}
} else {
fprintf(stderr, "Nothing returned!\n");
abort();
}
} else
printf("%g", getValue(p->op.left));
}
} else {
fprintf(stderr, "Empty argument node\n");
abort();
}
}
} else printf("\n");
} else if (root->type != STR_nd) {
getValue(root);
}
}
Frame *callFunc(astNode *root) {
Frame *f = loadFrame(copyFrame(root->sym->templt));
//loadFrame(root->sym->templt);
//refreshVars(root->sym->code);
refreshVars(f->args);
// Load up arguments
int supplied = 0;
astNode *argVars = f->args;
for (astNode *arg=root->next; arg != NULL; arg=arg->next) {
supplied++;
if (argVars == NULL) {
fprintf(stderr, "Too many arguments supplied\nOnly wanted %d\n",
f->nargs);
abort();
}
if (arg->op.left->type == STR_nd) {
Symbol *s = argVars->op.left->sym;
if (s->varType == STRING)
free(s->str);
s->type = VAR;
s->varType = STRING;
s->str = arg->op.left->str;
} else {
Symbol *s = argVars->op.left->sym;
s->type = VAR;
s->varType = NUMBER;
s->val = getValue(arg->op.left);
}
argVars = argVars->next;
}
if (argVars != NULL) {
fprintf(stderr, "Not enough arguments supplied\nWanted %d, got %d\n",
f->nargs, supplied);
abort();
}
refreshVars(root->sym->code);
execute(root->sym->code);
f = popFrame();
Frame *top = topFrame();
if (top->next != NULL) {
refreshVars(root->sym->code);
refreshVars(top->args);
}
return f;
}
void refreshVars(astNode *root) {
if (root != NULL) {
if (root->type == VAR_nd) {
Symbol *s = lookup(root->sym->name);
if (s == NULL) {
fprintf(stderr, "Couldn't find %s\n", root->sym->name);
abort();
}
root->sym = s;
}
refreshVars(root->next);
refreshVars(root->op.left);
refreshVars(root->op.right);
}
}
void printTree(astNode *root, int level, bool nxt) {
if (root != NULL) {
// LEFT
if (root->op.left != NULL) {
printTree(root->op.left, level + 1, false);
}
// ROOT
if (nxt) {
printf("───┼");
} else {
for (int i=0; i < level; i++) {
if (i == level-1)
printf("───┼");
else
printf(" ├");
}
}
switch (root->type) {
case NUM_nd:
printf("%g\n", root->value);
break;
case ADD_nd:
printf("ADD\n");
break;
case SUB_nd:
printf("SUB\n");
break;
case MUL_nd:
printf("MUL\n");
break;
case DIV_nd:
printf("DIV\n");
break;
case EXP_nd:
printf("EXPO\n");
break;
case MOD_nd:
printf("MOD\n");
break;
case NEG_nd:
printf("NEG");
break;
case ASGN_nd:
printf("ASGN\n");
break;
case VAR_nd:
printf("%s\n", root->sym->name);
break;
case BLTIN_nd:
printf("%s", root->sym->name);
break;
case NOT_nd:
printf("NOT");
break;
case GT_nd:
printf("GT\n");
break;
case LT_nd:
printf("LT\n");
break;
case GE_nd:
printf("GE\n");
break;
case LE_nd:
printf("LE\n");
break;
case EQ_nd:
printf("EQ\n");
break;
case NEQ_nd:
printf("NEQ\n");
break;
case AND_nd:
printf("AND\n");
break;
case OR_nd:
printf("OR\n");
break;
case IF_nd:
printf("IF");
break;
case WHILE_nd:
printf("WHILE");
break;
case STATEMENT_nd:
printf("STMNT");
break;
case BREAK_nd:
printf("BREAK");
break;
case CONTINUE_nd:
printf("CONTINUE");
break;
case PRINT_nd:
printf("PRINT");
break;
case ARG_nd:
printf("ARG");
break;
case FUNCDEC_nd:
printf("func decl");
break;
case FNC_nd:
printf("%s", root->sym->name);
break;
case RETRN_nd:
printf("RETURN");
break;
default:
printf("Unknown");
break;
}
}
// Next
if (root->next != NULL) {
printTree(root->next, level, true);
}
// RIGHT
if (root->op.right != NULL) {
printTree(root->op.right, level + 1, false);
}
}
void freeTree(astNode *root) {
if (root != NULL) {
if (root->next != NULL) freeTree(root->next);
if (root->op.left != NULL) freeTree(root->op.left);
if (root->op.right != NULL) freeTree(root->op.right);
if (root->str != NULL) free(root->str);
free(root);
}
}