-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.cpp
222 lines (186 loc) · 3.79 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
#include "ast.h"
//之后的几个函数都是重写accept,传入对应结点自身
void ProgramNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void VarDefineNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void IdentifierNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void CustomFuncNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void ReturnTypeNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void ReturnNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void FuncBodyNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void CondStatementNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void ElseStatementNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void StatementSetNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void LoopStatementNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void ReadStatementNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void WriteStatementNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void AssignStatementNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void FactorNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void Operator::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void Expression::accept(Visitor& visitor)
{
visitor.visit(*this);
}
void AssistNode::accept(Visitor& visitor)
{
visitor.visit(*this);
}
//之后的几个函数都是重写show,展示对应结点的信息
void ProgramNode::Show()
{
cout << "程序根节点";
}
void VarDefineNode::Show()
{
cout << "变量声明";
}
void IdentifierNode::Show()
{
string out = (type == IdentifierNode::VAR) ? "变量" : "函数";
cout << out << "标识符: " << idName;
}
void FactorNode::Show()
{
if (facType == INT)
{
cout << "整数" << value;
}
else if (facType == VAR)
{
cout << "变量: " << id;
}
else if (facType == FUNCTION)
{
cout << "函数调用: " << id;
}
}
void CustomFuncNode::Show()
{
cout << "自定义函数";
}
void ReturnTypeNode::Show()
{
string out = (retType == tree_node::ReturnType::_VOID) ? "VOID" : "INT";
cout << "返回值类型: " << out;
}
void FuncBodyNode::Show()
{
string out = isMain ? "主" : "";
cout <<out<< "函数体";
}
void CondStatementNode::Show()
{
cout << "条件语句";
}
void ElseStatementNode::Show()
{
cout << "ELSE语句";
}
void LoopStatementNode::Show()
{
cout << "循环语句";
}
void ReadStatementNode::Show()
{
cout << "读语句,存入: "<<id;
}
void WriteStatementNode::Show()
{
cout << "写语句";
}
void AssignStatementNode::Show()
{
cout << "赋值语句,左值为: "<<id;
}
void StatementSetNode::Show()
{
cout << "语句序列";
}
void ReturnNode::Show()
{
cout << "返回语句";
}
void Operator::Show()
{
if (opType == Operator::ADD)
{
cout << " \"+\" 优先级: "<< priority;
}
else if (opType == Operator::SUBTRACT)
{
cout << " \"-\" 优先级: " << priority;
}
else if (opType == Operator::MULTIPLY)
{
cout << " \"*\" 优先级: " << priority;
}
else if (opType == Operator::DIVIDE)
{
cout << " \"/\" 优先级: " << priority;
}
}
void Expression::Show()
{
for (int i=0;i<expList.size();i++)
{
if (expList[i]->Get_type() == tree_node::OPERATOR)
{
auto opr = dynamic_pointer_cast<Operator>(expList[i]);
opr->Show();
cout << " ";
}
else
{
auto fac = dynamic_pointer_cast<FactorNode>(expList[i]);
fac->Show();
cout << " ";
}
}
}