forked from DoctorWkt/acwj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.c
44 lines (38 loc) · 1.01 KB
/
interp.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
#include "defs.h"
#include "data.h"
#include "decl.h"
// AST tree interpreter
// Copyright (c) 2019 Warren Toomey, GPL3
// List of AST operators
static char *ASTop[] = { "+", "-", "*", "/" };
// Given an AST, interpret the
// operators in it and return
// a final value.
int interpretAST(struct ASTnode *n) {
int leftval, rightval;
// Get the left and right sub-tree values
if (n->left)
leftval = interpretAST(n->left);
if (n->right)
rightval = interpretAST(n->right);
// Debug: Print what we are about to do
if (n->op == A_INTLIT)
printf("int %d\n", n->intvalue);
else
printf("%d %s %d\n", leftval, ASTop[n->op], rightval);
switch (n->op) {
case A_ADD:
return (leftval + rightval);
case A_SUBTRACT:
return (leftval - rightval);
case A_MULTIPLY:
return (leftval * rightval);
case A_DIVIDE:
return (leftval / rightval);
case A_INTLIT:
return (n->intvalue);
default:
fprintf(stderr, "Unknown AST operator %d\n", n->op);
exit(1);
}
}