-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.cpp
More file actions
335 lines (321 loc) · 10.1 KB
/
utils.cpp
File metadata and controls
335 lines (321 loc) · 10.1 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
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
/*
* @Copyright: minic
* @Author: David.Huangjunlang
* @Description: 工具集的函数实现,包括新节点生成,以及打印语法树
* @LastEditors: David.Huangjunlang
* @LastEditTime: 2020-04-30 10:12:27
* @FilePath: /minic/utils.cpp
*/
#include "utils.h"
// 链接程序根节点
extern node *programNode;
// 链接语法树输出文件
extern FILE *result;
/**
* @description: 新建一个 statement 类型的 node 并返回
* @param {StmtKind}
* @return: node*
*/
node *newStmtNode(StmtKind kind)
{
node *t = new node;
for (int i = 0; i < MAXCHILDREN; i++)
{
t->nodeChild[i] = nullptr;
}
t->nodekind = StmtK;
t->kind.stmt = kind;
t->next = nullptr;
t->isArray = 0;
t->isGlobal = 0;
t->isParameter = 0;
t->local_size = 0;
t->param_size = 0;
t->val = 0;
return t;
}
/**
* @description: 新建一个 expression 类型的 node 并返回
* @param {ExpKind}
* @return: node*
* @author: David.Huangjunlang
*/
node *newExpNode(ExpKind kind)
{
node *t = new node;
for (int i = 0; i < MAXCHILDREN; i++)
{
t->nodeChild[i] = nullptr;
}
t->nodekind = ExpK;
t->kind.exp = kind;
t->next = nullptr;
t->isArray = 0;
t->isGlobal = 0;
t->isParameter = 0;
t->local_size = 0;
t->param_size = 0;
t->val = 0;
return t;
}
/**
* @description: 在 nodeList 链表的末尾添加一个 statement 类型的 node
* @param {node*, node*}
* @return: void
* @author: David.Huangjunlang
*/
void addNode(node *list, node *stmt)
{
node *temp = list;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = stmt;
}
/**
* @description: 从程序根节点遍历语法树,并打印到文件中
* @param {void}
* @return: void
* @author: David.Huangjunlang
*/
void printTree()
{
node *temp = programNode->nodeChild[0];
if (temp == nullptr)
{
return;
}
while (temp->next != nullptr)
{
temp = temp->next;
printNode(temp, 0);
}
}
/**
* @description: 打印节点信息到文件中, level 参数用于设定输出缩进格式
* @param {node*, int}
* @return: void
* @author: David.Huangjunlang
*/
void printNode(node *t, int level)
{
std::string preWhiteSpace = "";
for (int i = 0; i < level; i++)
{
preWhiteSpace += "\t";
}
if (t->nodekind == StmtK)
{
switch (t->kind.stmt)
{
case FunK:
{
fprintf(result, "fun-declaration: \n");
fprintf(result, "\ttype: int\n");
fprintf(result, "\tname: %s\n", t->name.c_str());
if (t->nodeChild[0] != nullptr)
{
printNode(t->nodeChild[0], level + 1);
}
else
{
fprintf(result, "\tparams: void\n");
}
printNode(t->nodeChild[1], level + 1);
break;
}
case ParamlK:
{
if (t != nullptr)
{
fprintf(result, "\tparams: \n");
node *child = t;
while (child->next != nullptr)
{
child = child->next;
printNode(child, 2);
}
}
else
{
fprintf(result, "\tparams: void\n");
}
break;
}
case CompK:
{
fprintf(result, "\tcompound: \n");
if (t->nodeChild[0] != nullptr)
{
fprintf(result, "\t\tlocal-declarations: \n");
printNode(t->nodeChild[0], 3);
}
else
{
fprintf(result, "\t\tlocal-declarations: null\n");
}
if (t->nodeChild[1] != nullptr)
{
fprintf(result, "\t\tstatement-list: \n");
printNode(t->nodeChild[1], 3);
}
else
{
fprintf(result, "\t\tstatement-list: null\n");
}
break;
}
case LocdeclK:
case StmtlK:
{
node *child = t;
while (child->next != nullptr)
{
child = child->next;
printNode(child, level);
}
break;
}
case ExpressionK:
{
if (t->nodeChild[0] != nullptr)
{
printNode(t->nodeChild[0], level);
}
else
{
fprintf(result, "%sexpression-stmt: null\n", preWhiteSpace.c_str());
}
break;
}
case SelectK:
{
fprintf(result, "%sselection-stmt:\n%s\texpression: \n", preWhiteSpace.c_str(), preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
fprintf(result, "%s\tstatement: \n", preWhiteSpace.c_str());
printNode(t->nodeChild[1], level + 2);
if (t->nodeChild[2] != nullptr)
{
fprintf(result, "%s\telse-statement: \n", preWhiteSpace.c_str());
printNode(t->nodeChild[2], level + 2);
}
break;
}
case DeclK:
{
fprintf(result, "program: \n");
break;
}
case IteraK:
{
fprintf(result, "%siteration-stmt: \n%s\texpression: \n", preWhiteSpace.c_str(), preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
fprintf(result, "%s\tstatement: \n", preWhiteSpace.c_str());
printNode(t->nodeChild[1], level + 2);
break;
}
case ReturnK:
{
if (t->nodeChild[0] != nullptr)
{
fprintf(result, "%sreturn-stmt: \n%s\texpression: \n", preWhiteSpace.c_str(), preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
}
else
{
fprintf(result, "%sreturn-stmt: \n%s\texpression: null\n", preWhiteSpace.c_str(), preWhiteSpace.c_str());
}
break;
}
case ArgsK:
{
node *child = t;
while (child->next != nullptr)
{
child = child->next;
printNode(child, level);
}
break;
}
case ArrayK:
fprintf(result, "%svarArray: \n%s\tname: %s\n%s\t\n", preWhiteSpace.c_str(),
preWhiteSpace.c_str(), t->name.c_str(), preWhiteSpace.c_str());
break;
default:
break;
}
}
else
{
switch (t->kind.exp)
{
case AssignK:
fprintf(result, "%sassignment: \n%s\tvarName: %s\n%s\texpression: \n", preWhiteSpace.c_str(), preWhiteSpace.c_str(), t->nodeChild[0]->name.c_str(), preWhiteSpace.c_str());
printNode(t->nodeChild[1], level + 1);
break;
case IdK:
fprintf(result, "%svar: \n%s\tname: %s\n", preWhiteSpace.c_str(), preWhiteSpace.c_str(), t->name.c_str());
break;
case VarK:
fprintf(result, "%svar: \n%s\tname: %s\n", preWhiteSpace.c_str(), preWhiteSpace.c_str(), t->name.c_str());
break;
// case IndexK:
// fprintf(result, "%sarrayIndex: \n%s\tname: %s\n%s\tindex: \n", preWhiteSpace.c_str(), preWhiteSpace.c_str(), t->name.c_str(), preWhiteSpace.c_str());
// printNode(t->nodeChild[0], level + 1);
// break;
// case ArrayptrK:
// fprintf(result, "%sarrayPtr: \n%s\tname: %s\n", preWhiteSpace.c_str(), preWhiteSpace.c_str(), t->name.c_str());
// break;
case ConstK:
fprintf(result, "%sconst: \n%s\tval: %d\n", preWhiteSpace.c_str(), preWhiteSpace.c_str(), t->val);
break;
case OpK:
fprintf(result, "%ssimple-expression: \n", preWhiteSpace.c_str());
if (t->nodeChild[1] == nullptr)
{
printNode(t->nodeChild[0], level + 1);
}
else
{
fprintf(result, "%s\tfirst:\n", preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
fprintf(result, "%s\top: %s\n", preWhiteSpace.c_str(), t->op.c_str());
fprintf(result, "%s\tsecond:\n", preWhiteSpace.c_str());
printNode(t->nodeChild[1], level + 2);
}
break;
/* case AddK:
fprintf(result, "%sadditive-expression: \n", preWhiteSpace.c_str());
fprintf(result, "%s\tfirst:\n", preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
fprintf(result, "%s\top: %s\n", preWhiteSpace.c_str(), t->op.c_str());
fprintf(result, "%s\tsecond:\n", preWhiteSpace.c_str());
printNode(t->nodeChild[1], level + 2);
break;
case MulK:
fprintf(result, "%sterm: \n", preWhiteSpace.c_str());
fprintf(result, "%s\tfirst:\n", preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
fprintf(result, "%s\top: %s\n", preWhiteSpace.c_str(), t->op.c_str());
fprintf(result, "%s\tsecond:\n", preWhiteSpace.c_str());
printNode(t->nodeChild[1], level + 2);
break;*/
case CallK:
fprintf(result, "%sfunctionCall: \n", preWhiteSpace.c_str());
fprintf(result, "%s\tname: %s\n", preWhiteSpace.c_str(), t->name.c_str());
if (t->nodeChild[0] != nullptr)
{
fprintf(result, "%s\targs: \n", preWhiteSpace.c_str());
printNode(t->nodeChild[0], level + 2);
}
else
{
fprintf(result, "%s\targs: void\n", preWhiteSpace.c_str());
}
break;
default:
break;
}
}
}