-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.c
378 lines (367 loc) · 9.91 KB
/
print.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
void print_expr(Expr *expr);
void print_typespec(Typespec *typespec);
void print_stmt(Stmt *stmt);
#define INDENT_WIDTH 4
static int print_indent = 0;
void print_newline(void) {
if (print_indent > 0)
printf("\n%*s", print_indent*INDENT_WIDTH, " ");
else
printf("\n");
}
void print_expr(Expr *expr) {
if (expr == NULL) {
printf("NULL");
return;
}
switch (expr->kind) {
case EXPR_INT: {
bool is_unsigned = IS_SET(expr->mod, TOKENMOD_UNSIGNED);
char *fmt = is_unsigned ? "%ull" : "%lld";
if (IS_SET(expr->mod, TOKENMOD_HEX) || IS_SET(expr->mod, TOKENMOD_BIN)) {
fmt = is_unsigned ? "%#Xull" : "%#Xll";
} else if (IS_SET(expr->mod, TOKENMOD_OCT)) {
fmt = is_unsigned ? "%#oull" : "%#oll";
}
printf(fmt, expr->int_val);
break;
}
case EXPR_FLOAT:
printf("%f", expr->float_val);
break;
case EXPR_BOOL:
printf("%s", expr->bool_val ? "true" : "false");
break;
case EXPR_STR:
printf("\"%s\"", expr->str_val);
break;
case EXPR_NAME:
printf("%s", expr->name);
break;
case EXPR_UNARY:
if (expr->unary.op == '!')
printf("(not ");
else
printf("(%c ", expr->unary.op);
print_expr(expr->unary.expr);
printf(")");
break;
case EXPR_BINARY:
if (expr->binary.op == TOKEN_LOGICAL_OR) {
printf("(or ");
} else if (expr->binary.op == TOKEN_LOGICAL_AND) {
printf("(and ");
} else {
printf("(%s ", token_kind_to_str(expr->binary.op));
}
print_expr(expr->binary.left);
printf(" ");
print_expr(expr->binary.right);
printf(")");
break;
case EXPR_TERNARY:
printf("(ternary ");
print_expr(expr->ternary.cond);
printf(" ");
print_expr(expr->ternary.then_expr);
printf(" ");
print_expr(expr->ternary.else_expr);
printf(")");
break;
case EXPR_CALL:
printf("(%s", expr->call.expr->name);
for (int i=0; i<expr->call.num_args; ++i) {
printf(" ");
print_expr(expr->call.args[i]);
}
printf(")");
break;
case EXPR_INDEX:
printf("(index ");
print_expr(expr->index.expr);
printf(" ");
print_expr(expr->index.index);
printf(")");
break;
case EXPR_FIELD:
printf("(field ");
print_expr(expr->field.expr);
printf(" %s)", expr->field.name);
break;
case EXPR_COMPOUND:
printf("(");
print_typespec(expr->compound.typespec);
++print_indent;
for (int i=0; i<expr->compound.num_args; ++i) {
print_newline();
print_expr(expr->compound.args[i].field_value);
}
--print_indent;
printf(")");
break;
case EXPR_CAST:
printf("(cast ");
print_typespec(expr->cast.typespec);
printf(" ");
print_expr(expr->cast.expr);
printf(")");
break;
case EXPR_SIZEOF_EXPR:
printf("(sizeof ");
print_expr(expr->sizeof_expr);
printf(")");
break;
case EXPR_SIZEOF_TYPE:
printf("(sizeof ");
print_typespec(expr->sizeof_typespec);
printf(")");
break;
default:
printf("Error: Printer: Unknown expr kind: %d\n", expr->kind);
assert(0);
break;
}
}
void print_stmt_block(StmtBlock block) {
printf("(block");
++print_indent;
for (int i=0; i<block.num_stmts; ++i) {
print_newline();
print_stmt(block.stmts[i]);
}
--print_indent;
printf(")");
}
void print_stmt(Stmt *stmt) {
if (stmt == NULL) {
printf("NULL");
return;
}
switch (stmt->kind) {
case STMT_RETURN:
printf("(return");
if (stmt->return_stmt.expr) {
printf(" ");
print_expr(stmt->return_stmt.expr);
}
printf(")");
break;
case STMT_CONTINUE:
printf("continue");
break;
case STMT_BREAK:
printf("break");
break;
case STMT_BRACE_BLOCK:
print_stmt_block(stmt->block);
break;
case STMT_EXPR:
print_expr(stmt->expr);
break;
case STMT_IF:
printf("(if ");
print_expr(stmt->if_stmt.cond);
++print_indent;
print_newline();
print_stmt_block(stmt->if_stmt.then_block);
--print_indent;
for (int i=0; i<stmt->if_stmt.num_else_ifs; ++i) {
ElseIf else_if = stmt->if_stmt.else_ifs[i];
print_newline();
printf("(elseif ");
print_expr(else_if.cond);
++print_indent;
print_newline();
print_stmt_block(else_if.block);
--print_indent;
}
if (stmt->if_stmt.else_block.num_stmts > 0) {
print_newline();
print_stmt_block(stmt->if_stmt.else_block);
}
printf(")");
break;
case STMT_INIT:
printf("(init %s ", stmt->init.name);
print_typespec(stmt->init.typespec);
printf(" ");
print_expr(stmt->init.expr);
printf(")");
break;
case STMT_ASSIGN:
printf("(%s ", token_kind_to_str(stmt->assign.op));
print_expr(stmt->assign.left);
if (stmt->assign.right) {
printf(" ");
print_expr(stmt->assign.right);
}
printf(")");
break;
case STMT_FOR:
printf("(for ");
print_stmt(stmt->for_stmt.init);
printf(" ");
print_expr(stmt->for_stmt.cond);
printf(" ");
print_stmt(stmt->for_stmt.next);
printf(" ");
print_stmt_block(stmt->for_stmt.block);
printf(")");
break;
case STMT_DO:
printf("(do ");
print_stmt_block(stmt->while_stmt.block);
print_newline();
printf("(while ");
print_expr(stmt->while_stmt.cond);
printf(")");
printf(")");
break;
case STMT_WHILE:
printf("(while ");
print_expr(stmt->while_stmt.cond);
printf(" ");
print_stmt_block(stmt->while_stmt.block);
printf(")");
break;
case STMT_SWITCH:
printf("(switch ");
print_expr(stmt->switch_stmt.expr);
++print_indent;
for (int i=0; i<stmt->switch_stmt.num_cases; ++i) {
SwitchCase *sc = &stmt->switch_stmt.cases[i];
print_newline();
if (sc->is_default)
printf("(default");
else
printf("(case");
for (int j=0; j<sc->num_exprs; ++j) {
printf(" ");
print_expr(sc->exprs[j]);
}
printf(" ");
print_stmt_block(sc->block);
printf(")");
}
--print_indent;
printf(")");
break;
default:
assert(0);
break;
}
}
void print_typespec(Typespec *typespec) {
if (typespec == NULL) {
printf("NULL");
return;
}
switch (typespec->kind) {
case TYPESPEC_NAME:
printf("%s", typespec->name);
break;
case TYPESPEC_POINTER:
printf("(ptr ");
print_typespec(typespec->ptr.base);
printf(")");
break;
case TYPESPEC_ARRAY:
printf("(array ");
print_expr(typespec->array.num_items);
printf(" ");
print_typespec(typespec->array.base);
printf(")");
break;
case TYPESPEC_FUNC:
printf("(func (");
for (int i=0; i<typespec->func.num_params; ++i) {
if (i > 0) printf(" ");
print_typespec(typespec->func.params[i]);
}
printf(")");
if (typespec->func.ret) {
printf(" ");
print_typespec(typespec->func.ret);
}
break;
default:
assert(0);
break;
}
}
void print_decl(Decl *decl) {
switch (decl->kind) {
case DECL_CONST:
printf("(const %s ", decl->name);
print_expr(decl->const_decl.expr);
printf(")");
break;
case DECL_VAR:
printf("(var %s ", decl->name);
print_typespec(decl->var.typespec);
printf(" ");
print_expr(decl->var.expr);
printf(")");
break;
case DECL_TYPEDEF:
printf("(typedef %s ", decl->name);
print_typespec(decl->typedef_decl.typespec);
printf(")");
break;
case DECL_FUNC:
printf("(func %s (", decl->name);
for (int i=0; i<decl->func.num_params; ++i) {
FuncParam p = decl->func.params[i];
if (i > 0) printf(" ");
printf("%s ", p.name);
print_typespec(p.typespec);
}
if (decl->func.is_variadic)
printf(" ...");
printf(")");
if (decl->func.ret_typespec) {
printf(" ");
print_typespec(decl->func.ret_typespec);
}
printf(" ");
print_stmt_block(decl->func.block);
printf(")"); // close func
break;
case DECL_UNION:
case DECL_STRUCT:
if (decl->kind == DECL_STRUCT) {
printf("(struct %s ", decl->name);
} else {
assert(decl->kind == DECL_UNION);
printf("(union %s ", decl->name);
}
++print_indent;
for (int i=0; i<decl->aggregate.num_fields; ++i) {
print_newline();
AggregateField f = decl->aggregate.fields[i];
printf("(%s ", f.name);
print_typespec(f.typespec);
printf(")");
}
--print_indent;
printf(")");
break;
case DECL_ENUM:
printf("(enum %s ", decl->name);
++print_indent;
for (int i=0; i<decl->enum_decl.num_items; ++i) {
print_newline();
EnumItem item = decl->enum_decl.items[i];
printf("(%s ", item.name);
print_expr(item.expr);
printf(")");
}
--print_indent;
printf(")");
break;
default:
assert(0 && "Unknown decl kind");
break;
}
}
#undef INDENT_WIDTH