-
Notifications
You must be signed in to change notification settings - Fork 0
/
prabsyn.c
322 lines (303 loc) · 9.9 KB
/
prabsyn.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
/*
* prabsyn.c - Print Abstract Syntax data structures. Most functions
* handle an instance of an abstract syntax rule.
*/
#include "prabsyn.h" /* function prototype */
#include <stdio.h>
#include "absyn.h" /* abstract syntax data structures */
#include "symbol.h" /* symbol table data structures */
#include "util.h"
/* local function prototypes */
static void pr_var(FILE *out, A_var v, int d);
static void pr_dec(FILE *out, A_dec v, int d);
static void pr_ty(FILE *out, A_ty v, int d);
static void pr_field(FILE *out, A_field v, int d);
static void pr_fieldList(FILE *out, A_fieldList v, int d);
static void pr_expList(FILE *out, A_expList v, int d);
static void pr_fundec(FILE *out, A_fundec v, int d);
static void pr_fundecList(FILE *out, A_fundecList v, int d);
static void pr_decList(FILE *out, A_decList v, int d);
static void pr_namety(FILE *out, A_namety v, int d);
static void pr_nametyList(FILE *out, A_nametyList v, int d);
static void pr_efield(FILE *out, A_efield v, int d);
static void pr_efieldList(FILE *out, A_efieldList v, int d);
static void indent(FILE *out, int d) {
int i;
for (i = 0; i <= d; i++) fprintf(out, " ");
}
/* Print A_var types. Indent d spaces. */
static void pr_var(FILE *out, A_var v, int d) {
indent(out, d);
switch (v->kind) {
case A_simpleVar:
fprintf(out, "simpleVar(%s)", S_name(v->u.simple));
break;
case A_fieldVar:
fprintf(out, "%s\n", "fieldVar(");
pr_var(out, v->u.field.var, d + 1);
fprintf(out, "%s\n", ",");
indent(out, d + 1);
fprintf(out, "%s)", S_name(v->u.field.sym));
break;
case A_subscriptVar:
fprintf(out, "%s\n", "subscriptVar(");
pr_var(out, v->u.subscript.var, d + 1);
fprintf(out, "%s\n", ",");
pr_exp(out, v->u.subscript.exp, d + 1);
fprintf(out, "%s", ")");
break;
default:
assert(0);
}
}
static char str_oper[][12] = {"PLUS", "MINUS", "TIMES", "DIVIDE",
"EQUAL", "NOTEQUAL", "LESSTHAN", "LESSEQ",
"GREAT", "GREATEQ"};
static void pr_oper(FILE *out, A_oper d) { fprintf(out, "%s", str_oper[d]); }
/* Print A_var types. Indent d spaces. */
void pr_exp(FILE *out, A_exp v, int d) {
indent(out, d);
switch (v->kind) {
case A_varExp:
fprintf(out, "varExp(\n");
pr_var(out, v->u.var, d + 1);
fprintf(out, "%s", ")");
break;
case A_nilExp:
fprintf(out, "nilExp()");
break;
case A_intExp:
fprintf(out, "intExp(%d)", v->u.intt);
break;
case A_stringExp:
fprintf(out, "stringExp(%s)", v->u.stringg);
break;
case A_callExp:
fprintf(out, "callExp(%s,\n", S_name(v->u.call.func));
pr_expList(out, v->u.call.args, d + 1);
fprintf(out, ")");
break;
case A_opExp:
fprintf(out, "opExp(\n");
indent(out, d + 1);
pr_oper(out, v->u.op.oper);
fprintf(out, ",\n");
pr_exp(out, v->u.op.left, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.op.right, d + 1);
fprintf(out, ")");
break;
case A_recordExp:
fprintf(out, "recordExp(%s,\n", S_name(v->u.record.typ));
pr_efieldList(out, v->u.record.fields, d + 1);
fprintf(out, ")");
break;
case A_seqExp:
fprintf(out, "seqExp(\n");
pr_expList(out, v->u.seq, d + 1);
fprintf(out, ")");
break;
case A_assignExp:
fprintf(out, "assignExp(\n");
pr_var(out, v->u.assign.var, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.assign.exp, d + 1);
fprintf(out, ")");
break;
case A_ifExp:
fprintf(out, "iffExp(\n");
pr_exp(out, v->u.iff.test, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.iff.then, d + 1);
if (v->u.iff.elsee) { /* else is optional */
fprintf(out, ",\n");
pr_exp(out, v->u.iff.elsee, d + 1);
}
fprintf(out, ")");
break;
case A_whileExp:
fprintf(out, "whileExp(\n");
pr_exp(out, v->u.whilee.test, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.whilee.body, d + 1);
fprintf(out, ")\n");
break;
case A_forExp:
fprintf(out, "forExp(%s,\n", S_name(v->u.forr.var));
pr_exp(out, v->u.forr.lo, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.forr.hi, d + 1);
fprintf(out, "%s\n", ",");
pr_exp(out, v->u.forr.body, d + 1);
fprintf(out, ",\n");
indent(out, d + 1);
fprintf(out, "%s", v->u.forr.escape ? "TRUE)" : "FALSE)");
break;
case A_breakExp:
fprintf(out, "breakExp()");
break;
case A_letExp:
fprintf(out, "letExp(\n");
pr_decList(out, v->u.let.decs, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.let.body, d + 1);
fprintf(out, ")");
break;
case A_arrayExp:
fprintf(out, "arrayExp(%s,\n", S_name(v->u.array.typ));
pr_exp(out, v->u.array.size, d + 1);
fprintf(out, ",\n");
pr_exp(out, v->u.array.init, d + 1);
fprintf(out, ")");
break;
default:
assert(0);
}
}
static void pr_dec(FILE *out, A_dec v, int d) {
indent(out, d);
switch (v->kind) {
case A_functionDec:
fprintf(out, "functionDec(\n");
pr_fundecList(out, v->u.function, d + 1);
fprintf(out, ")");
break;
case A_varDec:
fprintf(out, "varDec(%s,\n", S_name(v->u.var.var));
if (v->u.var.typ) {
indent(out, d + 1);
fprintf(out, "%s,\n", S_name(v->u.var.typ));
}
pr_exp(out, v->u.var.init, d + 1);
fprintf(out, ",\n");
indent(out, d + 1);
fprintf(out, "%s", v->u.var.escape ? "TRUE)" : "FALSE)");
break;
case A_typeDec:
fprintf(out, "typeDec(\n");
pr_nametyList(out, v->u.type, d + 1);
fprintf(out, ")");
break;
default:
assert(0);
}
}
static void pr_ty(FILE *out, A_ty v, int d) {
indent(out, d);
switch (v->kind) {
case A_nameTy:
fprintf(out, "nameTy(%s)", S_name(v->u.name));
break;
case A_recordTy:
fprintf(out, "recordTy(\n");
pr_fieldList(out, v->u.record, d + 1);
fprintf(out, ")");
break;
case A_arrayTy:
fprintf(out, "arrayTy(%s)", S_name(v->u.array));
break;
default:
assert(0);
}
}
static void pr_field(FILE *out, A_field v, int d) {
indent(out, d);
fprintf(out, "field(%s,\n", S_name(v->name));
indent(out, d + 1);
fprintf(out, "%s,\n", S_name(v->typ));
indent(out, d + 1);
fprintf(out, "%s", v->escape ? "TRUE)" : "FALSE)");
}
static void pr_fieldList(FILE *out, A_fieldList v, int d) {
indent(out, d);
if (v) {
fprintf(out, "fieldList(\n");
pr_field(out, v->head, d + 1);
fprintf(out, ",\n");
pr_fieldList(out, v->tail, d + 1);
fprintf(out, ")");
} else
fprintf(out, "fieldList()");
}
static void pr_expList(FILE *out, A_expList v, int d) {
indent(out, d);
if (v) {
fprintf(out, "expList(\n");
pr_exp(out, v->head, d + 1);
fprintf(out, ",\n");
pr_expList(out, v->tail, d + 1);
fprintf(out, ")");
} else
fprintf(out, "expList()");
}
static void pr_fundec(FILE *out, A_fundec v, int d) {
indent(out, d);
fprintf(out, "fundec(%s,\n", S_name(v->name));
pr_fieldList(out, v->params, d + 1);
fprintf(out, ",\n");
if (v->result) {
indent(out, d + 1);
fprintf(out, "%s,\n", S_name(v->result));
}
pr_exp(out, v->body, d + 1);
fprintf(out, ")");
}
static void pr_fundecList(FILE *out, A_fundecList v, int d) {
indent(out, d);
if (v) {
fprintf(out, "fundecList(\n");
pr_fundec(out, v->head, d + 1);
fprintf(out, ",\n");
pr_fundecList(out, v->tail, d + 1);
fprintf(out, ")");
} else
fprintf(out, "fundecList()");
}
static void pr_decList(FILE *out, A_decList v, int d) {
indent(out, d);
if (v) {
fprintf(out, "decList(\n");
pr_dec(out, v->head, d + 1);
fprintf(out, ",\n");
pr_decList(out, v->tail, d + 1);
fprintf(out, ")");
} else
fprintf(out, "decList()");
}
static void pr_namety(FILE *out, A_namety v, int d) {
indent(out, d);
fprintf(out, "namety(%s,\n", S_name(v->name));
pr_ty(out, v->ty, d + 1);
fprintf(out, ")");
}
static void pr_nametyList(FILE *out, A_nametyList v, int d) {
indent(out, d);
if (v) {
fprintf(out, "nametyList(\n");
pr_namety(out, v->head, d + 1);
fprintf(out, ",\n");
pr_nametyList(out, v->tail, d + 1);
fprintf(out, ")");
} else
fprintf(out, "nametyList()");
}
static void pr_efield(FILE *out, A_efield v, int d) {
indent(out, d);
if (v) {
fprintf(out, "efield(%s,\n", S_name(v->name));
pr_exp(out, v->exp, d + 1);
fprintf(out, ")");
} else
fprintf(out, "efield()");
}
static void pr_efieldList(FILE *out, A_efieldList v, int d) {
indent(out, d);
if (v) {
fprintf(out, "efieldList(\n");
pr_efield(out, v->head, d + 1);
fprintf(out, ",\n");
pr_efieldList(out, v->tail, d + 1);
fprintf(out, ")");
} else
fprintf(out, "efieldList()");
}