-
Notifications
You must be signed in to change notification settings - Fork 1
/
syntaxtree.h
366 lines (311 loc) · 7.7 KB
/
syntaxtree.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#ifndef _SYNTAXTREE_H
#define _SYNTAXTREE_H
#define YYSTYPE_IS_DECLARED
namespace Syntax {
class Node;
typedef Node *Tree;
}
typedef Syntax::Tree YYSTYPE;
#include <parser.hpp>
#include <errormsg.h>
#include <idmap.h>
#include <string>
#include <list>
#include <assert.h>
#include <stdio.h>
namespace Syntax {
enum NodeType {
INTVALUE,
STRINGVALUE,
IDENTIFIER,
NIL,
BINARYOP,
EXPRESSIONLIST,
SEQUENCE,
ARRAYINDEXING,
ARRAYINSTANTIATION,
IF,
IFELSE,
WHILE,
FOR,
BREAK,
SCOPE,
RECORDFIELD,
FUNCTIONCALL,
RECORDINSTANTIATION,
TYPEDECLARATION,
ARRAYTYPEDEFINITION,
RECORDTYPEDEFINITION,
PARAMETERDECLARATION,
VARDECLARATION,
FUNCTION
};
class Node {
protected:
public:
NodeType type;
int linenumber;
Node(NodeType _type) : type(_type), linenumber(Error::getLineNumber()) {}
};
class IntValue: public Node {
public:
int value;
IntValue(int _value) : Node(INTVALUE), value(_value) {}
};
class Identifier: public Node {
public:
std::string name;
Identifier(const char *_name) : Node(IDENTIFIER), name(_name) {}
};
class StringValue: public Node {
public:
std::string value;
StringValue(const std::string &_value = "") : Node(STRINGVALUE), value(_value) {}
};
class BinaryOp: public Node {
public:
yytokentype operation;
Tree left, right;
BinaryOp(yytokentype _operation, Tree _left, Tree _right) :
Node(BINARYOP), operation(_operation), left(_left), right(_right)
{
switch (_operation) {
case SYM_PLUS:
case SYM_MINUS:
case SYM_ASTERISK:
case SYM_SLASH:
case SYM_EQUAL:
case SYM_NONEQUAL:
case SYM_LESS:
case SYM_LESSEQUAL:
case SYM_GREATER:
case SYM_GREATEQUAL:
case SYM_ASSIGN:
case SYM_AND:
case SYM_OR:
return;
default:
Error::fatalError("Unexpected operation token", left->linenumber);
}
}
};
class ExpressionList: public Node {
public:
std::list<Tree> expressions;
ExpressionList() : Node(EXPRESSIONLIST) {}
void prepend(Tree expression)
{
expressions.push_front(expression);
}
};
class Sequence: public Node {
public:
ExpressionList *content;
Sequence(Tree _content) : Node(SEQUENCE), content((ExpressionList *)_content)
{
assert(_content->type == EXPRESSIONLIST);
}
};
class ArrayIndexing: public Node {
public:
Tree array, index;
ArrayIndexing(Tree _array, Tree _index) :
Node(ARRAYINDEXING), array(_array), index(_index) {}
};
class ArrayInstantiation: public Node {
public:
ArrayIndexing *arraydef;
Tree value;
ArrayInstantiation(Tree _def, Tree _value) :
Node(ARRAYINSTANTIATION), arraydef((ArrayIndexing *)_def),
value(_value)
{
assert(_def->type == ARRAYINDEXING);
}
bool arrayIsSingleId() {return arraydef->array->type == IDENTIFIER;}
};
class If: public Node {
public:
Tree condition, action;
If(Tree _condition, Tree _action) :
Node(IF), condition(_condition), action(_action) {}
};
class IfElse: public Node {
public:
Tree condition, action, elseaction;
IfElse(Tree _condition, Tree _action, Tree _else) :
Node(IFELSE), condition(_condition), action(_action), elseaction(_else) {}
};
class While: public Node {
public:
Tree condition, action;
While(Tree _condition, Tree _action) :
Node(WHILE), condition(_condition), action(_action) {}
};
class For: public Node {
public:
Identifier *variable;
Tree start, stop, action;
/**
* Id for the loop variable
*/
ObjectId variable_id;
For(Tree _variable, Tree _start, Tree _stop, Tree _action, ObjectId _var_id) :
Node(FOR), variable((Identifier *)_variable),
start(_start), stop(_stop), action(_action), variable_id(_var_id)
{
assert(variable->type == IDENTIFIER);
}
};
class Scope: public Node {
public:
ExpressionList *declarations;
ExpressionList *action;
Scope(Tree _declarations, Tree _action) :
Node(SCOPE),
declarations((ExpressionList *)_declarations),
action((ExpressionList *)_action)
{
assert(_declarations->type == EXPRESSIONLIST);
assert(_action->type == EXPRESSIONLIST);
}
};
class RecordField: public Node {
public:
Tree record;
Identifier *field;
RecordField(Tree _record, Tree _field) :
Node(RECORDFIELD),
record(_record),
field((Identifier *)_field)
{
assert(_field->type == IDENTIFIER);
}
};
class FunctionCall: public Node {
public:
Identifier *function;
ExpressionList *arguments;
FunctionCall(Tree _function, Tree _arguments) :
Node(FUNCTIONCALL),
function((Identifier *)_function),
arguments((ExpressionList *)_arguments)
{
assert(_function->type == IDENTIFIER);
assert(_arguments->type == EXPRESSIONLIST);
}
};
class RecordInstantiation: public Node {
public:
Identifier *type;
ExpressionList *fieldvalues;
RecordInstantiation(Tree _type, Tree _values) :
Node(RECORDINSTANTIATION),
type((Identifier *)_type),
fieldvalues((ExpressionList *)_values)
{
assert(_type->type == IDENTIFIER);
assert(_values->type == EXPRESSIONLIST);
}
};
class TypeDeclaration: public Node {
public:
Identifier *name;
Tree definition;
TypeDeclaration(Tree _name, Tree _definition) :
Node(TYPEDECLARATION), name((Identifier *)_name),
definition(_definition)
{
assert(_name->type == IDENTIFIER);
assert((_definition->type == IDENTIFIER) ||
(_definition->type == ARRAYTYPEDEFINITION) ||
(_definition->type == RECORDTYPEDEFINITION));
}
};
class ArrayTypeDefinition: public Node {
public:
Identifier *elementtype;
ArrayTypeDefinition(Tree _type) : Node(ARRAYTYPEDEFINITION),
elementtype((Identifier *)_type)
{
assert(_type->type == IDENTIFIER);
}
};
class RecordTypeDefinition: public Node {
public:
ExpressionList *fields;
RecordTypeDefinition(Tree _fields) : Node(RECORDTYPEDEFINITION),
fields((ExpressionList *)_fields)
{
assert(_fields->type == EXPRESSIONLIST);
}
};
class ParameterDeclaration: public Node {
public:
Identifier *name, *type;
/**
* Unique within the program tree for all ParameterDeclarations,
* VariableDeclarations, For's and FunctionDeclaration
* Used to share information about the varible between multiple passes
* of the syntax tree
*/
ObjectId id;
ParameterDeclaration(int _id, Tree _name, Tree _type) :
Node(PARAMETERDECLARATION), id(_id),
name((Identifier *)_name), type((Identifier *)_type)
{
assert(_name->type == IDENTIFIER);
assert(_type->type == IDENTIFIER);
}
};
class VariableDeclaration: public Node {
public:
Identifier *name, *type;
Tree value;
/**
* Unique within the program tree for all ParameterDeclarations,
* VariableDeclarations, For's and FunctionDeclaration
* Used to share information about the varible between multiple passes
* of the syntax tree
*/
ObjectId id;
VariableDeclaration(int _id, Tree _name, Tree _value, Tree _type = NULL) :
Node(VARDECLARATION), id(_id), name((Identifier *)_name),
value(_value), type((Identifier *)_type)
{
assert(_name->type == IDENTIFIER);
assert((_type == NULL) || (_type->type == IDENTIFIER));
}
};
class Function: public Node {
public:
Identifier *name, *type;
ExpressionList *parameters;
Tree body;
/**
* Unique within the program tree for all ParameterDeclarations,
* VariableDeclarations, For's and FunctionDeclaration
* Used to share information about the varible between multiple passes
* of the syntax tree
*/
ObjectId id;
Function(int _id, Tree _name, Tree _type, Tree _parameters, Tree _body) :
id(_id), Node(FUNCTION), name((Identifier *)_name),
type((Identifier *)_type),
parameters((ExpressionList *)_parameters),
body(_body)
{
assert(_name->type == IDENTIFIER);
assert((_type == NULL) || (_type->type == IDENTIFIER));
assert(_parameters->type == EXPRESSIONLIST);
for (std::list<Tree>::iterator i = parameters->expressions.begin();
i != parameters->expressions.end(); i++)
assert((*i)->type == PARAMETERDECLARATION);
}
};
/**
* Recursively delete the entire tree
*/
void DestroySyntaxTree(Tree &tree);
}
#endif