-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast_pretty_printer.c
More file actions
257 lines (241 loc) · 5.29 KB
/
ast_pretty_printer.c
File metadata and controls
257 lines (241 loc) · 5.29 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "y.tab.h"
#include "symtab.h"
FILE* outfile;
char *filename;
void print_error(const char *msg, int line) {
fprintf(stderr, "%s(%i): %s\n", filename, line, msg);
exit(0);
}
void print_ident(int ident) {
int i;
for(i = 0; i < ident; i++) fprintf(outfile, " ");
}
void print_string(char* s) {
int i;
fprintf(outfile, "\"");
for(i = 0; i < strlen(s); i++)
switch(s[i]) {
case '\"': fprintf(outfile, "\\\""); break;
case '\\': fprintf(outfile, "\\\\"); break;
case '\t': fprintf(outfile, "\\t"); break;
case '\n': fprintf(outfile, "\\n"); break;
default: fprintf(outfile, "%c", s[i]);
}
fprintf(outfile, "\"");
}
void print_declrlist(int ident, DeclrListNode* dln)
{
if(dln) {
print_declr(ident, dln->declr);
fprintf(outfile, "\n");
print_declrlist(ident, dln->next);
}
}
void print_declr(int ident, Declr* d) {
print_ident(ident);
print_type(d->type);
switch(d->tag) {
case DECLR_VAR:
{
fprintf(outfile, " ");
fprintf(outfile, "%s",d->u.name);
fprintf(outfile, ";");
break;
}
case DECLR_FUNC:
{
fprintf(outfile, " %s(", d->u.func.name);
print_paramlist(d->u.func.params);
fprintf(outfile, ")");
if(d->u.func.block) {
fprintf(outfile, "\n");
print_block(ident, d->u.func.block);
} else fprintf(outfile, ";");
break;
}
}
}
void print_type(Type* t) {
switch(t->type) {
case TK_TVOID: fprintf(outfile, "void"); break;
case TK_TINT: fprintf(outfile, "int"); break;
case TK_TFLOAT: fprintf(outfile, "float"); break;
case TK_TCHAR: fprintf(outfile, "char"); break;
}
if(t->dimensions) {
IntListNode* iln = t->sizes;
while(iln) {
if(iln->n)
fprintf(outfile, "[%i]", iln->n);
else
fprintf(outfile, "[]");
iln = iln->next;
}
}
}
void print_strlist(StrListNode* sln) {
fprintf(outfile, sln->name);
if(sln->next) {
fprintf(outfile, ", ");
print_strlist(sln->next);
}
}
void print_paramlist(DeclrListNode* pln) {
if(!pln) return;
print_param(pln->declr);
if(pln->next) {
fprintf(outfile, ", ");
print_paramlist(pln->next);
}
}
void print_block(int ident, Block* b) {
if(!b) { fprintf(outfile, ";\n"); return; }
print_ident(ident);
fprintf(outfile, "{\n");
print_declrlist(ident + 2, b->declrs);
print_commlist(ident + 2, b->comms);
print_ident(ident);
fprintf(outfile, "}\n");
}
void print_param(Declr* p) {
if(!p)
fprintf(outfile, "...");
else {
print_type(p->type);
fprintf(outfile, " %s", p->u.name);
}
}
void print_commlist(int ident, CommListNode* cln) {
if(cln) {
print_command(ident, cln->comm);
fprintf(outfile, "\n");
print_commlist(ident, cln->next);
}
}
void print_command(int ident, Command* c) {
print_ident(ident);
switch(c->tag) {
case COMMAND_IF:
{
fprintf(outfile, "if(");
print_exp(c->u.cif.exp);
fprintf(outfile, ") ");
print_command(ident, c->u.cif.comm);
if(c->u.cif.celse) {
fprintf(outfile, "else ");
print_command(ident, c->u.cif.celse);
}
break;
}
case COMMAND_WHILE:
{
fprintf(outfile, "while(");
print_exp(c->u.cwhile.exp);
fprintf(outfile, ") ");
print_command(ident, c->u.cwhile.comm);
break;
}
case COMMAND_ATTR:
{
print_var(c->u.attr.lvalue);
fprintf(outfile, " = ");
print_exp(c->u.attr.rvalue);
fprintf(outfile, ";\n");
break;
}
case COMMAND_RET:
{
fprintf(outfile, "return ");
if(c->u.ret)
print_exp(c->u.ret);
fprintf(outfile, ";\n");
break;
}
case COMMAND_FUNCALL:
{
print_exp(c->u.funcall);
fprintf(outfile, ";\n");
break;
}
case COMMAND_BLOCK:
{
print_block(ident, c->u.block);
break;
}
}
}
void print_var(Var* v) {
fprintf(outfile, "%s", v->name);
if(v->idxs) {
ExpListNode* idx = v->idxs;
while(idx) {
fprintf(outfile, "[");
print_exp(idx->exp);
fprintf(outfile, "]");
idx = idx->next;
}
}
}
void print_exp(Exp* e) {
switch(e->tag) {
case EXP_INT:
fprintf(outfile, "%i", e->u.ival);
break;
case EXP_FLOAT:
fprintf(outfile, "%f", e->u.fval);
break;
case EXP_STRING:
print_string(e->u.sval);
break;
case EXP_VAR:
print_var(e->u.var);
break;
case EXP_BINOP:
{
fprintf(outfile, "(");
print_exp(e->u.binop.e1);
switch(e->u.binop.op) {
case TK_EQ: fprintf(outfile, "=="); break;
case TK_LEQ: fprintf(outfile, "<="); break;
case TK_GEQ: fprintf(outfile, ">="); break;
case TK_NEQ: fprintf(outfile, "!="); break;
case TK_AND: fprintf(outfile, "&&"); break;
case TK_OR: fprintf(outfile, "||"); break;
default: fprintf(outfile, "%c", e->u.binop.op);
}
print_exp(e->u.binop.e2);
fprintf(outfile, ")");
break;
}
case EXP_NEG:
{
fprintf(outfile, "-");
print_exp(e->u.exp);
break;
}
case EXP_LNEG:
{
fprintf(outfile, "!");
print_exp(e->u.exp);
break;
}
case EXP_FUNCALL:
{
fprintf(outfile, "%s(", e->u.funcall.name);
print_explist(e->u.funcall.expl);
fprintf(outfile, ")");
}
}
}
void print_explist(ExpListNode* eln) {
if(!eln) return;
print_exp(eln->exp);
if(eln->next) {
fprintf(outfile, ", ");
print_explist(eln->next);
}
}