-
Notifications
You must be signed in to change notification settings - Fork 20
/
AST.c
204 lines (192 loc) · 3.26 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
#include <stdlib.h>
#include "AST.h"
#ifdef DEBUG
// for debug
char *code_name(enum code op)
{
switch (op) {
case LIST:
return "LIST";
case NUM:
return "NUM";
case STR:
return "STR";
case SYM:
return "SYM";
case EX_EQ:
return "EX_EQ";
case PLUS_OP:
return "PLUS_OP";
case MINUS_OP:
return "MINUS_OP";
case MUL_OP:
return "MUL_OP";
case LT_OP:
return "LT_OP";
case GT_OP:
return "GT_OP";
case GET_ARRAY_OP:
return "GET_ARRAY_OP";
case SET_ARRAY_OP:
return "SET_ARRAY_OP";
case CALL_OP:
return "CALL_OP";
// case PRINTLN_OP:
// return "PRINTLN_OP";
case IF_STATEMENT:
return "IF_STATEMENT";
case BLOCK_STATEMENT:
return "BLOCK_STATEMENT";
case RETURN_STATEMENT:
return "RETURN_STATEMENT";
case WHILE_STATEMENT:
return "WHILE_STATEMENT";
case FOR_STATEMENT:
return "FOR_STATEMENT";
default:
return "???";
}
}
void _printAST(AST *p)
{
if (p == NULL) {
printf("()");
return;
}
switch (p->op) {
case NUM:
printf("%d", p->val);
break;
case STR:
printf("'%s'", p->str);
break;
case SYM:
printf("'%s'", p->sym->name);
break;
case LIST:
printf("(LIST ");
while (p != NULL) {
_printAST(p->left);
p = p->right;
if (p != NULL) {
printf(" ");
}
}
printf(")");
break;
default:
printf("(%s ", code_name(p->op));
_printAST(p->left);
printf(" ");
_printAST(p->right);
printf(")");
}
fflush(stdout);
}
void printAST(AST *p)
{
printf("\033[1m\033[35m");
_printAST(p);
printf("\033[0m\n");
}
#else
char *code_name(enum code op) { return "-"; }
void printAST(AST *p) {}
#endif
Symbol SymbolTable[MAX_SYMBOLS];
int n_symbols = 0;
AST *makeNum(int val)
{
AST *p = (AST *)malloc(sizeof(AST));
p->op = NUM;
p->val = val;
return p;
}
AST *makeStr(char *s)
{
AST *p = (AST *)malloc(sizeof(AST));
p->op = STR;
p->str = s;
return p;
}
AST *makeAST(enum code op, AST *left, AST *right)
{
// printf("[%d,%x,%x]\n",op,left,right);
AST *p = (AST *)malloc(sizeof(AST));
p->op = op;
p->left = left;
p->right = right;
return p;
}
AST *getNth(AST *p, int nth)
{
if (p->op != LIST) {
fprintf(stderr, "bad access to list %d\n", nth);
printAST(p);
printf("\n");
exit(1);
}
if (nth > 0) {
return (getNth(p->right, nth-1));
} else {
return p->left;
}
}
AST *addLast(AST *l, AST *p)
{
if (l == NULL) {
return makeAST(LIST, p, NULL);
}
AST *q = l;
while (q->right != NULL) {
q = q->right;
}
q->right = (p->op!=LIST) ? makeAST(LIST, p, NULL) : p;
return l;
}
AST *getNext(AST *p)
{
if (p->op != LIST) {
fprintf(stderr, "bad access to list\n");
printAST(p);
printf("\n");
exit(1);
} else {
return p->right;
}
}
Symbol *lookupSymbol(char *name)
{
Symbol *sp = NULL;
for (int i=0; i < n_symbols; i++) {
if (strcmp(SymbolTable[i].name, name) == 0) {
sp = &SymbolTable[i];
break;
}
}
if (sp == NULL) {
sp = &SymbolTable[n_symbols++];
sp->name = strdup(name);
}
return sp;
}
AST *makeSymbol(char *name)
{
//printf("SYM:[%s]\n", name);
AST *p = (AST *)malloc(sizeof(AST));
p->op = SYM;
p->sym = lookupSymbol(name);
return p;
}
Symbol *getSymbol(AST *p)
{
//printf("getSymbol [%x]\n", p);
if (!p) fprintf(stderr, "AST is null at getSymbol!\n");
if (p->op != SYM) {
fprintf(stderr, "bad access to symbol\n");
printAST(p);
printf("\n");
exit(1);
}
return p->sym;
}