-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.h
299 lines (243 loc) · 7.4 KB
/
ast.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
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
// An Abstract Syntax Tree is used
// to represent a Fn program in a tree format;
// this provides the right ordering when
// generating code.
//
// This is the data structure that links the parser
// and code generator.
#pragma once
#include <string> // std::string
#include <vector> // std::vector
#include "src/number.h" // fn::Number
namespace fn { namespace ast {
// Base class for all statements.
class Statement {
public:
virtual ~Statement() {}
virtual std::string asString(int indent) = 0;
std::string asString() { return this->asString(0); }
};
// Base class for all statements
// that refer to values.
class Value : public Statement {
public:
virtual ~Value() {}
virtual std::string asString(int indent) = 0;
};
// Base class for all statements
// that alias defined values.
class Reference : public Value {
public:
virtual ~Reference() {}
virtual std::string asString(int indent) = 0;
};
// Represents a variable name.
class Id : public Reference {
public:
std::string name;
Id(std::string name) { this->name = name; }
~Id() = default;
std::string asString(int indent) override {
return "(ID " + this->name + ")";
}
};
// Represents a nested variable reference.
// (The variable exists within another value.)
class Deref : public Reference {
public:
Value* parent;
Value* child;
Deref(Value* parent, Value* child) {
this->parent = parent;
this->child = child;
}
~Deref() { delete this->child; delete this->parent; }
std::string asString(int indent) override {
return "(DEREF parent:" +
this->parent->asString(indent) +
" child:" +
this->child->asString(indent) + ")";
}
};
// Represents the definition of a variable.
class Assignment : public Statement {
public:
Reference* key;
Value* value;
Assignment(Reference* key, Value* value) {
this->key = key;
this->value = value;
}
~Assignment() { delete this->value; delete this->key; }
virtual std::string asString(int indent) override {
return "(ASSIGNMENT id:" + this->key->asString(indent) + " value:" + this->value->asString(indent) + ")";
}
};
// Represents a collection of statements which,
// when executed, return a value.
//
// They can be thought of as zero-arity, immediately executed
// function closures, if you're feeling masochistic.
class Block : public Value {
public:
std::vector<Statement*> statements;
Block(std::vector<Statement*> statements) { this->statements = statements; }
Block() { this->statements = std::vector<Statement*>(); }
~Block() {
for(auto statement : this->statements) { delete statement; }
}
std::string asString(int indent) override {
std::string result = "(BLOCK [\n";
for(auto statement: this->statements) {
result += std::string(indent + 2, ' ') +
statement->asString(indent + 2) +
"\n";
}
result += std::string(indent, ' ') + "])";
return result;
}
};
// Represents a collection of statements which,
// when executed, return a value.
//
// They can be thought of as zero-arity, immediately executed
// function closures, if you're feeling masochistic.
class BlockValue : public Value {
public:
std::vector<Statement*> statements;
BlockValue(std::vector<Statement*> statements) { this->statements = statements; }
BlockValue() { this->statements = std::vector<Statement*>(); }
~BlockValue() {
for(auto statement : this->statements) { delete statement; }
}
std::string asString(int indent) override {
std::string result = "(BLOCKVALUE [\n";
for(auto statement: this->statements) {
result += std::string(indent + 2, ' ') +
statement->asString(indent + 2) +
"\n";
}
result += std::string(indent, ' ') + "])";
return result;
}
};
// Represents a boolean value.
class Bool : public Value {
public:
bool value;
Bool(bool value) { this->value = value; }
~Bool() = default;
std::string asString(int indent) override {
return "(BOOL " + std::to_string(this->value) + ")";
}
};
// Represents a numeric value.
class Number : public Value {
public:
fn::Number value;
Number(std::string value) {
this->value = fn::Number(value);
}
~Number() = default;
std::string asString(int indent) override {
return "(NUMBER " + this->value.toString() + ")";
}
};
// Represents a string of characters.
class String : public Value {
public:
std::string value;
String(std::string value) { this->value = value; }
~String() = default;
std::string asString(int indent) override {
return "(STRING " + this->value + ")";
}
};
// Represents a call (request to execute) to a function.
class Call : public Value {
public:
Reference* target;
std::vector<Value*> args;
Call(Reference* target, std::vector<Value*> args) {
this->target = target;
this->args = args;
}
~Call() {
for(auto arg : this->args) { delete arg; }
delete this->target;
}
std::string asString(int indent) override {
std::string result = "(CALL target:" +
this->target->asString(indent) +
" args:[\n";
for(auto arg: this->args) {
result += std::string(indent + 2, ' ') + arg->asString(indent + 2) + "\n";
}
result += std::string(indent, ' ') + "])";
return result;
}
};
// Represents a function definition (sometimes known as a prototype).
class Def : public Value {
public:
std::vector<std::string> params;
Block* body;
Def(std::vector<std::string> params, Block* body) {
this->params = params;
this->body = body;
}
~Def() { delete this->body; }
size_t numParams() { return this->params.size(); }
virtual std::string asString(int indent) override {
std::string result = "(DEF params:[\n";
for(auto param: this->params) {
result += std::string(indent + 2, ' ') + param + "\n";
}
result += std::string(indent, ' ') +
"] body:" +
this->body->asString(indent) +
")";
return result;
}
};
// Used within Conditionals; represents a test
// and the code to execute if the test passes.
class Condition : public Statement {
public:
Value* test;
Block* body;
Condition(Value* test, Block* body) {
this->test = test;
this->body = body;
}
~Condition() { delete this->body; delete this->test; }
std::string asString(int indent) override {
return "(COND test:" +
this->test->asString(indent) +
" body:" +
this->body->asString(indent) + ")";
}
};
// Represents a collection of Conditions
// (conditionally executed code paths).
class Conditional : public Value {
public:
std::vector<Condition*> conditions;
Conditional(std::vector<Condition*> conditions) {
this->conditions = conditions;
}
~Conditional() {
for(auto condition : this->conditions) { delete condition; }
}
std::string asString(int indent) override {
std::string result = "(WHEN [\n";
for(auto condition: this->conditions) {
result += std::string(indent + 2, ' ') +
condition->asString(indent + 2) +
"\n";
}
result += std::string(indent, ' ') + "])";
return result;
}
};
}}