-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.h
More file actions
245 lines (198 loc) · 6.99 KB
/
ast.h
File metadata and controls
245 lines (198 loc) · 6.99 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
#ifndef AST_H
#define AST_H
#include "token.h"
#include <memory>
#include <vector>
#include <string>
namespace AR437 {
// Forward declarations
class ASTVisitor;
// Base AST Node
class ASTNode {
public:
virtual ~ASTNode() = default;
virtual void accept(ASTVisitor& visitor) = 0;
virtual std::string toString() const = 0;
};
// Expression nodes
class Expression : public ASTNode {};
class NumberLiteral : public Expression {
public:
double value;
NumberLiteral(double val) : value(val) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class StringLiteral : public Expression {
public:
std::string value;
StringLiteral(const std::string& val) : value(val) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class BooleanLiteral : public Expression {
public:
bool value;
BooleanLiteral(bool val) : value(val) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class NullLiteral : public Expression {
public:
NullLiteral() {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class Identifier : public Expression {
public:
std::string name;
Identifier(const std::string& n) : name(n) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class BinaryOperation : public Expression {
public:
std::unique_ptr<Expression> left;
TokenType operator_;
std::unique_ptr<Expression> right;
BinaryOperation(std::unique_ptr<Expression> l, TokenType op, std::unique_ptr<Expression> r)
: left(std::move(l)), operator_(op), right(std::move(r)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class UnaryOperation : public Expression {
public:
TokenType operator_;
std::unique_ptr<Expression> operand;
UnaryOperation(TokenType op, std::unique_ptr<Expression> expr)
: operator_(op), operand(std::move(expr)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class Assignment : public Expression {
public:
std::string variable;
std::unique_ptr<Expression> value;
Assignment(const std::string& var, std::unique_ptr<Expression> val)
: variable(var), value(std::move(val)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class FunctionCall : public Expression {
public:
std::string name;
std::vector<std::unique_ptr<Expression>> arguments;
FunctionCall(const std::string& n) : name(n) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
// Statement nodes
class Statement : public ASTNode {};
class ExpressionStatement : public Statement {
public:
std::unique_ptr<Expression> expression;
ExpressionStatement(std::unique_ptr<Expression> expr) : expression(std::move(expr)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class VariableDeclaration : public Statement {
public:
std::string name;
std::unique_ptr<Expression> initializer;
VariableDeclaration(const std::string& n, std::unique_ptr<Expression> init = nullptr)
: name(n), initializer(std::move(init)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class Block : public Statement {
public:
std::vector<std::unique_ptr<Statement>> statements;
Block() {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class IfStatement : public Statement {
public:
std::unique_ptr<Expression> condition;
std::unique_ptr<Statement> thenBranch;
std::unique_ptr<Statement> elseBranch;
IfStatement(std::unique_ptr<Expression> cond, std::unique_ptr<Statement> then,
std::unique_ptr<Statement> else_ = nullptr)
: condition(std::move(cond)), thenBranch(std::move(then)), elseBranch(std::move(else_)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class WhileStatement : public Statement {
public:
std::unique_ptr<Expression> condition;
std::unique_ptr<Statement> body;
WhileStatement(std::unique_ptr<Expression> cond, std::unique_ptr<Statement> b)
: condition(std::move(cond)), body(std::move(b)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class ReturnStatement : public Statement {
public:
std::unique_ptr<Expression> value;
ReturnStatement(std::unique_ptr<Expression> val = nullptr) : value(std::move(val)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class DisplayStatement : public Statement {
public:
std::unique_ptr<Expression> expression;
DisplayStatement(std::unique_ptr<Expression> expr) : expression(std::move(expr)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class ReadStatement : public Statement {
public:
std::string variableName;
ReadStatement(const std::string& varName) : variableName(varName) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class FunctionDeclaration : public Statement {
public:
std::string name;
std::vector<std::string> parameters;
std::unique_ptr<Block> body;
FunctionDeclaration(const std::string& n, const std::vector<std::string>& params,
std::unique_ptr<Block> b)
: name(n), parameters(params), body(std::move(b)) {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
class Program : public ASTNode {
public:
std::vector<std::unique_ptr<Statement>> statements;
Program() {}
void accept(ASTVisitor& visitor) override;
std::string toString() const override;
};
// Visitor pattern for AST traversal
class ASTVisitor {
public:
virtual ~ASTVisitor() = default;
virtual void visit(NumberLiteral& node) = 0;
virtual void visit(StringLiteral& node) = 0;
virtual void visit(BooleanLiteral& node) = 0;
virtual void visit(NullLiteral& node) = 0;
virtual void visit(Identifier& node) = 0;
virtual void visit(BinaryOperation& node) = 0;
virtual void visit(UnaryOperation& node) = 0;
virtual void visit(Assignment& node) = 0;
virtual void visit(FunctionCall& node) = 0;
virtual void visit(ExpressionStatement& node) = 0;
virtual void visit(VariableDeclaration& node) = 0;
virtual void visit(Block& node) = 0;
virtual void visit(IfStatement& node) = 0;
virtual void visit(WhileStatement& node) = 0;
virtual void visit(ReturnStatement& node) = 0;
virtual void visit(DisplayStatement& node) = 0;
virtual void visit(ReadStatement& node) = 0;
virtual void visit(FunctionDeclaration& node) = 0;
virtual void visit(Program& node) = 0;
};
} // namespace AR437
#endif // AST_H