-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstmt.h
More file actions
293 lines (267 loc) · 8.27 KB
/
stmt.h
File metadata and controls
293 lines (267 loc) · 8.27 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
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
#pragma once
#include "expr.h"
#include "objects/array.h"
#include "scanner.h"
#define STMTTYPE(x) struct x##Statement;
#include "stmttypes.h"
#define NewStatement(x, ...) \
(::new(Gc::allocStatement2(sizeof(x##Statement))) \
x##Statement(__VA_ARGS__))
class StatementVisitor {
public:
virtual void visit(IfStatement *ifs) = 0;
virtual void visit(WhileStatement *ifs) = 0;
virtual void visit(FnStatement *ifs) = 0;
virtual void visit(FnBodyStatement *ifs) = 0;
virtual void visit(ClassStatement *ifs) = 0;
virtual void visit(TryStatement *ifs) = 0;
virtual void visit(CatchStatement *ifs) = 0;
virtual void visit(ImportStatement *ifs) = 0;
virtual void visit(BlockStatement *ifs) = 0;
virtual void visit(ExpressionStatement *ifs) = 0;
virtual void visit(VardeclStatement *ifs) = 0;
virtual void visit(MemberVariableStatement *ifs) = 0;
virtual void visit(VisibilityStatement *ifs) = 0;
virtual void visit(ThrowStatement *ifs) = 0;
virtual void visit(ReturnStatement *ifs) = 0;
virtual void visit(ForStatement *ifs) = 0;
virtual void visit(BreakStatement *ifs) = 0;
};
typedef enum { VIS_PUB, VIS_PROC, VIS_PRIV, VIS_DEFAULT } Visibility;
struct Statement {
public:
GcObject obj;
enum Type {
#define STMTTYPE(x) STMT_##x,
#include "stmttypes.h"
};
Token token;
Type type;
Statement(Token to, Type t) : token(to), type(t) {}
Type getType() { return type; }
bool isDeclaration() {
switch(type) {
case STMT_Fn:
case STMT_Class: return true;
default: return false;
};
}
#define STMTTYPE(x) \
bool is##x##Statement() { return type == STMT_##x; } \
x##Statement *to##x##Statement() { return (x##Statement *)this; }
#include "stmttypes.h"
bool isImport() { return (type == STMT_Import); }
void accept(StatementVisitor *visitor);
void mark();
size_t getSize(); // returns the actual allocated memory based on type
#ifdef DEBUG_GC
// const char *gc_repr();
#endif
};
struct IfStatement : public Statement {
public:
Expression *condition;
Statement * thenBlock;
Statement * elseBlock;
IfStatement(Token it, const Expression2 &cond, const Statement2 &then,
const Statement2 &else_)
: Statement(it, STMT_If), condition(cond), thenBlock(then),
elseBlock(else_) {}
void mark() {
Gc::mark(condition);
Gc::mark(thenBlock);
Gc::mark(elseBlock);
}
};
struct WhileStatement : public Statement {
public:
Expression *condition;
Statement * thenBlock;
bool isDo;
WhileStatement(Token w, const Expression2 &cond, const Statement2 &then,
bool isd)
: Statement(w, STMT_While), condition(cond), thenBlock(then),
isDo(isd) {}
void mark() {
Gc::mark(condition);
Gc::mark(thenBlock);
}
};
struct FnBodyStatement : public Statement {
public:
Array * args;
Statement *body;
bool isva;
FnBodyStatement(Token t, const Array2 &ar, const Statement2 &b, bool isv)
: Statement(t, STMT_FnBody), args(ar), body(b == nullptr ? nullptr : b),
isva(isv) {}
void mark() {
Gc::mark(args);
Gc::mark(body);
}
};
struct FnStatement : public Statement {
public:
Token name;
FnBodyStatement *body;
bool isMethod, isStatic, isNative, isConstructor;
Visibility visibility;
size_t arity;
FnStatement(Token fn, Token n, FnBodyStatement *fnBody, bool ism, bool iss,
bool isn, bool isc, Visibility vis)
: Statement(fn, STMT_Fn), name(n), body(fnBody), isMethod(ism),
isStatic(iss), isNative(isn), isConstructor(isc), visibility(vis),
arity(body->args->size - body->isva) {}
void mark() { Gc::mark(body); }
};
struct VardeclStatement : public Statement {
public:
Expression *expr;
Visibility vis;
VardeclStatement(Token name, const Expression2 &e, Visibility v)
: Statement(name, STMT_Vardecl), expr(e), vis(v) {}
void mark() { Gc::mark(expr); }
};
struct ClassStatement : public Statement {
public:
Token name;
Visibility vis;
bool isDerived;
Token derived;
Array * declarations;
ClassStatement(Token c, Token n, const Array2 &decl, Visibility v)
: Statement(c, STMT_Class), name(n), vis(v), isDerived(false),
declarations(decl) {}
ClassStatement(Token c, Token n, const Array2 &decl, Visibility v, bool isd,
Token d)
: ClassStatement(c, n, decl, v) {
isDerived = isd;
derived = d;
}
void mark() { Gc::mark(declarations); }
};
struct VisibilityStatement : public Statement {
public:
VisibilityStatement(Token t) : Statement(t, STMT_Visibility) {}
void mark() {}
};
struct MemberVariableStatement : public Statement {
public:
Array *members;
bool isStatic;
MemberVariableStatement(Token t, const Array2 &mem, bool iss)
: Statement(t, STMT_MemberVariable), members(mem), isStatic(iss) {}
void mark() { Gc::mark(members); }
};
struct ImportStatement : public Statement {
public:
Array *import_;
ImportStatement(Token t, const Array2 &imp)
: Statement(t, STMT_Import), import_(imp) {}
void mark() { Gc::mark(import_); }
};
struct TryStatement : public Statement {
public:
Statement *tryBlock;
Array * catchBlocks;
TryStatement(Token t, const Statement2 &tr, const Array2 &catches)
: Statement(t, STMT_Try), tryBlock(tr), catchBlocks(catches) {}
void mark() {
Gc::mark(tryBlock);
Gc::mark(catchBlocks);
}
};
struct CatchStatement : public Statement {
public:
Token typeName, varName;
Statement *block;
CatchStatement(Token c, Token typ, Token var, const Statement2 &b)
: Statement(c, STMT_Catch), typeName(typ), varName(var), block(b) {}
void mark() { Gc::mark(block); }
};
struct BlockStatement : public Statement {
public:
Array *statements;
bool isStatic;
BlockStatement(Token t)
: Statement(t, STMT_Block), statements(nullptr), isStatic(false) {}
BlockStatement(Token t, const Array2 &sts, bool iss = false)
: Statement(t, STMT_Block), statements(sts), isStatic(iss) {}
void mark() { Gc::mark(statements); }
};
struct ExpressionStatement : public Statement {
public:
Array *exprs;
ExpressionStatement(Token t)
: Statement(t, STMT_Expression), exprs(nullptr) {}
ExpressionStatement(Token t, const Array2 &e)
: Statement(t, STMT_Expression), exprs(e) {}
void mark() { Gc::mark(exprs); }
};
struct ThrowStatement : public Statement {
public:
Expression *expr;
ThrowStatement(Token t, const Expression2 &e)
: Statement(t, STMT_Throw), expr(e) {}
void mark() { Gc::mark(expr); }
};
struct ReturnStatement : public Statement {
public:
Expression *expr;
ReturnStatement(Token t, const Expression2 &e)
: Statement(t, STMT_Return), expr(e) {}
void mark() { Gc::mark(expr); }
};
struct ForStatement : public Statement {
public:
bool is_iterator;
Expression *cond;
Array * initializer, *incr;
Statement * body;
ForStatement(Token t, bool isi, const Array2 &ini, const Expression2 &c,
const Array2 &inc, const Statement2 &b)
: Statement(t, STMT_For), is_iterator(isi), cond(c), initializer(ini),
incr(inc), body(b) {}
void mark() {
Gc::mark(cond);
Gc::mark(initializer);
Gc::mark(incr);
Gc::mark(body);
}
};
struct BreakStatement : public Statement {
public:
BreakStatement(Token t) : Statement(t, STMT_Break) {}
void mark() {}
};
#ifdef DEBUG
struct StatementPrinter : public StatementVisitor {
private:
WritableStream & os;
ExpressionPrinter ep;
int tabCount;
void printTabs();
bool onElse;
public:
StatementPrinter(WritableStream &o)
: os(o), ep(o), tabCount(0), onElse(false) {}
void print(Statement *s);
void visit(IfStatement *ifs);
void visit(WhileStatement *ifs);
void visit(FnStatement *ifs);
void visit(FnBodyStatement *ifs);
void visit(ClassStatement *ifs);
void visit(TryStatement *ifs);
void visit(CatchStatement *ifs);
void visit(ImportStatement *ifs);
void visit(BlockStatement *ifs);
void visit(ExpressionStatement *ifs);
void visit(VardeclStatement *ifs);
void visit(MemberVariableStatement *ifs);
void visit(VisibilityStatement *ifs);
void visit(ThrowStatement *ifs);
void visit(ReturnStatement *ifs);
void visit(ForStatement *ifs);
void visit(BreakStatement *ifs);
};
#endif