-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractSyntaxTree.hpp
176 lines (158 loc) · 4.19 KB
/
AbstractSyntaxTree.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
#ifndef ABSTRACTSYNTAXTREE_HPP
#define ABSTRACTSYNTAXTREE_HPP
#include "Includes.hpp"
class Identifier;
class Integer;
class String;
class Unary;
class Binary;
class Statement;
class Assignment;
class IfStatement;
class PrintStatement;
class Visitor {
// Base calass for my visitor objects
public:
virtual void visit(Identifier& node) = 0;
virtual void visit(Integer& node) = 0;
virtual void visit(String& node) = 0;
virtual void visit(Unary& node) = 0;
virtual void visit(Binary& node) = 0;
virtual void visit(Statement& node) = 0;
virtual void visit(Assignment& node) = 0;
virtual void visit(IfStatement& node) = 0;
virtual void visit(PrintStatement& node) = 0;
};
class Expression {
// Base class for storing data types.
public:
virtual ~Expression();
virtual void accept(Visitor& visitor) = 0;
virtual string toString() const;
};
class Identifier : public Expression {
private:
string id;
public:
Identifier();
Identifier(string);
void accept(Visitor&) override;
string getID();
string toString() const override;
};
class Integer : public Expression {
private:
int integer;
public:
Integer();
Integer(int);
void accept(Visitor&) override;
int getInteger();
string toString() const override;
};
class String : public Expression {
private:
string string_variable;
public:
String();
String(string);
void accept(Visitor&) override;
string getString();
string toString() const override;
};
class Unary : public Expression {
private:
string unary_operator;
Expression* expression_1;
public:
Unary();
Unary(string, Expression*);
void accept(Visitor&) override;
string toString() const override;
};
class Binary : public Expression {
private:
string binary_operator;
Expression* expression_1;
Expression* expression_2;
public:
Binary();
Binary(string, Expression*, Expression*);
void accept(Visitor&) override;
string getBinaryOperator();
Expression* getExpression1();
Expression* getExpression2();
string toString() const override;
};
class Program {
// Base class for all future nodes
public:
virtual ~Program();
virtual void accept(Visitor& visitor) = 0;
};
class Statement : public Program {
private:
unique_ptr<Statement> statement_1;
unique_ptr<Statement> statement_2;
public:
Statement();
Statement(unique_ptr<Statement>, unique_ptr<Statement>);
void accept(Visitor&) override;
Statement* getStatement1();
Statement* getStatement2();
virtual string toString();
};
class Assignment : public Statement {
private:
Identifier left_hand_side;
Expression* right_hand_side;
public:
Assignment();
Assignment(Identifier, Expression*);
void accept(Visitor&) override;
Identifier getLeftHandSide();
Expression* getRightHandSide();
string toString() override;
};
class IfStatement : public Statement {
private:
Expression* test;
unique_ptr<Statement> then_statement;
unique_ptr<Statement> else_statement;
public:
IfStatement();
IfStatement(Expression*, unique_ptr<Statement>);
IfStatement(Expression*, unique_ptr<Statement>, unique_ptr<Statement>);
void accept(Visitor&) override;
Expression* getTest();
unique_ptr<Statement> getThenStatement();
unique_ptr<Statement> getElseStatement();
string toString() override;
};
class PrintStatement : public Statement {
private:
Expression* expression;
public:
PrintStatement();
PrintStatement(Expression*);
void accept(Visitor&) override;
Expression* getExpression();
string toString() override;
};
class ExecutionVisitor : public Visitor {
private:
unordered_map<string, variant<int, string>> symbolTable;
variant<int, string> last_evaluated_value;
public:
ExecutionVisitor();
void visit(Identifier& node) override;
void visit(Integer& node) override;
void visit(String& node) override;
void visit(Unary& node) override;
void visit(Binary& node) override;
void visit(Statement& node) override;
void visit(Assignment& node) override;
void visit(IfStatement& node) override;
void visit(PrintStatement& node) override;
};
#endif // !ABSTRACTSYNTAXTREE_HPP