-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast.hpp
executable file
·400 lines (317 loc) · 10.2 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
#ifndef AST_H
#define AST_H
#include <iostream>
#include <memory>
#include <map>
#include <vector>
// Forward declarations
namespace ast {
struct Node;
struct Program;
struct Type;
struct Expression;
struct UnaryExpression;
struct BinaryExpression;
struct Identifier;
struct IntType;
struct RealType;
struct BoolType;
struct ArrayType;
struct RecordType;
struct IntLiteral;
struct RealLiteral;
struct BoolLiteral;
struct VariableDeclaration;
struct RoutineDeclaration;
struct Body;
struct Statement;
struct ReturnStatement;
struct PrintStatement;
struct AssignmentStatement;
struct IfStatement;
struct WhileLoop;
struct ForLoop;
struct RoutineCall;
} // namespace ast
// Base class for code generator and anything that traverses AST.
class Visitor {
public:
virtual void visit(ast::Program *program) = 0;
virtual void visit(ast::IntType *it) = 0;
virtual void visit(ast::RealType *it) = 0;
virtual void visit(ast::BoolType *it) = 0;
virtual void visit(ast::ArrayType *at) = 0;
virtual void visit(ast::RecordType *rt) = 0;
virtual void visit(ast::IntLiteral *il) = 0;
virtual void visit(ast::RealLiteral *rl) = 0;
virtual void visit(ast::BoolLiteral *bl) = 0;
virtual void visit(ast::VariableDeclaration *vardecl) = 0;
virtual void visit(ast::Identifier *id) = 0;
virtual void visit(ast::UnaryExpression *exp) = 0;
virtual void visit(ast::BinaryExpression *exp) = 0;
virtual void visit(ast::RoutineDeclaration *routine) = 0;
virtual void visit(ast::Body *body) = 0;
virtual void visit(ast::ReturnStatement *stmt) = 0;
virtual void visit(ast::PrintStatement *stmt) = 0;
virtual void visit(ast::AssignmentStatement *stmt) = 0;
virtual void visit(ast::IfStatement *stmt) = 0;
virtual void visit(ast::WhileLoop *stmt) = 0;
virtual void visit(ast::ForLoop *stmt) = 0;
virtual void visit(ast::RoutineCall *stmt) = 0;
};
namespace ast {
// Pointer to an AST node.
template <typename Node> using node_ptr = std::shared_ptr<Node>;
// Enumerations
enum class TypeEnum { INT, REAL, BOOL, ARRAY, RECORD };
enum class OperatorEnum { PLUS, MINUS, MUL, DIV, MOD, AND, OR, NOT, XOR, EQ, NEQ, LT, GT, LEQ, GEQ };
// Base class for AST nodes
struct Node {
virtual void accept(Visitor *v) = 0;
};
// A special node containing program variables, type aliases, and routines.
struct Program : Node {
std::vector<node_ptr<VariableDeclaration>> variables;
std::map<std::string, node_ptr<Type>> types;
std::vector<node_ptr<RoutineDeclaration>> routines;
void accept(Visitor *v) override { v->visit(this); }
};
// Base class for Expressions
struct Expression : Node {
node_ptr<Type> dtype;
};
// Base class for Types
struct Type : Node {
virtual TypeEnum getType() { return TypeEnum::INT; };
virtual void accept(Visitor *v) = 0;
};
// Base class for Statements
struct Statement : virtual Node {
void accept(Visitor *v) override = 0;
};
// <Types>
struct IntType : Type {
IntType() {}
TypeEnum getType() { return TypeEnum::INT; }
void accept(Visitor *v) override { v->visit(this); }
};
struct RealType : Type {
RealType() {}
TypeEnum getType() { return TypeEnum::REAL; }
void accept(Visitor *v) override { v->visit(this); }
};
struct BoolType : Type {
BoolType() {}
TypeEnum getType() { return TypeEnum::BOOL; }
void accept(Visitor *v) override { v->visit(this); }
};
struct ArrayType : Type {
node_ptr<Expression> size;
node_ptr<Type> dtype;
ArrayType(node_ptr<Expression> size, node_ptr<Type>dtype) {
this->size = size;
this->dtype = dtype;
}
TypeEnum getType() { return TypeEnum::ARRAY; }
void accept(Visitor *v) override { v->visit(this); }
};
struct RecordType : Type {
std::string name; // set by llvm vardecl or typedecl
std::vector<node_ptr<VariableDeclaration>> fields;
RecordType(std::vector<node_ptr<VariableDeclaration>> fields) {
this->fields = fields;
}
TypeEnum getType() { return TypeEnum::RECORD; }
void accept(Visitor *v) override { v->visit(this); }
};
// </Types>
// <Expressions>
struct UnaryExpression : Expression {
node_ptr<Expression> operand;
OperatorEnum op;
UnaryExpression(OperatorEnum op, node_ptr<Expression> operand) {
this->operand = operand;
this->op = op;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct BinaryExpression : Expression {
node_ptr<Expression> lhs, rhs;
OperatorEnum op;
BinaryExpression(node_ptr<Expression> lhs, OperatorEnum op, node_ptr<Expression> rhs) {
this->lhs = lhs;
this->rhs = rhs;
this->op = op;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct IntLiteral : Expression {
int64_t value;
IntLiteral(int64_t value) {
this->dtype = std::make_shared<IntType>();
this->value = value;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct RealLiteral : Expression {
double value;
RealLiteral(double value) {
this->dtype = std::make_shared<RealType>();
this->value = value;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct BoolLiteral : Expression {
bool value;
BoolLiteral(bool value) {
this->dtype = std::make_shared<BoolType>();
this->value = value;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct Identifier : Expression {
std::string name;
node_ptr<Expression> idx;
// variable or record field access
Identifier(std::string name) {
this->name = name;
}
// array element access
Identifier(std::string name, node_ptr<Expression> idx) {
this->name = name;
this->idx = idx;
}
void accept(Visitor *v) override { v->visit(this); }
};
// </Expressions>
// <Nodes>
struct VariableDeclaration : Node {
std::string name;
node_ptr<Type> dtype;
node_ptr<Expression> initial_value;
VariableDeclaration(std::string name, node_ptr<Type> dtype) {
this->name = name;
this->dtype = dtype;
this->initial_value = nullptr;
}
VariableDeclaration(std::string name, node_ptr<Expression> initial_value) {
this->name = name;
this->dtype = initial_value->dtype;
this->initial_value = initial_value;
}
VariableDeclaration(std::string name, node_ptr<Type> dtype, node_ptr<Expression> initial_value) {
this->name = name;
this->dtype = dtype;
this->initial_value = initial_value;
}
void accept(Visitor *v) { v->visit(this); }
};
struct Body : Node {
std::vector<node_ptr<Statement>> statements;
std::vector<node_ptr<VariableDeclaration>> variables;
// std::vector<node_ptr<TypeDeclaration>> types;
Body(std::vector<node_ptr<VariableDeclaration>> variables, std::vector<node_ptr<Statement>> statements) {
this->variables = variables;
this->statements = statements;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct RoutineDeclaration : Node {
std::string name;
std::vector<node_ptr<VariableDeclaration>> params;
node_ptr<Type> rtype;
node_ptr<Body> body;
RoutineDeclaration(std::string name, std::vector<node_ptr<VariableDeclaration>> params, node_ptr<Body> body, node_ptr<Type> rtype) {
this->name = name;
this->params = params;
this->rtype = rtype;
this->body = body;
}
RoutineDeclaration(std::string name, std::vector<node_ptr<VariableDeclaration>> params, node_ptr<Body> body) {
this->name = name;
this->params = params;
this->body = body;
}
void accept(Visitor *v) override { v->visit(this); }
};
// </Nodes>
// <Statements>
struct ReturnStatement : Statement {
node_ptr<Expression> exp;
ReturnStatement() {}
ReturnStatement(node_ptr<Expression> exp) {
this->exp = exp;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct PrintStatement : Statement {
node_ptr<Expression> exp;
node_ptr<std::string> str;
bool endl;
PrintStatement(node_ptr<Expression> exp, bool endl=false) {
this->exp = exp;
this->endl = endl;
}
PrintStatement(node_ptr<std::string> str, bool endl=false) {
this->str = str;
this->endl = endl;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct AssignmentStatement : Statement {
node_ptr<Identifier> id;
node_ptr<Expression> exp;
AssignmentStatement(node_ptr<Identifier> id, node_ptr<Expression> exp) {
this->id = id;
this->exp = exp;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct IfStatement : Statement {
node_ptr<Expression> cond;
node_ptr<Body> then_body, else_body;
IfStatement(node_ptr<Expression> cond, node_ptr<Body> then_body) {
this->cond = cond;
this->then_body = then_body;
}
IfStatement(node_ptr<Expression> cond, node_ptr<Body> then_body, node_ptr<Body> else_body) {
this->cond = cond;
this->then_body = then_body;
this->else_body = else_body;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct WhileLoop : Statement {
node_ptr<Expression> cond;
node_ptr<Body> body;
WhileLoop(node_ptr<Expression> cond, node_ptr<Body> body) {
this->cond = cond;
this->body = body;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct ForLoop : Statement {
node_ptr<VariableDeclaration> loop_var;
node_ptr<Expression> cond;
node_ptr<Body> body;
node_ptr<AssignmentStatement> action;
ForLoop(node_ptr<VariableDeclaration> loop_var, node_ptr<Expression> cond, node_ptr<Body> body, node_ptr<AssignmentStatement> action) {
this->loop_var = loop_var;
this->cond = cond;
this->body = body;
this->action = action;
}
void accept(Visitor *v) override { v->visit(this); }
};
struct RoutineCall : Statement, Expression {
node_ptr<RoutineDeclaration> routine;
std::vector<node_ptr<Expression>> args;
RoutineCall(node_ptr<RoutineDeclaration> routine, std::vector<node_ptr<Expression>> args) {
this->routine = routine;
this->args = args;
}
void accept(Visitor *v) override { v->visit(this); }
};
// </Statements>
} // namespace ast
#endif // AST_H