-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cpp
335 lines (304 loc) · 8.31 KB
/
ast.cpp
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
#include "def.h"
#define YYSTYPE int //此行是为了包含parser.tab.hpp不引起错误而加,可以在后面使用相关常量
#include "parser.tab.hpp"
int AST::MaxVarSize=0;
int AST::MaxTempVarOffset=0;
//SymbolStackDef AST SymbolStack1={{}};
void space(int indent)
{
for(int i=0;i<indent;i++) cout<<" ";
}
map <int,string> SymbolMap={{T_CHAR,"char"},{T_INT,"int"},{T_FLOAT,"float"},{T_VOID,"void"},{GT,">"},{GE,">="},{LT,"<"},
{LE,"<="},{EQ,"=="},{NE,"!="},{PLUS,"+"},{MINUS,"-"},{UPLUS,"*"},{UMINUS,"/"},{MOD, "%"},{ASSIGN,"="},
{AND,"&&"},{OR,"||"},{NOT,"!"},{DPLUS,"++N"},{DMINUS, "--N"},{PLUSD,"N++"},{MINUSD, "N--"}};
void ProgAST::DisplayAST(int indent)
{ //依次显示向量ExtDefs中的各个外部定义(外部变量定义和函数定义)
for(auto a:ExtDefs)
a->DisplayAST(0);
}
void ExtVarDefAST::DisplayAST(int indent)
{ //显示外部变量定义
cout<<"外部变量定义:"<<endl;
cout<<" 类 型 名: ";
Type->DisplayAST(indent);
cout<<endl<<" 变量列表: "<<endl;
for(auto a:ExtVars)
{
a->DisplayAST(15);
}
}
void BasicTypeAST::DisplayAST(int indent)
{ //显示基本类型名符号串
cout.width(6);
cout.setf(ios::left);
cout<<SymbolMap[Type]<<endl;
}
void VarDecAST::DisplayAST(int indent)
{ //显示外部变量定义中的单个变量
space(indent);
cout<<Name; //显示变量名
for(auto a:Dims) //如果是数组,依次显示各维大小
cout<<"["<<a<<"]";
if (Exp) //有初始化表达式
{
cout<<"= ";
if (typeid(*Exp)==typeid(BinaryExprAST))
{
cout<<SymbolMap[((BinaryExprAST *)Exp)->Op]<<endl;
((BinaryExprAST *)Exp)->LeftExp->DisplayAST(indent+Name.length()+5);
cout<<endl;
((BinaryExprAST *)Exp)->RightExp->DisplayAST(indent+Name.length()+5);
}
else Exp->DisplayAST(0);
}
cout<<endl;
}
void FuncDefAST::DisplayAST(int indent)
{ //显示函数定义
cout<<"函数定义:"<<endl;
cout<<" 返回类型:"; //显示函数返回值类型
Type->DisplayAST(indent);
cout<<endl<<" 函 数 名:"<<Name<<endl; //显示函数名
cout<<" 形 参 表:"; //显示形参
if (!Params.size())
cout<<"无"<<endl;
else
{
cout<<endl;
for(auto a:Params)
a->DisplayAST(14);
}
if(Body) {
cout<<" 函 数 体: "<<endl;//显示函数返回值类型
Body->DisplayAST(0);
}
cout<<endl;
}
void ParamAST::DisplayAST(int indent)
{ //显示形式参数
space(indent);
Type->DisplayAST(indent);
ParamName->DisplayAST(indent);
}
/***********各种语句结点***************/
void CompStmAST::DisplayAST(int indent)
{ //显示复合语句
space(indent);
if (indent) cout<<"复合语句:"<<endl;
else indent=8; //显示函数体
if (Decls.size())
{
space(indent+2);
cout<<"说明部分:"<<endl;
for(auto a:Decls)
a->DisplayAST(indent+4);
}
if (Stms.size())
{
space(indent+2);
cout<<"语句部分:"<<endl;
for(auto a:Stms)
a->DisplayAST(indent+4);
}
}
void ExprStmAST::DisplayAST(int indent)
{ //显示表达式语句
if (typeid(*Exp)==typeid(FuncCallAST))
Exp->DisplayAST(indent); //对形式为函数调用后接分号的函数调用语句
else
{
space(indent);
cout<<"表达式语句:"<<endl;
Exp->DisplayAST(indent+4);
cout<<endl;
}
}
void IfStmAST::DisplayAST(int indent)
{ //显示条件语句1
space(indent);
cout<<"if语句:"<<endl;
space(indent+2);
cout<<"条件:"<<endl;
Cond->DisplayAST(indent+8);
space(indent+2);
cout<<"if子句:"<<endl;
ThenStm->DisplayAST(indent+8);
}
void IfElseStmAST::DisplayAST(int indent)
{ //显示条件语句2
space(indent);
cout<<"if语句:"<<endl;
space(indent+2);
cout<<"条件:"<<endl;
Cond->DisplayAST(indent+8);
cout<<endl;
space(indent+2);
cout<<"if子句:"<<endl;
ThenStm->DisplayAST(indent+8);
space(indent+2);
cout<<"else子句:"<<endl;
ElseStm->DisplayAST(indent+8);
}
void WhileStmAST::DisplayAST(int indent)
{ //显示while循环语句
space(indent);
cout<<"while语句:"<<endl;
space(indent+2);
cout<<"循环条件:"<<endl;
Cond->DisplayAST(indent+8);
space(indent+2);
cout<<"循环体:"<<endl;
Body->DisplayAST(indent+8);
}
void ForStmAST::DisplayAST(int indent)
{ //显示for循环语句
space(indent);
cout<<"for语句:"<<endl;
space(indent+2);
cout<<"单次表达式:"<<endl;
SinExp->DisplayAST(indent+8);
space(indent+2);
cout<<"循环条件:"<<endl;
Cond->DisplayAST(indent+8);
space(indent+2);
cout<<"末尾循环体:"<<endl;
EndExp->DisplayAST(indent+8);
space(indent+2);
cout<<"循环体:"<<endl;
Body->DisplayAST(indent+8);
}
void CaseStmAST::DisplayAST(int indent)
{ //显示case语句
space(indent);
cout<<"常量Key:"<<endl;
Cond->DisplayAST(indent+4);
if (Body.size())
{
space(indent);
cout<<"语句部分:"<<endl;
for(auto a:Body)
a->DisplayAST(indent+4);
}
}
void SwitchStmAST::DisplayAST(int indent)
{ //显示case语句
space(indent);
cout<<"表达式:"<<endl;
Exp->DisplayAST(indent+4);
if (Cases.size())
{
space(indent);
cout<<"Case部分:"<<endl;
for(auto a:Cases)
a->DisplayAST(indent+4);
}
if (containDefault && Default.size())
{
space(indent);
cout<<"Default部分:"<<endl;
for(auto a:Default)
a->DisplayAST(indent+4);
}
}
void BreakStmAST::DisplayAST(int indent)
{
space(indent);
cout<<"break语句"<<endl;
}
void ContinueStmAST::DisplayAST(int indent)
{
space(indent);
cout<<"continue语句"<<endl;
}
void ReturnStmAST::DisplayAST(int indent)
{ //显示返回语句
space(indent);
if (Exp)
{
cout<<"返回表达式:"<<endl;
Exp->DisplayAST(indent+4);
}
else cout<<"返回语句"<<endl;
cout<<endl;
}
void DefAST::DisplayAST(int indent)
{ //显示局部变量
space(indent);
cout<<"类型:";
Type->DisplayAST(0);
cout<<endl;
space(indent);
cout<<"变量列表: "<<endl;
for(auto a:LocVars) //如果是数组,依次显示各维大小
a->DisplayAST(indent+10);
cout<<endl;
}
/***********表达式结点***************/
void AssignAST::DisplayAST(int indent)
{ //显示赋值表达式
space(indent);
cout<<"赋值运算符:"<<SymbolMap[Op]<<endl;
space(indent+2); cout<<"左值表达式:"<<endl;
LeftValExp->DisplayAST(indent+16);
space(indent+2); cout<<"右值表达式:"<<endl;
RightValExp->DisplayAST(indent+16);
}
void BinaryExprAST::DisplayAST(int indent)
{ //显示二元运算表达式
space(indent);
cout<<SymbolMap[Op]<<endl;
LeftExp->DisplayAST(indent+4);
cout<<endl;
RightExp->DisplayAST(indent+4);
cout<<endl;
}
void ConstAST::DisplayAST(int indent)
{ //显示常量
space(indent);
switch (Type) //显示常量值
{
case T_CHAR: cout<<ConstVal.constCHAR;break;
case T_INT: cout<<ConstVal.constINT;break;
case T_FLOAT: cout<<ConstVal.constFLOAT;break;
}
cout<<endl;
}
void VarAST::DisplayAST(int indent)
{ //显示变量
space(indent);
cout<<Name<<endl; //显示外部变量名
if (index.size())
{
space(indent+10);
cout<<index.size()<<"个下标:\n"<<endl;
for(auto a:index)
{
a->DisplayAST(indent+16);
}
}
}
void FuncCallAST::DisplayAST(int indent)
{ //显示函数调用
space(indent);
cout<<"函数调用: ";
// space(indent+4);
cout<<"函数名:"<<Name;
if (!Params.size())
{
cout<<" <无实参表达式>"<<endl;
return;
}
cout<<endl;
space(indent+10);
cout<<Params.size()<<"个实参表达式:\n"<<endl;
for(auto a:Params)
{
a->DisplayAST(indent+14);
}
}
void UnaryExprAST::DisplayAST(int indent)
{ //显示单目运算
space(indent);
cout<<"单目:"<<SymbolMap[Op]<<endl;
Exp->DisplayAST(indent+8);
}