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
|
#pragma once
#include <string>
#include <memory>
#include <vector>
#include <cstdint>
namespace cps {
class SourceLocation {
int SourceLine = 0;
public:
explicit SourceLocation(int Line = 0) : SourceLine(Line) {}
virtual ~SourceLocation() = default;
int getLine() const { return SourceLine; }
void setLine(int Line) { SourceLine = Line; }
};
class ExprAST : public SourceLocation {
public:
using SourceLocation::SourceLocation;
virtual ~ExprAST() = default;
};
class IntegerExprAST : public ExprAST {
int64_t Val;
public:
IntegerExprAST(int64_t Val) : Val(Val) {}
int64_t getVal() const { return Val; }
};
class RealExprAST : public ExprAST {
double Val;
public:
RealExprAST(double Val) : Val(Val) {}
double getVal() const { return Val; }
};
class BooleanExprAST : public ExprAST {
bool Val;
public:
BooleanExprAST(bool Val) : Val(Val) {}
bool getVal() const { return Val; }
};
class CharExprAST : public ExprAST {
char Val;
public:
CharExprAST(char Val) : Val(Val) {}
char getVal() const { return Val; }
};
class StringExprAST : public ExprAST {
std::string Val;
public:
StringExprAST(const std::string &Val) : Val(Val) {}
const std::string &getVal() const { return Val; }
};
class VariableExprAST : public ExprAST {
std::string Name;
public:
VariableExprAST(const std::string &Name) : Name(Name) {}
const std::string &getName() const { return Name; }
};
class ArrayAccessExprAST : public ExprAST {
std::string Name;
std::vector<std::unique_ptr<ExprAST>> Indices;
public:
ArrayAccessExprAST(const std::string &Name, std::vector<std::unique_ptr<ExprAST>> Indices, int Line)
: ExprAST(Line), Name(Name), Indices(std::move(Indices)) {}
const std::string &getName() const { return Name; }
const std::vector<std::unique_ptr<ExprAST>> &getIndices() const { return Indices; }
};
class UnaryExprAST : public ExprAST {
int Op;
std::unique_ptr<ExprAST> Operand;
public:
UnaryExprAST(int Op, std::unique_ptr<ExprAST> Operand)
: Op(Op), Operand(std::move(Operand)) {}
int getOp() const { return Op; }
ExprAST *getOperand() const { return Operand.get(); }
};
class BinaryExprAST : public ExprAST {
int Op;
std::unique_ptr<ExprAST> LHS, RHS;
public:
BinaryExprAST(int Op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS, int Line)
: ExprAST(Line), Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
int getOp() const { return Op; }
ExprAST *getLHS() const { return LHS.get(); }
ExprAST *getRHS() const { return RHS.get(); }
};
class StmtAST : public SourceLocation {
public:
using SourceLocation::SourceLocation;
virtual ~StmtAST() = default;
};
class TryStmtAST : public StmtAST {
std::vector<std::unique_ptr<StmtAST>> Body, CatchBody;
public:
TryStmtAST(std::vector<std::unique_ptr<StmtAST>> Body,
std::vector<std::unique_ptr<StmtAST>> CatchBody)
: Body(std::move(Body)), CatchBody(std::move(CatchBody)) {}
const std::vector<std::unique_ptr<StmtAST>> &getBody() const { return Body; }
const std::vector<std::unique_ptr<StmtAST>> &getCatchBody() const { return CatchBody; }
};
class ThrowStmtAST : public StmtAST {};
class ConstantStmtAST : public StmtAST {
std::string Name;
std::unique_ptr<ExprAST> Value;
public:
ConstantStmtAST(std::string Name, std::unique_ptr<ExprAST> Value)
: Name(std::move(Name)), Value(std::move(Value)) {}
const std::string &getName() const { return Name; }
ExprAST *getValue() const { return Value.get(); }
};
class DeclareStmtAST : public StmtAST {
std::string Name;
std::string Type;
public:
DeclareStmtAST(const std::string &Name, const std::string &Type = "INTEGER")
: Name(Name), Type(Type) {}
const std::string &getName() const { return Name; }
const std::string &getType() const { return Type; }
};
class ArrayDeclareStmtAST : public StmtAST {
std::string Name;
std::vector<std::pair<std::unique_ptr<ExprAST>, std::unique_ptr<ExprAST>>> Bounds;
std::string Type;
public:
ArrayDeclareStmtAST(const std::string &Name,
std::vector<std::pair<std::unique_ptr<ExprAST>, std::unique_ptr<ExprAST>>> Bounds,
const std::string &Type)
: Name(Name), Bounds(std::move(Bounds)), Type(Type) {}
const std::string &getName() const { return Name; }
const std::vector<std::pair<std::unique_ptr<ExprAST>, std::unique_ptr<ExprAST>>> &getBounds() const { return Bounds; }
const std::string &getType() const { return Type; }
};
class AssignStmtAST : public StmtAST {
std::string Name;
std::unique_ptr<ExprAST> Expr;
public:
AssignStmtAST(const std::string &Name, std::unique_ptr<ExprAST> Expr)
: Name(Name), Expr(std::move(Expr)) {}
const std::string &getName() const { return Name; }
ExprAST *getExpr() const { return Expr.get(); }
};
class ArrayAssignStmtAST : public StmtAST {
std::string Name;
std::vector<std::unique_ptr<ExprAST>> Indices;
std::unique_ptr<ExprAST> Expr;
public:
ArrayAssignStmtAST(const std::string &Name, std::vector<std::unique_ptr<ExprAST>> Indices, std::unique_ptr<ExprAST> Expr, int Line)
: StmtAST(Line), Name(Name), Indices(std::move(Indices)), Expr(std::move(Expr)) {}
const std::string &getName() const { return Name; }
const std::vector<std::unique_ptr<ExprAST>> &getIndices() const { return Indices; }
ExprAST *getExpr() const { return Expr.get(); }
};
class InputStmtAST : public StmtAST {
std::string Name;
public:
InputStmtAST(const std::string &Name) : Name(Name) {}
const std::string &getName() const { return Name; }
};
class OutputStmtAST : public StmtAST {
std::vector<std::unique_ptr<ExprAST>> Exprs;
public:
OutputStmtAST(std::vector<std::unique_ptr<ExprAST>> Exprs) : Exprs(std::move(Exprs)) {}
const std::vector<std::unique_ptr<ExprAST>> &getExprs() const { return Exprs; }
};
struct CaseBranch {
std::unique_ptr<ExprAST> Lower, Upper; // Null Lower denotes OTHERWISE.
std::vector<std::unique_ptr<StmtAST>> Body;
int Line;
};
class CaseStmtAST : public StmtAST {
std::string Selector;
std::vector<CaseBranch> Branches;
public:
CaseStmtAST(std::string Selector, std::vector<CaseBranch> Branches)
: Selector(std::move(Selector)), Branches(std::move(Branches)) {}
const std::string &getSelector() const { return Selector; }
const std::vector<CaseBranch> &getBranches() const { return Branches; }
};
class IfStmtAST : public StmtAST {
std::unique_ptr<ExprAST> Cond;
std::vector<std::unique_ptr<StmtAST>> ThenStmts;
std::vector<std::unique_ptr<StmtAST>> ElseStmts;
public:
IfStmtAST(std::unique_ptr<ExprAST> Cond,
std::vector<std::unique_ptr<StmtAST>> ThenStmts,
std::vector<std::unique_ptr<StmtAST>> ElseStmts)
: Cond(std::move(Cond)), ThenStmts(std::move(ThenStmts)), ElseStmts(std::move(ElseStmts)) {}
ExprAST *getCond() const { return Cond.get(); }
const std::vector<std::unique_ptr<StmtAST>> &getThenStmts() const { return ThenStmts; }
const std::vector<std::unique_ptr<StmtAST>> &getElseStmts() const { return ElseStmts; }
};
class WhileStmtAST : public StmtAST {
std::unique_ptr<ExprAST> Cond;
std::vector<std::unique_ptr<StmtAST>> Body;
public:
WhileStmtAST(std::unique_ptr<ExprAST> Cond, std::vector<std::unique_ptr<StmtAST>> Body)
: Cond(std::move(Cond)), Body(std::move(Body)) {}
ExprAST *getCond() const { return Cond.get(); }
const std::vector<std::unique_ptr<StmtAST>> &getBody() const { return Body; }
};
class RepeatStmtAST : public StmtAST {
std::vector<std::unique_ptr<StmtAST>> Body;
std::unique_ptr<ExprAST> Cond;
public:
RepeatStmtAST(std::vector<std::unique_ptr<StmtAST>> Body, std::unique_ptr<ExprAST> Cond)
: Body(std::move(Body)), Cond(std::move(Cond)) {}
ExprAST *getCond() const { return Cond.get(); }
const std::vector<std::unique_ptr<StmtAST>> &getBody() const { return Body; }
};
class ForStmtAST : public StmtAST {
std::string VarName;
std::unique_ptr<ExprAST> Start;
std::unique_ptr<ExprAST> End;
std::unique_ptr<ExprAST> Step;
std::vector<std::unique_ptr<StmtAST>> Body;
public:
ForStmtAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
std::vector<std::unique_ptr<StmtAST>> Body)
: VarName(VarName), Start(std::move(Start)), End(std::move(End)),
Step(std::move(Step)), Body(std::move(Body)) {}
const std::string &getVarName() const { return VarName; }
ExprAST *getStart() const { return Start.get(); }
ExprAST *getEnd() const { return End.get(); }
ExprAST *getStep() const { return Step.get(); }
const std::vector<std::unique_ptr<StmtAST>> &getBody() const { return Body; }
};
} // namespace cps
|