-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.hpp
475 lines (369 loc) · 9.74 KB
/
ast.hpp
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#pragma once
#include "scope.hpp"
#include "code.hpp"
#include <string>
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
/*
*
* Nodes and Helpers
* */
struct ASTNode{
string text;
int memo = -1;
ASTNode(){}
ASTNode(string s) : text(s){}
virtual string get_text() const {
return text;
}
virtual void print_node() const {
cout << "[" << this->get_text();
this->print_children();
cout << "]";
}
virtual void print_children() const {}
virtual void check_and_generate(Code &, ScopeStack &) {}
virtual int _count_declarations() { return 0; }
virtual int count_declarations() {
if(memo != -1) return memo;
return memo = _count_declarations();
}
template<typename T>
static shared_ptr<T> get_as(shared_ptr<ASTNode> st){
return dynamic_pointer_cast<T>(st);
}
static void check_and_generate_expression(shared_ptr<ASTNode>, Code & code, ScopeStack &);
};
struct DecASTNode : public ASTNode {
int val;
DecASTNode(string s) : val(atoi(s.c_str())){}
DecASTNode(int x) : val(x){}
DecASTNode(shared_ptr<ASTNode> st){
val = atoi(st->get_text().c_str());
}
string get_text() const;
void check_and_generate(Code & code, ScopeStack & sta);
};
struct IdASTNode : public ASTNode{
IdASTNode(string s){
text = s;
}
IdASTNode(shared_ptr<ASTNode> st){
text = st->get_text();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct BinASTNode : public ASTNode{
shared_ptr<ASTNode> left, right;
BinASTNode(shared_ptr<ASTNode> left, shared_ptr<ASTNode> right, string text = "+"){
this->left = left;
this->right = right;
this->text = text;
}
BinASTNode(shared_ptr<ASTNode> left, shared_ptr<ASTNode> right,
shared_ptr<ASTNode> op){
this->left = left;
this->right = right;
this->text = op->get_text();
}
void print_children() const{
cout << " ";
left->print_node();
cout << " ";
right->print_node();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct UnASTNode : public ASTNode{
shared_ptr<ASTNode> child;
UnASTNode(shared_ptr<ASTNode> child, string text){
this->child = child;
this->text = text;
}
UnASTNode(shared_ptr<ASTNode> child, shared_ptr<ASTNode> op){
this->child = child;
this->text = op->get_text();
}
void print_children() const {
cout << " ";
child->print_node();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct TypeASTNode : public ASTNode{
shared_ptr<ASTNode> st;
TypeASTNode(shared_ptr<ASTNode> st){
this->st = st;
}
string get_text() const {
return this->st->get_text();
}
bool is_int() const { return this->get_text() == "int"; }
bool is_void() const { return this->get_text() == "void"; }
};
struct VarASTNode : public ASTNode {
shared_ptr<IdASTNode> id;
shared_ptr<TypeASTNode> type;
VarASTNode(shared_ptr<ASTNode> id, shared_ptr<ASTNode> type){
this->id = static_pointer_cast<IdASTNode>(id);
this->type = static_pointer_cast<TypeASTNode>(type);
}
string get_text() const {
return id->get_text();
}
bool is_int() const {
return type->is_int();
}
bool is_void() const {
return type->is_void();
}
};
struct ListASTNode : public ASTNode{
vector<shared_ptr<ASTNode>> child;
virtual void append(shared_ptr<ASTNode> nw){
child.push_back(nw);
}
int size() const { return child.size(); }
void print_children() const {
for(const auto & no : child){
cout << " ";
no->print_node();
}
}
};
struct ArgsASTNode : public ListASTNode{
string get_text() const {
return "arglist";
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct ParamsASTNode : public ListASTNode{
string get_text() const {
return "paramlist";
}
int _count_declarations() override { return this->size(); }
void check_and_generate(Code & code, ScopeStack & sta);
};
struct CallASTNode : public ASTNode{
shared_ptr<IdASTNode> id;
shared_ptr<ArgsASTNode> args;
CallASTNode(shared_ptr<ASTNode> id){
this->id = static_pointer_cast<IdASTNode>(id);
this->args = make_shared<ArgsASTNode>();
}
CallASTNode(shared_ptr<ASTNode> id, shared_ptr<ArgsASTNode> args){
this->id = static_pointer_cast<IdASTNode>(id);
this->args = args;
}
string get_text() const {
return "funccall";
}
string get_func_name() const {
return this->id->get_text();
}
void print_children() const {
cout << " ";
id->print_node();
cout << " ";
args->print_node();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct AssignASTNode : public ASTNode{
shared_ptr<IdASTNode> id;
shared_ptr<ASTNode> expr;
AssignASTNode(shared_ptr<ASTNode> id, shared_ptr<ASTNode> expr){
this->id = static_pointer_cast<IdASTNode>(id);
this->expr = expr;
}
string get_text() const {
return "assign";
}
void print_children() const {
cout << " ";
id->print_node();
cout << " ";
expr->print_node();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct DecvarASTNode : public ASTNode{
shared_ptr<VarASTNode> var;
shared_ptr<ASTNode> expr;
DecvarASTNode(shared_ptr<ASTNode> var, shared_ptr<ASTNode> expr = 0){
this->var = static_pointer_cast<VarASTNode>(var);
this->expr = expr;
}
string get_text() const {
return "decvar";
}
void print_children() const {
cout << " ";
var->print_node();
if(expr){
cout << " ";
expr->print_node();
}
}
int _count_declarations() override { return 1; }
void check_and_generate(Code & code, ScopeStack & sta);
};
struct ProgASTNode : public ListASTNode{
void append(shared_ptr<ASTNode> st){
//child.insert(child.begin(), st);
child.push_back(st);
}
string get_text() const {
return "program";
}
int _count_declarations() override {
int res = 0;
for(auto p : child)
res += p->count_declarations();
return res;
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct LoopASTNode : public ASTNode{
int idx = 0;
int get_index() const { return idx; }
};
struct BlockASTNode : public ASTNode{
vector<shared_ptr<ASTNode>> declarations, statements;
void append_declaration(shared_ptr<ASTNode> st){
declarations.push_back(st);
}
void append_statement(shared_ptr<ASTNode> st){
statements.push_back(st);
}
string get_text() const {
return "block";
}
void print_children() const {
for(const auto & p : declarations){
cout << " ";
p->print_node();
}
for(const auto & p : statements){
cout << " ";
p->print_node();
}
}
int _count_declarations() override{
int res = declarations.size();
for(auto p : statements)
res += p->count_declarations();
return res;
}
void check_and_generate(Code & code, ScopeStack & sta){
for(auto p : declarations)
p->check_and_generate(code, sta);
for(auto p : statements)
p->check_and_generate(code, sta);
}
};
struct DecfuncASTNode : public ASTNode {
shared_ptr<VarASTNode> var;
shared_ptr<ParamsASTNode> params;
shared_ptr<BlockASTNode> block;
DecfuncASTNode(shared_ptr<ASTNode> var, shared_ptr<ASTNode> params,
shared_ptr<ASTNode> block){
this->var = static_pointer_cast<VarASTNode>(var);
this->params = dynamic_pointer_cast<ParamsASTNode>(params);
this->block = static_pointer_cast<BlockASTNode>(block);
}
string get_text() const {
return "decfunc";
}
void print_children() const {
cout << " ";
var->print_node();
cout << " ";
params->print_node();
cout << " ";
block->print_node();
}
int _count_declarations() override{
return block->count_declarations();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct ReturnASTNode : public ASTNode{
shared_ptr<ASTNode> expr;
ReturnASTNode(shared_ptr<ASTNode> expr = 0){
this->expr = expr;
}
string get_text() const{
return "return";
}
void print_children() const {
if(expr){
cout << " ";
expr->print_node();
}
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct BreakASTNode : public ASTNode{
string get_text() const {
return "break";
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct ContinueASTNode : public ASTNode{
string get_text() const{
return "continue";
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct WhileASTNode : public LoopASTNode{
shared_ptr<ASTNode> expr;
shared_ptr<BlockASTNode> block;
WhileASTNode(shared_ptr<ASTNode> expr, shared_ptr<ASTNode> block){
this->expr = expr;
this->block = dynamic_pointer_cast<BlockASTNode>(block);
}
string get_text() const {
return "while";
}
int _count_declarations() override {
return block->count_declarations();
}
void print_children() const {
cout << " ";
expr->print_node();
cout << " ";
block->print_node();
}
void check_and_generate(Code & code, ScopeStack & sta);
};
struct IfASTNode : public ASTNode{
shared_ptr<ASTNode> expr;
shared_ptr<BlockASTNode> block, else_block;
IfASTNode(shared_ptr<ASTNode> expr, shared_ptr<ASTNode> block,
shared_ptr<ASTNode> else_block = 0){
this->expr = expr;
this->block = dynamic_pointer_cast<BlockASTNode>(block);
this->else_block = dynamic_pointer_cast<BlockASTNode>(else_block);
}
string get_text() const {
return "if";
}
void print_children() const {
cout << " ";
expr->print_node();
cout << " ";
block->print_node();
if(else_block){
cout << " ";
else_block->print_node();
}
}
int _count_declarations() override{
return block->count_declarations() +
(else_block ? else_block->count_declarations() : 0);
}
void check_and_generate(Code & code, ScopeStack & sta);
};