-
Notifications
You must be signed in to change notification settings - Fork 0
/
gencodeAST.cpp
320 lines (309 loc) · 11.5 KB
/
gencodeAST.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
#include "definition.h"
string cppDataType(string s) {
if (s == "int") return "int";
if (s == "float") return "float";
if (s == "str") return "string";
if (s == "bool") return "bool";
if (s == "long") return "long";
if (s == "None") return "void";
if (s == "True") return "true";
if (s == "False") return "false";
// else: str const(int/float/long)
return s;
}
string cppDataType(NestDataType ds) {
string s = "";
for (int i = 0; i < ds.size(); i++) {
UnaryDataType d = ds.datatype_list[i];
if (d.name == "List") {
s += "vector<";
}
else if (i < ds.size() - 1) {
s += "map<" + cppDataType(d.name) + ",";
}
else {
s += cppDataType(d.name);
}
}
for (int i = 0; i < ds.size() - 1; i++) {
s += ">";
}
return s;
}
string cppOneOp(string s) {
if (s == "+") return "+";
if (s == "-") return "-";
if (s == "not") return "!";
return s;
}
string cppTwoOp(string s) {
if (s == ">") return ">";
if (s == ">=") return ">=";
if (s == "<") return "<";
if (s == "<=") return "<=";
if (s == "==") return "==";
if (s == "!=") return "!=";
if (s == "and") return "&&";
if (s == "or") return "||";
if (s == "+") return "+";
if (s == "-") return "-";
if (s == "*") return "*";
if (s == "/") return "/";
if (s == "%") return "%";
return s;
}
int temp_cnt = 0;
string new_temp() {
return "temp" + to_string(temp_cnt++);
}
string getLVal(vector<string> &pre_lines, ASTNode *root, string prefix, int DEBUG);
string genExpCode(vector<string> &pre_lines, ASTNode *root, string prefix, int DEBUG);
string getLVal(vector<string> &pre_lines, ASTNode *root, string prefix, int DEBUG) {
// [self] [var/attr {Index}] {FuncCall / attr{Index}}
ASTNode *p = root->first_child;
bool needDot = false;
string s = "";
if (p->s == "self") {
s += "this->";
p = p->next;
}
if (p->type == "var" || p->type == "attr") {
s += p->s;
p = p->next;
needDot = true;
}
while (p != nullptr && p->type == "Index") {
s += "[" + genExpCode(pre_lines, p->first_child, prefix, DEBUG) + "]";
p = p->next;
}
while (p != nullptr) {
if (needDot) s += ".";
if (p != nullptr && p->type == "FuncCall") {
s += p->s + "(";
// FuncRParams: {Exp}
ASTNode* p2 = p->first_child;
while (p2 != nullptr && p2->type == "FuncRParam") {
s += genExpCode(pre_lines, p2->first_child, prefix, DEBUG);
p2 = p2->next;
if (p2 != nullptr && p2->type == "FuncRParam") s += ", ";
}
s += ")";
p = p->next;
}
else if (p != nullptr && p->type == "attr") {
s += "." + p->s;
p = p->next;
while (p != nullptr && p->type == "Index") {
s += "[" + genExpCode(pre_lines, p->first_child, prefix, DEBUG) + "]";
p = p->next;
}
}
}
return s;
}
string genExpCode(vector<string> &pre_lines, ASTNode *root, string prefix, int DEBUG) {
if (root == nullptr) return "";
if (root->type == "LVal") {
return getLVal(pre_lines, root, prefix, DEBUG);
}
else if (root->type == "TwoOp") {
string t1 = genExpCode(pre_lines, root->first_child, prefix, DEBUG);
string t2 = genExpCode(pre_lines, root->first_child->next, prefix, DEBUG);
string t = new_temp();
string datatype = cppDataType(root->datatype.datatype_list[0].name);
string op = cppTwoOp(root->s);
pre_lines.push_back(prefix + datatype + " " + t + " = (" + t1 + " " + op + " " + t2 + ");");
return t;
}
else if (root->type == "OneOp") {
string t1 = genExpCode(pre_lines, root->first_child, prefix, DEBUG);
string t = new_temp();
string datatype = cppDataType(root->datatype.datatype_list[0].name);
string op = cppOneOp(root->s);
pre_lines.push_back(prefix + datatype + " " + t + " = (" + op + t1 + ");");
return t;
}
else if (root->type == "const") {
return cppDataType(root->s);
}
return "";
}
string getInitVal(vector<string> &pre_lines, ASTNode *root, string prefix, int DEBUG) {
string s;
ASTNODE* p = root->first_child;
if (root->type == "ListInitVal") {
s += "{";
while (p != nullptr) {
s += getInitVal(pre_lines, p, prefix, DEBUG);
p = p->next;
if (p != nullptr) s += ", ";
}
s += "}";
}
else if (root->type == "DictInitVal") {
s += "{";
while (p != nullptr) {
string key = genExpCode(pre_lines, p->first_child, prefix, DEBUG);
string value = getInitVal(pre_lines, p->first_child->next, prefix, DEBUG);
s += "{" + key + ", " + value + "}";
p = p->next;
if (p != nullptr) s += ", ";
}
s += "}";
}
else {
return genExpCode(pre_lines, root, prefix, DEBUG);
}
return s;
}
void genASTCppCode(ASTNode *root, string type, vector<string> &output_lines, string prefix, int DEBUG){
if (root == nullptr) return;
ASTNode* p = root->first_child;
if (DEBUG) cout << "[" << type << "]" << root->s << endl;
if (type == "CompUnit") {
cout << "Generating C++ code from AST..." << endl;
output_lines.push_back("#include <iostream>");
output_lines.push_back("#include <map>");
output_lines.push_back("#include <vector>");
output_lines.push_back("using namespace std;");
while (p != nullptr) {
// {ClassDef} {FuncDef}
if (p->type == "template") {
// 占位用的泛型模板
p = p->next;
continue;
}
genASTCppCode(p, p->type, output_lines, prefix, DEBUG);
p = p->next;
}
}
else if (type == "ClassDef") {
output_lines.push_back("class " + root->s);
output_lines.push_back("{");
output_lines.push_back(" public:");
while (p != nullptr) {
// {ClassAttrDef}, {ClassInitDef}, {ClassFuncDef}
genASTCppCode(p, p->type, output_lines, prefix + " ", DEBUG);
p = p->next;
}
output_lines.push_back("};");
}
else if (type == "ClassAttrDef") {
// {ClassAttrDef}, {ClassInitDef}, {ClassFuncDef}
string datatype = cppDataType(root->datatype);
string varname = root->s;
output_lines.push_back(prefix + datatype + " " + varname + ";");
}
else if (type == "ClassInitDef") {
string class_name = root->datatype.datatype_list[0].name;
string s = prefix + class_name + "(";
while (p != nullptr && p->type == "FuncFParam") {
string datatype = cppDataType(p->datatype);
string varname = p->s;
s += datatype + " " + varname;
p = p->next;
if (p != nullptr && p->type == "FuncFParam")
s += ", ";
}
output_lines.push_back(s + ")");
if (p != nullptr && p->type == "Block")
genASTCppCode(p, p->type, output_lines, prefix, DEBUG);
}
else if (type == "ClassFuncDef" || type == "FuncDef") {
// {FuncFParam}, Block
string func_name = root->s;
string ret = cppDataType(root->datatype);
string s = prefix + ret + " " + func_name + "(";
while (p != nullptr && p->type == "FuncFParam") {
string datatype = cppDataType(p->datatype);
string varname = p->s;
s += datatype + " " + varname;
p = p->next;
if (p != nullptr && p->type == "FuncFParam")
s +=", ";
}
output_lines.push_back(s + ")");
if (p != nullptr && p->type == "Block")
genASTCppCode(p, p->type, output_lines, prefix, DEBUG);
}
else if (type == "Block") {
output_lines.push_back(prefix + "{");
while (p != nullptr) {
// {Decl}, {Stmt}
genASTCppCode(p, p->type, output_lines, prefix + " ", DEBUG);
p = p->next;
}
output_lines.push_back(prefix + "}");
}
else if (type == "Decl") {
if (root->s == "=") {
//变量var,赋值InitVal (ListInitVal / DictInitVal / Exp)
string datatype = cppDataType(p->datatype);
string varname = p->s;
string initval = getInitVal(output_lines, p->next, prefix, DEBUG);
output_lines.push_back(prefix + datatype + " " + varname + " = " + initval + ";");
}
else {
string datatype = cppDataType(root->datatype);
string varname = root->s;
output_lines.push_back(prefix + datatype + " " + varname + ";");
}
}
else if (type == "Stmt") {
if (root->s == "=") {
// LVal, Exp
string lval = getLVal(output_lines, p, prefix, DEBUG);
string exp = genExpCode(output_lines, p->next, prefix, DEBUG);
output_lines.push_back(prefix + lval + " = " + exp + ";");
}
else if (root->s == "append") {
string lval = getLVal(output_lines, p, prefix, DEBUG);
string exp = genExpCode(output_lines, p->next, prefix, DEBUG);
output_lines.push_back(prefix + lval + ".push_back(" + exp + ");");
}
else if (root->s == "if") {
// Exp, Block[, Block]
string exp = genExpCode(output_lines, p, prefix, DEBUG);
output_lines.push_back(prefix + "if (" + exp + ")");
genASTCppCode(p->next, p->next->type, output_lines, prefix, DEBUG);
if (p->next->next != nullptr) {
output_lines.push_back(prefix + "else");
genASTCppCode(p->next->next, p->next->next->type, output_lines, prefix, DEBUG);
}
}
else if (root->s == "while") {
// Exp, Block
vector<string> condition_lines;
string exp = genExpCode(condition_lines, p, prefix, DEBUG);
output_lines.insert(output_lines.end(), condition_lines.begin(), condition_lines.end());
output_lines.push_back(prefix + "while (" + exp + ")");
vector<string> block_lines;
genASTCppCode(p->next, p->next->type, block_lines, prefix, DEBUG);
// 每次循环后重新计算condition
condition_lines.push_back(prefix + " if (!" + exp + ") break;");
block_lines.insert(block_lines.begin() + 1, condition_lines.begin(), condition_lines.end());
output_lines.insert(output_lines.end(), block_lines.begin(), block_lines.end());
}
else if (root->s == "break" || root->s == "continue") {
output_lines.push_back(prefix + root->s + ";");
}
else if (root->s == "return") {
string exp = genExpCode(output_lines, p, prefix, DEBUG);
output_lines.push_back(prefix + "return " + exp + ";");
}
else if (root->s == "print") {
string s = "cout";
while (p != nullptr) {
string exp = genExpCode(output_lines, p, prefix, DEBUG);
s += " << " + exp;
p = p->next;
}
output_lines.push_back(prefix + s + " << endl;");
}
else { // Exp
string exp = genExpCode(output_lines, p, prefix, DEBUG);
output_lines.push_back(prefix + exp + ";");
}
}
if (DEBUG) cout << "[" << type << "] end" << endl;
}