-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.h
245 lines (229 loc) · 7.45 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
#include<iostream>
#include"lexer.cpp"
#include<iostream>
#include<memory>
#include<vector>
using namespace std;
class Node {
public:
string type;
virtual string serialize() const = 0;
virtual string getType() const = 0;
};
// turn all fields into pointers
using namespace std;
struct NodeType {
const string Program = "PROGRAM";
const string Expression = "EXPRESSION";
const string Statement = "STATEMENT";
const string LetStatement = "LET_STATEMENT";
const string ReturnStatement = "RETURN_STATEMENT";
const string BlockStatement = "BLOCK_STATEMENT";
const string IntLiteral = "INT_LITERAL";
const string BoolLiteral = "BOOL_LITERAL";
const string FnLiteral = "FN_LITERAL";
const string CallExpression = "CALL_EXPRESSION";
const string Identifier = "IDENTIFIER";
const string ExpressionStatement = "EXPRESSION_STATEMENT";
const string IfExpression = "IF_EXPRESSION";
const string PrefixExpression = "PREFIX_EXPRESSION";
const string InfixExpression = "INFIX_EXPRESSION";
const string StringLiteral = "STRING_LITERAL";
const string IndexExpression = "INDEX_EXPRESSION";
const string ArrayLiteral = "ARRAY_LITERAL";
const string HashLiteral = "HASH_LITERAL";
} ntypes;
/************************* Expressions *********************/
class Expression : public Node {
public:
string type = ntypes.Expression;
Expression();
virtual ~Expression();
virtual string serialize() const;
virtual string getType() const;
};
class Identifier : public Expression {
public:
Token token;
string type = ntypes.Identifier;
string value;
string serialize() const final override;
string getType() const final override;
};
/************************* Statements ************************/
class Statement : public Node {
public:
string type = ntypes.Statement;
Statement();
virtual ~Statement();
virtual string serialize() const;
virtual string getType() const;
};
class LetStatement: public Statement {
public:
Token token;
string type = ntypes.LetStatement;
Identifier identifier;
unique_ptr<Expression> value;
LetStatement();
LetStatement(Token tok, Identifier ident, unique_ptr<Expression>& val);
string serialize() const final override;
string getType() const final override;
};
class ReturnStatement: public Statement {
public:
Token token;
string type = ntypes.ReturnStatement;
unique_ptr<Expression> value;
ReturnStatement();
ReturnStatement(Token tok, unique_ptr<Expression>& val);
string serialize() const final override;
string getType() const final override;
};
/* To allow for a single line expression like "x + 5;"*/
class ExpressionStatement: public Statement {
public:
Token token;
string type = ntypes.ExpressionStatement;
unique_ptr<Expression> expression;
ExpressionStatement();
ExpressionStatement(Token tok, unique_ptr<Expression>& express);
string serialize() const final override;
string getType() const final override;
};
class BlockStatement : public Statement {
public:
Token token;
string type = ntypes.BlockStatement;
vector<unique_ptr<Statement>> statements;
BlockStatement(Token tok, vector<unique_ptr<Statement>>&& stmts);
string serialize();
string getType() const final override;
};
// Expressions
class IntLiteral : public Expression {
public:
Token token;
string type = ntypes.IntLiteral;
int value;
IntLiteral(Token tok, int val);
string serialize() const final override;
string getType() const final override;
};
class BoolLiteral : public Expression {
public:
Token token;
string type = ntypes.BoolLiteral;
bool value;
BoolLiteral(Token tok, bool val);
string serialize() const final override;
string getType() const final override;
};
class StringLiteral : public Expression {
public:
Token token;
string type = ntypes.StringLiteral;
string value;
StringLiteral(Token tok, string val);
string serialize() const final override;
string getType() const final override;
};
class FnLiteral : public Expression {
public:
Token token;
string type = ntypes.FnLiteral;
vector<unique_ptr<Expression>> params;
unique_ptr<BlockStatement> body;
FnLiteral(Token tok, vector<unique_ptr<Expression>>&& params, unique_ptr<BlockStatement>& body);
string serialize() const override;
string getType() const override;
};
class ArrayLiteral : public Expression {
public:
Token token;
string type = ntypes.ArrayLiteral;
vector<unique_ptr<Expression>> elements;
ArrayLiteral(Token tok, vector<unique_ptr<Expression>>&& elements);
string serialize() const override;
string getType() const override;
};
// struct HashPair {
// Expression* key;
// Expression* value;
// };
class HashLiteral : public Expression {
public:
Token token;
string type = ntypes.HashLiteral;
vector<pair<unique_ptr<Expression>, unique_ptr<Expression>>> pairs;
HashLiteral(Token tok, vector<pair<unique_ptr<Expression>, unique_ptr<Expression>>>& pairs);
string serialize() const override;
string getType() const override;
};
class PrefixExpression : public Expression {
public:
Token token;
string type = ntypes.PrefixExpression;
string Operator;
unique_ptr<Expression> right;
PrefixExpression();
PrefixExpression(Token tok, string Operator, unique_ptr<Expression>& right);
string serialize() const override;
string getType() const override;
};
class IndexExpression : public Expression {
public:
Token token;
string type = ntypes.IndexExpression;
unique_ptr<Expression> entity;
unique_ptr<Expression> index;
IndexExpression(Token tok, unique_ptr<Expression>& entity, unique_ptr<Expression>& index);
string serialize() const override;
string getType() const override;
};
class InfixExpression : public Expression {
public:
Token token;
string Operator;
string type = ntypes.InfixExpression;
unique_ptr<Expression> left;
unique_ptr<Expression> right;
InfixExpression();
InfixExpression(Token tok, string Operator, unique_ptr<Expression>& left, unique_ptr<Expression>& right);
string serialize() const override;
string getType() const override;
};
class IfExpression : public Expression {
public:
Token token;
string type = ntypes.IfExpression;
unique_ptr<Expression> condition;
unique_ptr<BlockStatement> consequence;
unique_ptr<BlockStatement> alternative;
IfExpression(Token tok,
unique_ptr<Expression>& cond,
unique_ptr<BlockStatement>& cons,
unique_ptr<BlockStatement>& alt);
string serialize() const override;
string getType() const override;
};
class CallExpression : public Expression {
public:
Token token;
string type = ntypes.CallExpression;
unique_ptr<Expression> function;
vector<unique_ptr<Expression>> args;
CallExpression(Token tok, unique_ptr<Expression>& function, vector<unique_ptr<Expression>>&& args);
string serialize() const override;
string getType() const override;
};
/*********************** Program (root node) ********************/
class Program : public Node {
public:
string type = ntypes.Program;
vector<unique_ptr<Statement>> statements;
Program() = default;
// Program(vector<unique_ptr<Statement>>& statements) : statements(statements) {};
string serialize() const final override;
string getType() const override;
};