-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
365 lines (325 loc) · 10.1 KB
/
util.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
#include "util.h"
#include "lex.yy.h"
#include <stdarg.h>
const char* tokenToString(TokenType t)
{
switch (t) {
case ERROR:
return "ERROR";
case IF:
return "IF";
case ELSE:
return "ELSE";
case INT:
return "INT";
case RETURN:
return "RETURN";
case VOID:
return "VOID";
case WHILE:
return "WHILE";
case PLUS:
return "+";
case MINUS:
return "-";
case MULTIPLY:
return "*";
case DIVIDE:
return "/";
case LESSTHAN:
return "<";
case LESSTHANEQ:
return "<=";
case GREATERTHAN:
return ">";
case GREATERTHANEQ:
return ">=";
case EQUAL:
return "==";
case NOTEQUAL:
return "!=";
case ASSIGN:
return "=";
case SEMICOLON:
return ";";
case COMMA:
return ",";
case LPAREN:
return "(";
case RPAREN:
return ")";
case LBRACKET:
return "[";
case RBRACKET:
return "]";
case LBRACE:
return "{";
case RBRACE:
return "}";
case ID:
return "ID";
case NUM:
return "NUM";
}
return "Unknown";
}
#ifdef __GNUC__
__attribute__((__format__(__printf__, 2, 3)))
#endif
static int
iprintf(int level, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
for (int i = 0; i < level; ++i) {
printf(" ");
}
int result = vprintf(fmt, args);
va_end(args);
return result;
}
static void printSubTree(Node node, int level)
{
if (node->kind == NodeExpr) {
switch (node->expr) {
case ExprConst:
iprintf(level, "Const : %d\n", node->value.num);
break;
case ExprId:
iprintf(level, "Id : %s\n", node->value.name);
break;
case ExprIndex:
iprintf(level, "Index id: %s\n", node->value.name);
printSubTree(node->children[0], level + 1);
break;
case ExprCall:
iprintf(level, "Call id: %s\n", node->value.name);
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
case ExprBinOp:
iprintf(level, "Op %s\n", opToString(node->value.op));
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
case ExprAssign:
iprintf(level, "Assign\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
}
} else {
switch (node->stmt) {
case StmtReturn:
iprintf(level, "Return\n");
if (node->num_children == 1) {
printSubTree(node->children[0], level + 1);
}
break;
case StmtWhile:
iprintf(level, "While\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
case StmtIf:
iprintf(level, "If\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
case StmtExprStmt:
printSubTree(node->children[0], level);
break;
case StmtCompoundStmt:
iprintf(level, "Compound Statement\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
case StmtParamList:
iprintf(level, "Parameters\n");
if (node->num_children == 0) {
iprintf(level + 1, "(Void)\n");
} else {
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
}
break;
case StmtParam:
if (node->value.param.is_array) {
iprintf(level, "Parameter id : %s\tType : %s[]\n",
node->value.param.name,
typeToString(node->value.param.kind));
} else {
iprintf(level, "Parameter id : %s\tType : %s\n",
node->value.param.name,
typeToString(node->value.param.kind));
}
break;
case StmtFunction:
iprintf(level, "Function id : %s\tType : %s\n",
node->value.func.name,
typeToString(node->value.func.return_type));
// parameter
printSubTree(node->children[0], level + 1);
// body
printSubTree(node->children[1], level + 1);
break;
case StmtVar:
if (node->value.var.is_array) {
iprintf(level, "Variable id : %s\tType : %s[%d]\n",
node->value.var.name,
typeToString(node->value.var.kind),
node->value.var.array_size);
} else {
iprintf(level, "Variable id : %s\tType : %s\n",
node->value.var.name,
typeToString(node->value.var.kind));
}
break;
case StmtDeclList:
iprintf(level, "Syntax Tree:\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTree(node->children[i], level + 1);
}
break;
}
}
}
void printTree(Node node)
{
printSubTree(node, 0);
}
static void printSubTreeLatex(Node node, int level)
{
if (node->kind == NodeExpr) {
switch (node->expr) {
case ExprConst:
iprintf(level, "[{%d}]\n", node->value.num);
break;
case ExprId:
iprintf(level, "[{%s}]\n", node->value.name);
break;
case ExprIndex:
iprintf(level, "[{Index %s}\n", node->value.name);
printSubTreeLatex(node->children[0], level + 1);
iprintf(level, "]\n");
break;
case ExprCall:
iprintf(level, "[{Call %s}\n", node->value.name);
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
case ExprBinOp:
iprintf(level, "[{%s}\n", opToString(node->value.op));
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
case ExprAssign:
iprintf(level, "[{Assign}\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
}
} else {
switch (node->stmt) {
case StmtReturn:
iprintf(level, "[{Return}\n");
if (node->num_children == 1) {
printSubTreeLatex(node->children[0], level + 1);
}
iprintf(level, "]\n");
break;
case StmtWhile:
iprintf(level, "[{While}\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
case StmtIf:
iprintf(level, "[{If}\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
case StmtExprStmt:
printSubTreeLatex(node->children[0], level);
break;
case StmtCompoundStmt:
iprintf(level, "[{Compound Statement}\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
case StmtParamList:
iprintf(level, "[{Parameters}\n");
if (node->num_children == 0) {
iprintf(level + 1, "[{(Void)}]\n");
} else {
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
}
iprintf(level, "]\n");
break;
case StmtParam:
if (node->value.param.is_array) {
iprintf(level, "[{%s : %s[]}]\n",
node->value.param.name,
typeToString(node->value.param.kind));
} else {
iprintf(level, "[{%s : %s}]\n",
node->value.param.name,
typeToString(node->value.param.kind));
}
break;
case StmtFunction:
iprintf(level, "[{Fun %s : %s}\n",
node->value.func.name,
typeToString(node->value.func.return_type));
// parameter
printSubTreeLatex(node->children[0], level + 1);
// body
printSubTreeLatex(node->children[1], level + 1);
iprintf(level, "]\n");
break;
case StmtVar:
if (node->value.var.is_array) {
iprintf(level, "[{Var %s : %s[%d]}]\n",
node->value.var.name,
typeToString(node->value.var.kind),
node->value.var.array_size);
} else {
iprintf(level, "[{Var %s : %s}]\n",
node->value.var.name,
typeToString(node->value.var.kind));
}
break;
case StmtDeclList:
iprintf(level, "[{Program}\n");
for (int i = 0; i < node->num_children; ++i) {
printSubTreeLatex(node->children[i], level + 1);
}
iprintf(level, "]\n");
break;
}
}
}
void printTreeLatex(Node node)
{
printSubTreeLatex(node, 0);
}