-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStmt.h
189 lines (151 loc) · 3.33 KB
/
Stmt.h
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
#pragma once
#include "Expr/Expr.h"
#include "Token.h"
#include "StmtVisitor.h"
#include <any>
#include <vector>
class Stmt;
typedef std::shared_ptr<Stmt> StmtPtr;
class Stmt
{
public:
Stmt() = default;
virtual ~Stmt() = default;
virtual std::any Accept(StmtVisitor<std::any>& visitor) const = 0;
};
class ExpressionStmt : public Stmt
{
private:
ExprPtr _expr;
public:
ExpressionStmt(ExprPtr expr)
{
_expr = expr;
}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitExpressionStmt(*this);
}
ExprPtr GetExpression() const
{
return _expr;
}
};
class PrintStmt : public Stmt
{
private:
ExprPtr _expr;
public:
PrintStmt(ExprPtr expr) : _expr(expr) {}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitPrintStmt(*this);
}
ExprPtr GetExpression() const { return _expr; }
};
class ReturnStmt : public Stmt
{
private:
Token _keyword;
ExprPtr _value;
public:
ReturnStmt(Token keyword, ExprPtr value)
{
_keyword = keyword;
_value = value;
}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitReturnStmt(*this);
}
auto GetKeyword() const { return _keyword; }
auto GetValue() const { return _value; }
};
class VarStmt : public Stmt
{
private:
ExprPtr _initializer;
Token _name;
public:
VarStmt(Token op, ExprPtr initializer)
{
_initializer = initializer;
_name = op;
}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitVarStmt(*this);
}
ExprPtr GetInitializer() const { return _initializer; }
Token GetName() const { return _name; }
};
class BlockStmt : public Stmt
{
private:
std::vector<StmtPtr> _statements;
public:
BlockStmt(std::vector<StmtPtr> statements)
{
_statements = statements;
}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitBlockStmt(*this);
}
std::vector<StmtPtr> GetStatements() const { return _statements; }
};
class If : public Stmt
{
private:
ExprPtr _condition;
StmtPtr _thenBranch;
StmtPtr _elseBranch;
public:
If(ExprPtr condition, StmtPtr then, StmtPtr elseB) : _condition(condition), _thenBranch(then), _elseBranch(elseB)
{
}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitIfStmt(*this);
}
auto GetCondition() const { return _condition; }
auto GetThenBranch() const { return _thenBranch; }
auto GetElseBranch() const { return _elseBranch; }
bool HasElseBranch() const { return _elseBranch != nullptr; }
};
class While : public Stmt
{
private:
ExprPtr _condition;
StmtPtr _body;
public:
While(ExprPtr cond, StmtPtr body) : _condition(cond), _body(body)
{}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitWhileStmt(*this);
}
auto GetCondition() const { return _condition; }
auto GetBody() const { return _body; }
};
class Function : public Stmt
{
private:
Token _name;
std::vector<Token> _params;
std::vector<StmtPtr> _body;
public:
Function(Token name, std::vector<Token> params, std::vector<StmtPtr> body)
{
_name = name;
_params = params;
_body = body;
}
std::any Accept(StmtVisitor<std::any>& visitor) const override
{
return visitor.VisitFunctionStmt(*this);
}
auto GetName() const { return _name; }
auto GetParams() const { return _params; }
auto GetBody() const { return _body; }
};