-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.c
186 lines (166 loc) · 2.72 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
#include <stdio.h>
#include <stdarg.h>
#include "ast.h"
static void print_decl(struct decl *);
static void print_stmt(struct stmt *);
static void print_expr(struct expr *);
static void print_type(struct type *);
static void print_param(struct param *);
static FILE *fout;
static void write(const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vfprintf(fout, fmt, argp);
fputc('\n', fout);
va_end(argp);
}
void ast_print(struct prog *prog, struct config *cfg)
{
fout = cfg->fout;
print_decl(prog->ast);
}
void print_decl(struct decl *d)
{
if (!d) {
return;
}
struct type *t = d->type;
print_type(t);
write("%s", d->name);
if (t->kind == TYPE_FUNCTION) {
write("(");
print_param(t->params);
write(")");
if (d->code) {
print_stmt(d->code);
} else {
write(";");
}
} else {
if (d->value) {
write("=");
print_expr(d->value);
}
write(";");
}
print_decl(d->next);
}
void print_stmt(struct stmt *s)
{
if (!s) {
return;
}
switch (s->kind) {
case STMT_DECL:
print_decl(s->decl);
break;
case STMT_EXPR:
print_expr(s->expr);
write(";");
break;
case STMT_IF_ELSE:
write("if\n(");
print_expr(s->expr);
write(")");
print_stmt(s->body);
if (s->ebody) {
write("else");
print_stmt(s->ebody);
}
break;
case STMT_WHILE:
write("while\n(");
print_expr(s->expr);
write(")");
print_stmt(s->body);
break;
case STMT_RETURN:
write("return");
print_expr(s->expr);
write(";");
break;
case STMT_BLOCK:
write("{");
print_stmt(s->body);
write("}");
break;
case STMT_PRINT:
write("print");
print_expr(s->expr);
write(";");
break;
}
print_stmt(s->next);
}
void print_expr(struct expr *e)
{
if (!e) {
return;
}
const char *op = expr_kind_to_s(e->kind);
print_expr(e->left);
switch (e->kind) {
case EXPR_CALL:
write("%s\n(", e->name);
print_expr(e->right); // special case, need to print right expr early here
write(")");
return;
case EXPR_ARG:
if (e->right) {
write(",");
}
break;
case EXPR_NAME:
case EXPR_CHAR:
case EXPR_STRING:
write("%s", e->name);
break;
case EXPR_INT:
write("%d", e->constant);
break;
case EXPR_BOOLEAN:
if (e->constant) {
write("true");
} else {
write("false");
}
break;
case EXPR_ASSIGN:
write("%s", e->name);
write("%s", op);
break;
default:
if (op) {
write("%s", op);
}
break;
}
print_expr(e->right);
}
void print_type(struct type *t)
{
if (!t) {
return;
}
switch (t->kind) {
case TYPE_FUNCTION:
print_type(t->rtype);
break;
default:
write("%s", type_kind_to_s(t->kind));
break;
}
}
void print_param(struct param *p)
{
if (!p) {
return;
}
print_type(p->type);
write("%s", p->name);
if (p->next) {
write(",");
print_param(p->next);
}
}