-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.h
169 lines (123 loc) · 4.1 KB
/
TreeNode.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
#pragma once
#include <string>
#include <vector>
#include "token.h"
#include "visitor.h"
class TreeNode {
public:
enum Type {
STATEMENT_LIST, OPERATOR, VARIABLE, CONSTANT, ASSIGNMENT, BRANCH
};
virtual ~TreeNode() = default;
virtual double accept(Visitor *visitor) = 0;
[[nodiscard]] virtual bool isExpression() const = 0;
virtual void replaceChild(TreeNode *value, TreeNode *replacement) = 0;
[[nodiscard]] virtual Type getType() const = 0;
};
class StatementListNode : public TreeNode {
public:
std::vector<TreeNode *> statements;
~StatementListNode() override {
for (auto *statement : statements) {
delete statement;
}
}
double accept(Visitor *visitor) override { return visitor->visit(this); };
[[nodiscard]] bool isExpression() const override {
return false;
}
void replaceChild(TreeNode *value, TreeNode *replacement) override;
[[nodiscard]] Type getType() const override {
return STATEMENT_LIST;
}
};
class ExpressionNode : public TreeNode {
public:
[[nodiscard]] bool isExpression() const override {
return true;
}
};
class OperatorNode : public ExpressionNode {
public:
ExpressionNode *left;
ExpressionNode *right;
const Operator *oper;
OperatorNode(Operator *oper, ExpressionNode *left, ExpressionNode *right) :
oper(oper), left(left), right(right) {};
~OperatorNode() override {
delete left;
delete right;
}
double accept(Visitor *visitor) override { return visitor->visit(this); };
void replaceChild(TreeNode *value, TreeNode *replacement) override;
[[nodiscard]] Type getType() const override {
return OPERATOR;
}
};
class VariableNode : public ExpressionNode {
public:
const std::string name;
explicit VariableNode(std::string name) : name(std::move(name)) {};
~VariableNode() override = default;
double accept(Visitor *visitor) override { return visitor->visit(this); };
void replaceChild(TreeNode *value, TreeNode *replacement) override {
throw std::invalid_argument("VariableNode doesn't have any children.");
}
[[nodiscard]] Type getType() const override {
return VARIABLE;
}
};
class ConstantNode : public ExpressionNode {
public:
const Number *value;
explicit ConstantNode(const Number *value) : value(value) {};
~ConstantNode() override {
delete value;
};
double accept(Visitor *visitor) override { return visitor->visit(this); };
void replaceChild(TreeNode *node, TreeNode *replacement) override {
throw std::invalid_argument("ConstantNode doesn't have any children.");
}
[[nodiscard]] Type getType() const override {
return CONSTANT;
}
};
class AssignmentNode : public TreeNode {
public:
VariableNode *variable;
ExpressionNode *expression;
AssignmentNode(VariableNode *variable, ExpressionNode *expression) : variable(variable), expression(expression) {}
~AssignmentNode() override {
delete variable;
delete expression;
};
double accept(Visitor *visitor) override { return visitor->visit(this); };
void replaceChild(TreeNode *value, TreeNode *replacement) override;
[[nodiscard]] bool isExpression() const override {
return false;
}
[[nodiscard]] Type getType() const override {
return ASSIGNMENT;
}
};
class BranchNode : public TreeNode {
public:
ExpressionNode *condition;
StatementListNode *ifTrue;
StatementListNode *ifFalse;
BranchNode(ExpressionNode *condition, StatementListNode *ifTrue, StatementListNode *ifFalse) :
condition(condition), ifTrue(ifTrue), ifFalse(ifFalse) {}
~BranchNode() override {
delete condition;
delete ifTrue;
delete ifFalse;
};
double accept(Visitor *visitor) override { return visitor->visit(this); };
void replaceChild(TreeNode *value, TreeNode *replacement) override;
[[nodiscard]] bool isExpression() const override {
return false;
}
[[nodiscard]] Type getType() const override {
return BRANCH;
}
};