-
Notifications
You must be signed in to change notification settings - Fork 0
/
astprinter.c
196 lines (182 loc) · 5.86 KB
/
astprinter.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
/*
* Copyright 2019 Yuxuan Xiao
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include "util.h"
#include "symbol.h"
#include "absyn.h"
#include "astprinter.h"
static void indent(FILE * out, int deep) {
for (int i = 0; i < deep; i++) {
fprintf(out, " ");
}
}
void printExp(FILE * out, A_exp exp, int deepth) {
if (!exp)
return;
indent(out, deepth + 1);
switch (exp->kind) {
case A_CONST:
fprintf(out, "constExp\n");
break;
case A_CALL:
fprintf(out, "callExp\n");
printExpList(out, exp->u.call.para, deepth + 1);
break;
case A_DOUBLE_EXP:
fprintf(out, "doubleExp\n");
printExp(out, exp->u.doublexp.left, deepth + 1);
printExp(out, exp->u.doublexp.right, deepth + 1);
break;
case A_SINGLE_EXP:
fprintf(out, "singleExp\n");
printExp(out, exp->u.singexp.exp, deepth + 1);
break;
}
}
void printExpList(FILE * out, A_expList root, int deepth) {
if (!root)
return;
indent(out, deepth + 1);
fprintf(out, "expList\n");
while (root && root->head) {
printExp(out, root->head, deepth + 1);
root = root->tail;
}
}
void printVar(FILE * out, A_var var, int deepth) {
if (!var)
return;
indent(out, deepth + 1);
switch (var->kind) {
case A_SYMBOL_VAR:
fprintf(out, "symbolVar\n");
break;
case A_ARRAY_VAR:
fprintf(out, "arrayVar\n");
printExpList(out, var->u.arrayvar.exp, deepth + 1);
break;
case A_STRUCT_VAR:
fprintf(out, "structVar\n");
break;
}
}
void printTypeDec(FILE * out, A_tyDec dec, int deepth) {
if (!dec)
return;
indent(out, deepth + 1);
switch (dec->kind) {
case A_VAR_DEC:
fprintf(out, "varDec\n");
break;
case A_ARRAY_DEC:
fprintf(out, "arrayDec\n");
printExpList(out, dec->u.array.exp, deepth + 1);
break;
case A_NULL_DEC:
break;
}
}
void printTypeDecList(FILE * out, A_tyDecList root, int deepth) {
if (!root)
return;
indent(out, deepth + 1);
fprintf(out, "typeDecList\n");
while (root && root->head) {
printTypeDec(out, root->head, deepth + 1);
root = root->tail;
}
}
void printStm(FILE * out, A_stm stm, int deepth) {
if (!stm)
return;
indent(out, deepth + 1);
switch (stm->kind) {
case A_ASSIGN_STM:
fprintf(out, "assignStm\n");
printVar(out, stm->u.assign.symbol, deepth + 1);
printExp(out, stm->u.assign.exp, deepth + 1);
break;
case A_DEC_STM:
fprintf(out, "decStm\n");
printTypeDec(out, stm->u.dec, deepth + 1);
break;
case A_IF_STM:
fprintf(out, "ifStm\n");
printExp(out, stm->u.iff.test, deepth + 1);
printStmList(out, stm->u.iff.iff, deepth + 1);
printStmList(out, stm->u.iff.elsee, deepth + 1);
break;
case A_WHILE_STM:
fprintf(out, "whileStm\n");
printExp(out, stm->u.whilee.test, deepth + 1);
printStmList(out, stm->u.whilee.whilee, deepth + 1);
break;
case A_EXP_STM:
fprintf(out, "expStm\n");
printExp(out, stm->u.exp, deepth + 1);
break;
case A_BREAK_STM:
fprintf(out, "breakStm\n");
break;
case A_CONTINUE_STM:
fprintf(out, "continueStm\n");
break;
case A_RETURN_STM:
fprintf(out, "returnStm\n");
printExp(out, stm->u.returnn, deepth + 1);
break;
}
}
void printStmList(FILE * out, A_stmList root, int deepth) {
if (!root)
return;
indent(out, deepth + 1);
fprintf(out, "stmList\n");
while (root && root->head) {
printStm(out, root->head, deepth + 1);
root = root->tail;
}
}
void printGlobalDec(FILE * out, A_globalDec dec, int deepth) {
if (!dec)
return;
indent(out, deepth + 1);
switch (dec->kind) {
case A_FUN:
fprintf(out, "globalFunDec\n");
printTypeDecList(out, dec->u.fun.para, deepth + 1);
printStmList(out, dec->u.fun.body, deepth + 1);
break;
case A_STRUCT:
fprintf(out, "globalStructDec\n");
printTypeDecList(out, dec->u.struc.declist, deepth + 1);
break;
case A_GLOBAL_VAR:
fprintf(out, "globalVarDec\n");
break;
}
}
void printAST(FILE * out, A_globalDecList root) {
if (!root)
return;
fprintf(out, "globalDecList\n");
while (root && root->head) {
printGlobalDec(out, root->head, 0);
root = root->tail;
}
}