-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast.hpp
649 lines (528 loc) · 19.2 KB
/
ast.hpp
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#ifndef __AST_HPP
#define __AST_HPP
#include <iostream>
#include <list>
#include <vector>
#include <stack>
#include <string>
#include <sstream>
// Enumaration of all base types in the language
typedef enum {bt_integer, bt_boolean, bt_none, bt_object} BaseType;
// Forward declarations of AST Node classes
class ProgramNode;
class ClassNode;
class MethodNode;
class MethodBodyNode;
class ParameterNode;
class DeclarationNode;
class ReturnStatementNode;
class AssignmentNode;
class CallNode;
class IfElseNode;
class WhileNode;
class RepeatNode;
class PrintNode;
class PlusNode;
class MinusNode;
class TimesNode;
class DivideNode;
class LessNode;
class LessEqualNode;
class EqualNode;
class AndNode;
class OrNode;
class NotNode;
class NegationNode;
class MethodCallNode;
class MemberAccessNode;
class VariableNode;
class IntegerLiteralNode;
class BooleanLiteralNode;
class NewNode;
class IntegerTypeNode;
class BooleanTypeNode;
class ObjectTypeNode;
class NoneNode;
class StatementNode;
class ExpressionNode;
class TypeNode;
class IdentifierNode;
class IntegerNode;
// Check order of inclusion
#ifdef YYSTYPE_IS_TRIVIAL
#error Make sure to include this file BEFORE parser.hpp
#endif
// Define YYSTYPE (type of all $$, $N variables) as a union
// of all necessary pointers (including lists). This will
// be used in %type specifiers and in lexer.
typedef union {
ProgramNode* program_ptr;
std::list<ClassNode*>* class_list_ptr;
ClassNode* class_ptr;
IdentifierNode* identifier_ptr;
std::list<DeclarationNode*>* declaration_list_ptr;
std::list<MethodNode*>* method_list_ptr;
MethodNode* method_ptr;
std::list<ParameterNode*>* parameter_list_ptr;
TypeNode* type_ptr;
MethodBodyNode* methodbody_ptr;
std::list<StatementNode*>* statement_list_ptr;
ReturnStatementNode* returnstatement_ptr;
ParameterNode* parameter_ptr;
DeclarationNode* declaration_ptr;
std::list<IdentifierNode*>* identifier_list_ptr;
ExpressionNode* expression_ptr;
AssignmentNode* assignment_ptr;
CallNode* call_ptr;
MethodCallNode* methodcall_ptr;
IfElseNode* ifelse_ptr;
WhileNode* while_ptr;
RepeatNode* repeat_ptr;
PrintNode* print_ptr;
PlusNode* plus_ptr;
MinusNode* minus_ptr;
TimesNode* times_ptr;
DivideNode* divide_ptr;
LessNode* less_ptr;
LessEqualNode* lessequal_ptr;
EqualNode* equal_ptr;
AndNode* and_ptr;
OrNode* or_ptr;
NotNode* not_ptr;
NegationNode* negation_ptr;
std::list<ExpressionNode*>* expression_list_ptr;
MemberAccessNode* memberaccess_ptr;
VariableNode* variable_ptr;
IntegerLiteralNode* integerliteral_ptr;
IntegerNode* integer_ptr;
BooleanLiteralNode* booleanliteral_ptr;
NewNode* new_ptr;
IntegerTypeNode* integertype_ptr;
BooleanTypeNode* booleantype_ptr;
ObjectTypeNode* objecttype_ptr;
NoneNode* none_ptr;
StatementNode* statement_ptr;
char* base_char_ptr;
int base_int;
} astnode_union;
#define YYSTYPE astnode_union
// Define abstract base class for all Visitors
class Visitor {
public:
// Declare all virtual visitor functions (all must be implemented in visitors)
virtual void visitProgramNode(ProgramNode* node) = 0;
virtual void visitClassNode(ClassNode* node) = 0;
virtual void visitMethodNode(MethodNode* node) = 0;
virtual void visitMethodBodyNode(MethodBodyNode* node) = 0;
virtual void visitParameterNode(ParameterNode* node) = 0;
virtual void visitDeclarationNode(DeclarationNode* node) = 0;
virtual void visitReturnStatementNode(ReturnStatementNode* node) = 0;
virtual void visitAssignmentNode(AssignmentNode* node) = 0;
virtual void visitCallNode(CallNode* node) = 0;
virtual void visitIfElseNode(IfElseNode* node) = 0;
virtual void visitWhileNode(WhileNode* node) = 0;
virtual void visitRepeatNode(RepeatNode* node) = 0;
virtual void visitPrintNode(PrintNode* node) = 0;
virtual void visitPlusNode(PlusNode* node) = 0;
virtual void visitMinusNode(MinusNode* node) = 0;
virtual void visitTimesNode(TimesNode* node) = 0;
virtual void visitDivideNode(DivideNode* node) = 0;
virtual void visitLessNode(LessNode* node) = 0;
virtual void visitLessEqualNode(LessEqualNode* node) = 0;
virtual void visitEqualNode(EqualNode* node) = 0;
virtual void visitAndNode(AndNode* node) = 0;
virtual void visitOrNode(OrNode* node) = 0;
virtual void visitNotNode(NotNode* node) = 0;
virtual void visitNegationNode(NegationNode* node) = 0;
virtual void visitMethodCallNode(MethodCallNode* node) = 0;
virtual void visitMemberAccessNode(MemberAccessNode* node) = 0;
virtual void visitVariableNode(VariableNode* node) = 0;
virtual void visitIntegerLiteralNode(IntegerLiteralNode* node) = 0;
virtual void visitBooleanLiteralNode(BooleanLiteralNode* node) = 0;
virtual void visitNewNode(NewNode* node) = 0;
virtual void visitIntegerTypeNode(IntegerTypeNode* node) = 0;
virtual void visitBooleanTypeNode(BooleanTypeNode* node) = 0;
virtual void visitObjectTypeNode(ObjectTypeNode* node) = 0;
virtual void visitNoneNode(NoneNode* node) = 0;
virtual void visitIdentifierNode(IdentifierNode* node) = 0;
virtual void visitIntegerNode(IntegerNode* node) = 0;
};
// Define abstract base class for all AST Nodes
// (this also serves to define the visitable objects)
class ASTNode {
public:
// All AST nodes have a member which stores their basetype (int, bool, none, object)
BaseType basetype;
// All AST nodes have a member which stores the class name, applicable if the base type
// is object. Otherwise this field may be unused
std::string objectClassName;
// All AST nodes provide visit children and accept methods
virtual void visit_children(Visitor* v) = 0;
virtual void accept(Visitor* v) = 0;
};
// Define all abstract AST node classes
class StatementNode : public ASTNode {};
class ExpressionNode : public ASTNode {};
class TypeNode : public ASTNode {};
// Define leaf AST nodes for ids and ints (also used for bools)
// Identifiers have a member name, which is a string
class IdentifierNode : public ASTNode {
public:
std::string name;
virtual void visit_children(Visitor* v) { /* No Children */ }
virtual void accept(Visitor* v) { v->visitIdentifierNode(this); }
IdentifierNode(std::string name) { this->name = name; }
};
// Integers have a member value, which is a C int
class IntegerNode : public ASTNode {
public:
int value;
virtual void visit_children(Visitor* v) {/* No Children */ }
virtual void accept(Visitor* v) { v->visitIntegerNode(this); }
IntegerNode(int value) { this->value = value; }
};
// Define all other AST nodes
// AST Node for Program
class ProgramNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitProgramNode(this); }
std::list<ClassNode*>* class_list;
ProgramNode(std::list<ClassNode*>* class_list);
};
// AST Node for Class
class ClassNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitClassNode(this); }
IdentifierNode* identifier_1;
IdentifierNode* identifier_2;
std::list<DeclarationNode*>* declaration_list;
std::list<MethodNode*>* method_list;
ClassNode(IdentifierNode* identifier_1, IdentifierNode* identifier_2, std::list<DeclarationNode*>* declaration_list, std::list<MethodNode*>* method_list);
};
// AST Node for Method
class MethodNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitMethodNode(this); }
IdentifierNode* identifier;
std::list<ParameterNode*>* parameter_list;
TypeNode* type;
MethodBodyNode* methodbody;
MethodNode(IdentifierNode* identifier, std::list<ParameterNode*>* parameter_list, TypeNode* type, MethodBodyNode* methodbody);
};
// AST Node for MethodBody
class MethodBodyNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitMethodBodyNode(this); }
std::list<DeclarationNode*>* declaration_list;
std::list<StatementNode*>* statement_list;
ReturnStatementNode* returnstatement;
MethodBodyNode(std::list<DeclarationNode*>* declaration_list, std::list<StatementNode*>* statement_list, ReturnStatementNode* returnstatement);
};
// AST Node for Parameter
class ParameterNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitParameterNode(this); }
TypeNode* type;
IdentifierNode* identifier;
ParameterNode(TypeNode* type, IdentifierNode* identifier);
};
// AST Node for Declaration
class DeclarationNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitDeclarationNode(this); }
TypeNode* type;
std::list<IdentifierNode*>* identifier_list;
DeclarationNode(TypeNode* type, std::list<IdentifierNode*>* identifier_list);
};
// AST Node for ReturnStatement
class ReturnStatementNode : public ASTNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitReturnStatementNode(this); }
ExpressionNode* expression;
ReturnStatementNode(ExpressionNode* expression);
};
// AST Node for Assignment
class AssignmentNode : public StatementNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitAssignmentNode(this); }
IdentifierNode* identifier_1;
IdentifierNode* identifier_2;
ExpressionNode* expression;
AssignmentNode(IdentifierNode* identifier_1, IdentifierNode* identifier_2, ExpressionNode* expression);
};
// AST Node for Call
class CallNode : public StatementNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitCallNode(this); }
MethodCallNode* methodcall;
CallNode(MethodCallNode* methodcall);
};
// AST Node for IfElse
class IfElseNode : public StatementNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitIfElseNode(this); }
ExpressionNode* expression;
std::list<StatementNode*>* statement_list_1;
std::list<StatementNode*>* statement_list_2;
IfElseNode(ExpressionNode* expression, std::list<StatementNode*>* statement_list_1, std::list<StatementNode*>* statement_list_2);
};
// AST Node for While
class WhileNode : public StatementNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitWhileNode(this); }
ExpressionNode* expression;
std::list<StatementNode*>* statement_list;
WhileNode(ExpressionNode* expression, std::list<StatementNode*>* statement_list);
};
// AST Node for Repeat
class RepeatNode : public StatementNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitRepeatNode(this); }
std::list<StatementNode*>* statement_list;
ExpressionNode* expression;
RepeatNode(std::list<StatementNode*>* statement_list, ExpressionNode* expression);
};
// AST Node for Print
class PrintNode : public StatementNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitPrintNode(this); }
ExpressionNode* expression;
PrintNode(ExpressionNode* expression);
};
// AST Node for Plus
class PlusNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitPlusNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
PlusNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Minus
class MinusNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitMinusNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
MinusNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Times
class TimesNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitTimesNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
TimesNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Divide
class DivideNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitDivideNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
DivideNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Less
class LessNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitLessNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
LessNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for LessEqual
class LessEqualNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitLessEqualNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
LessEqualNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Equal
class EqualNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitEqualNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
EqualNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for And
class AndNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitAndNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
AndNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Or
class OrNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitOrNode(this); }
ExpressionNode* expression_1;
ExpressionNode* expression_2;
OrNode(ExpressionNode* expression_1, ExpressionNode* expression_2);
};
// AST Node for Not
class NotNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitNotNode(this); }
ExpressionNode* expression;
NotNode(ExpressionNode* expression);
};
// AST Node for Negation
class NegationNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitNegationNode(this); }
ExpressionNode* expression;
NegationNode(ExpressionNode* expression);
};
// AST Node for MethodCall
class MethodCallNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitMethodCallNode(this); }
IdentifierNode* identifier_1;
IdentifierNode* identifier_2;
std::list<ExpressionNode*>* expression_list;
MethodCallNode(IdentifierNode* identifier_1, IdentifierNode* identifier_2, std::list<ExpressionNode*>* expression_list);
};
// AST Node for MemberAccess
class MemberAccessNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitMemberAccessNode(this); }
IdentifierNode* identifier_1;
IdentifierNode* identifier_2;
MemberAccessNode(IdentifierNode* identifier_1, IdentifierNode* identifier_2);
};
// AST Node for Variable
class VariableNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitVariableNode(this); }
IdentifierNode* identifier;
VariableNode(IdentifierNode* identifier);
};
// AST Node for IntegerLiteral
class IntegerLiteralNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitIntegerLiteralNode(this); }
IntegerNode* integer;
IntegerLiteralNode(IntegerNode* integer);
};
// AST Node for BooleanLiteral
class BooleanLiteralNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitBooleanLiteralNode(this); }
IntegerNode* integer;
BooleanLiteralNode(IntegerNode* integer);
};
// AST Node for New
class NewNode : public ExpressionNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitNewNode(this); }
IdentifierNode* identifier;
std::list<ExpressionNode*>* expression_list;
NewNode(IdentifierNode* identifier, std::list<ExpressionNode*>* expression_list);
};
// AST Node for IntegerType
class IntegerTypeNode : public TypeNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitIntegerTypeNode(this); }
};
// AST Node for BooleanType
class BooleanTypeNode : public TypeNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitBooleanTypeNode(this); }
};
// AST Node for ObjectType
class ObjectTypeNode : public TypeNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitObjectTypeNode(this); }
IdentifierNode* identifier;
ObjectTypeNode(IdentifierNode* identifier);
};
// AST Node for None
class NoneNode : public TypeNode {
public:
virtual void visit_children(Visitor* v);
virtual void accept(Visitor* v) { v->visitNoneNode(this); }
};
// Define the provided Print visitor, which will print the AST,
// this is an example of a concrete visitor which visit the tree
class Print : public Visitor {
private:
std::vector<std::string>* elements;
std::stack<std::vector<std::string>*> stack;
unsigned int indent;
void pushLevel(std::string, bool);
void addElement(std::string);
void popLevel(bool, bool);
public:
virtual void visitProgramNode(ProgramNode* node);
virtual void visitClassNode(ClassNode* node);
virtual void visitMethodNode(MethodNode* node);
virtual void visitMethodBodyNode(MethodBodyNode* node);
virtual void visitParameterNode(ParameterNode* node);
virtual void visitDeclarationNode(DeclarationNode* node);
virtual void visitReturnStatementNode(ReturnStatementNode* node);
virtual void visitAssignmentNode(AssignmentNode* node);
virtual void visitCallNode(CallNode* node);
virtual void visitIfElseNode(IfElseNode* node);
virtual void visitWhileNode(WhileNode* node);
virtual void visitRepeatNode(RepeatNode* node);
virtual void visitPrintNode(PrintNode* node);
virtual void visitPlusNode(PlusNode* node);
virtual void visitMinusNode(MinusNode* node);
virtual void visitTimesNode(TimesNode* node);
virtual void visitDivideNode(DivideNode* node);
virtual void visitLessNode(LessNode* node);
virtual void visitLessEqualNode(LessEqualNode* node);
virtual void visitEqualNode(EqualNode* node);
virtual void visitAndNode(AndNode* node);
virtual void visitOrNode(OrNode* node);
virtual void visitNotNode(NotNode* node);
virtual void visitNegationNode(NegationNode* node);
virtual void visitMethodCallNode(MethodCallNode* node);
virtual void visitMemberAccessNode(MemberAccessNode* node);
virtual void visitVariableNode(VariableNode* node);
virtual void visitIntegerLiteralNode(IntegerLiteralNode* node);
virtual void visitBooleanLiteralNode(BooleanLiteralNode* node);
virtual void visitNewNode(NewNode* node);
virtual void visitIntegerTypeNode(IntegerTypeNode* node);
virtual void visitBooleanTypeNode(BooleanTypeNode* node);
virtual void visitObjectTypeNode(ObjectTypeNode* node);
virtual void visitNoneNode(NoneNode* node);
virtual void visitIdentifierNode(IdentifierNode* node);
virtual void visitIntegerNode(IntegerNode* node);
};
#endif