-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.hpp
357 lines (261 loc) · 8.53 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
#ifndef _AST_HPP_
#define _AST_HPP_
#include <memory>
#include <vector>
#include "token.hpp"
namespace pl0::ast {
using token::Token;
// Forward
struct Block;
struct ConstDeclarations;
struct VariableDeclarations;
struct ProcedureDeclaration;
struct AssignStatement;
struct CallStatement;
struct InputStatement;
struct PrintStatement;
struct BeginStatement;
struct IfStatement;
struct WhileStatement;
struct OddExpression;
struct BinaryExpression;
struct UnaryExpression;
struct VariableExpression;
struct LiteralExpression;
struct AstVisitor {
AstVisitor() = default;
virtual ~AstVisitor() = default;
virtual auto visit(Block* block) -> void = 0;
virtual auto visit(ConstDeclarations* decl) -> void = 0;
virtual auto visit(VariableDeclarations* decl) -> void = 0;
virtual auto visit(ProcedureDeclaration* decl) -> void = 0;
virtual auto visit(AssignStatement* stmt) -> void = 0;
virtual auto visit(CallStatement* stmt) -> void = 0;
virtual auto visit(InputStatement* stmt) -> void = 0;
virtual auto visit(PrintStatement* stmt) -> void = 0;
virtual auto visit(BeginStatement* stmt) -> void = 0;
virtual auto visit(IfStatement* stmt) -> void = 0;
virtual auto visit(WhileStatement* stmt) -> void = 0;
virtual auto visit(OddExpression* expr) -> void = 0;
virtual auto visit(BinaryExpression* expr) -> void = 0;
virtual auto visit(UnaryExpression* expr) -> void = 0;
virtual auto visit(VariableExpression* expr) -> void = 0;
virtual auto visit(LiteralExpression* expr) -> void = 0;
};
// Statement base class
struct Statement {
Statement() = default;
virtual ~Statement() = default;
virtual auto accept(AstVisitor* visitor) -> void = 0;
};
using StatementPtr = std::unique_ptr<Statement>;
template<typename T>
concept StatementType = requires {
std::is_base_of_v<Statement, T>;
};
template <StatementType Stmt, typename... Args>
inline auto buildStatement(Args&&... args) -> StatementPtr {
return std::unique_ptr<Statement>(new Stmt(std::forward<Args>(args)...));
}
// Expression base class
struct Expression {
Expression() = default;
virtual ~Expression() = default;
virtual auto accept(AstVisitor* visitor) -> void = 0;
};
using ExpressionPtr = std::unique_ptr<Expression>;
template<typename T>
concept ExpressionType = requires {
std::is_base_of_v<Expression, T>;
};
template <ExpressionType Expr, typename... Args>
inline auto buildExpression(Args&&... args) -> ExpressionPtr {
return std::unique_ptr<Expression>(new Expr(std::forward<Args>(args)...));
}
// Statements
struct Block final : public Statement {
Block(StatementPtr& constantsDeclaration,
StatementPtr& variablesDeclaration,
std::vector<StatementPtr>& procedureDeclarations,
StatementPtr& statement)
: constantsDeclaration(std::move(constantsDeclaration)),
variablesDeclaration(std::move(variablesDeclaration)),
procedureDeclarations(std::move(procedureDeclarations)),
statement(std::move(statement)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
StatementPtr constantsDeclaration;
StatementPtr variablesDeclaration;
std::vector<StatementPtr> procedureDeclarations;
StatementPtr statement;
};
struct ConstDeclaration {
Token identifier;
int initializer;
};
struct ConstDeclarations final : public Statement {
ConstDeclarations(std::vector<ConstDeclaration>& declarations)
: declarations(std::move(declarations)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
std::vector<ConstDeclaration> declarations;
};
struct VariableDeclarations final : public Statement {
VariableDeclarations(std::vector<Token>& identifiers)
: identifiers(std::move(identifiers)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
std::vector<Token> identifiers;
};
struct ProcedureDeclaration final : public Statement {
ProcedureDeclaration(Token name, StatementPtr& block)
: name(name), block(std::move(block)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
Token name;
StatementPtr block;
};
struct AssignStatement final : public Statement {
AssignStatement(Token lvalue, ExpressionPtr& rvalue)
: lvalue(lvalue), rvalue(std::move(rvalue)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
Token lvalue;
ExpressionPtr rvalue;
};
struct CallStatement final : public Statement {
CallStatement(Token callee)
: callee(callee) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
Token callee;
};
struct InputStatement final : public Statement {
InputStatement(Token destination)
: destination(destination) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
Token destination;
};
struct PrintStatement final : public Statement {
PrintStatement(ExpressionPtr& arg)
: argument(std::move(arg)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
ExpressionPtr argument;
};
struct BeginStatement final : public Statement {
BeginStatement(std::vector<StatementPtr>& statements)
: statements(std::move(statements)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
std::vector<StatementPtr> statements;
};
struct IfStatement final : public Statement {
IfStatement(ExpressionPtr& condition, StatementPtr& body)
: condition(std::move(condition)),
body(std::move(body)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
ExpressionPtr condition;
StatementPtr body;
};
struct WhileStatement final : public Statement {
WhileStatement(ExpressionPtr& condition, StatementPtr& body)
: condition(std::move(condition)),
body(std::move(body)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
ExpressionPtr condition;
StatementPtr body;
};
// Expressions
struct OddExpression final : public Expression {
OddExpression(ExpressionPtr& expr)
: expr(std::move(expr)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
ExpressionPtr expr;
};
struct BinaryExpression final : public Expression {
BinaryExpression(ExpressionPtr& left, Token op, ExpressionPtr& right)
: left(std::move(left)), op(op), right(std::move(right)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
ExpressionPtr left;
Token op;
ExpressionPtr right;
};
struct UnaryExpression final : public Expression {
UnaryExpression(Token op, ExpressionPtr& right)
: op(op), right(std::move(right)) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
Token op;
ExpressionPtr right;
};
struct VariableExpression final : public Expression {
VariableExpression(Token name)
: name(name) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
Token name;
};
struct LiteralExpression final : public Expression {
constexpr LiteralExpression(int value)
: value(value) {}
auto accept(AstVisitor* visitor) -> void {
visitor->visit(this);
}
int value;
};
// AST Printer
class AstPrinter : public AstVisitor {
public:
AstPrinter() = default;
auto print(StatementPtr& ast) -> void;
private:
auto visit(Block* block) -> void;
auto visit(ConstDeclarations* decl) -> void;
auto visit(VariableDeclarations* decl) -> void;
auto visit(ProcedureDeclaration* decl) -> void;
auto visit(AssignStatement* stmt) -> void;
auto visit(CallStatement* stmt) -> void;
auto visit(InputStatement* stmt) -> void;
auto visit(PrintStatement* stmt) -> void;
auto visit(BeginStatement* stmt) -> void;
auto visit(IfStatement* stmt) -> void;
auto visit(WhileStatement* stmt) -> void;
auto visit(OddExpression* expr) -> void;
auto visit(BinaryExpression* expr) -> void;
auto visit(UnaryExpression* expr) -> void;
auto visit(VariableExpression* expr) -> void;
auto visit(LiteralExpression* expr) -> void;
inline auto indent() -> void {
m_level++;
}
inline auto dedent() -> void {
m_level--;
}
auto newline() const -> void;
private:
static constexpr int TAB_SIZE = 2;
int m_level = 0;
};
}
#endif