-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cpp
300 lines (233 loc) · 6.21 KB
/
ast.cpp
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
#include "ast.h"
#include <iostream>
#include "calculator.h"
// for debug information uncomment
//#define debug
AST::AST() {}
AST::~AST() {}
BinaryNode::BinaryNode(AST* left, AST* right):
AST(),
leftTree(left),
rightTree(right)
{}
BinaryNode::~BinaryNode() {
#ifdef debug
cout << "In BinaryNode destructor" << endl;
#endif
try {
delete leftTree;
} catch (...) {}
try {
delete rightTree;
} catch(...) {}
}
AST* BinaryNode::getLeftSubTree() const {
return leftTree;
}
AST* BinaryNode::getRightSubTree() const {
return rightTree;
}
UnaryNode::UnaryNode(AST* sub):
AST(),
subTree(sub)
{}
UnaryNode::~UnaryNode() {
#ifdef debug
cout << "In UnaryNode destructor" << endl;
#endif
try {
delete subTree;
} catch (...) {}
}
AST* UnaryNode::getSubTree() const {
return subTree;
}
AddNode::AddNode(AST* left, AST* right):
BinaryNode(left,right)
{}
int AddNode::evaluate() {
return getLeftSubTree()->evaluate() + getRightSubTree()->evaluate();
}
string AddNode::compile() {
string line = getLeftSubTree()->compile() + getRightSubTree()->compile();
return line + "\n# Add \n"+
"operator2 := M[sp+0] \n"+
"operator1 := M[sp+1] \n"+
"operator1 := operator1 + operator2 \n"+
"sp := sp + one \n"+
"M[sp+0] := operator1";
}
SubNode::SubNode(AST* left, AST* right):
BinaryNode(left,right)
{}
int SubNode::evaluate() {
return getLeftSubTree()->evaluate() - getRightSubTree()->evaluate();
}
string SubNode::compile() {
string line = getLeftSubTree()->compile() + getRightSubTree()->compile();
return line + "\n# Sub \n"+
"operator2 := M[sp+0] \n"+
"operator1 := M[sp+1] \n"+
"operator1 := operator1 - operator2 \n"+
"sp := sp + one \n"+
"M[sp+0] := operator1";
}
TimesNode::TimesNode(AST* left, AST* right):
BinaryNode(left,right)
{}
int TimesNode::evaluate() {
return getLeftSubTree()->evaluate() * getRightSubTree()->evaluate();
}
string TimesNode::compile() {
string line = getLeftSubTree()->compile() + getRightSubTree()->compile();
return line + "\n# Times \n"+
"operator2 := M[sp+0] \n"+
"operator1 := M[sp+1] \n"+
"operator1 := operator1 * operator2 \n"+
"sp := sp + one \n"+
"M[sp+0] := operator1";
}
DivideNode::DivideNode(AST* left, AST* right):
BinaryNode(left,right)
{}
int DivideNode::evaluate() {
return getLeftSubTree()->evaluate() / getRightSubTree()->evaluate();
}
string DivideNode::compile() {
string line = getLeftSubTree()->compile() + getRightSubTree()->compile();
return line + "\n# Divide \n"+
"operator2 := M[sp+0] \n"+
"operator1 := M[sp+1] \n"+
"operator1 := operator1 / operator2 \n"+
"sp := sp + one \n"+
"M[sp+0] := operator1";
}
ModuleNode::ModuleNode(AST* left, AST* right):
BinaryNode(left,right)
{}
int ModuleNode::evaluate() {
return getLeftSubTree()->evaluate() % getRightSubTree()->evaluate();
}
string ModuleNode::compile() {
string line = getLeftSubTree()->compile() + getRightSubTree()->compile();
return line + "\n# TimesNode \n"+
"operator2 := M[sp+0] \n"+
"operator1 := M[sp+1] \n"+
"operator1 := operator1 % operator2 \n"+
"sp := sp + one \n"+
"M[sp+0] := operator1";
}
NumNode::NumNode(int n) :
AST(),
val(n)
{}
int NumNode::evaluate() {
return val;
}
string NumNode::compile() {
string line = "";
return line + "\n# push("+to_string(val)+") \n"+
"sp := sp - one \n" +
"operator1 := " + to_string(val) + "\n"+
"M[sp+0] := operator1";
}
IdNode::IdNode(std::string var) :
AST(),
var(var)
{}
int IdNode::evaluate() {
if(calc->containVar(var))
return calc->getVar(var);
else
calc->setVar(var, 0);
return 0;
}
string IdNode::compile() {
string line = "";
evaluate();
return line + "\n# IdNode \n"+
"sp := sp - one \n"+
"M[sp+0] := " + var;
}
RecallNode::RecallNode() :
AST()
{}
RecallNode::~RecallNode() {}
int RecallNode::evaluate() {
return calc->recall();
}
string RecallNode::compile() {
string line = "";
return line + "\n# Recall \n"+
"sp := sp + one \n"+
"M[sp+0] := memory";
}
InitVarNode::InitVarNode(string var, AST* sub) : UnaryNode(sub), var(var) {}
InitVarNode::~InitVarNode() {}
int InitVarNode::evaluate(){
calc->setVar(var, getSubTree()->evaluate());
return calc->getVar(var);
}
string InitVarNode::compile() {
evaluate();
string line = getSubTree()->compile();
return line + "\n# InitVarNode \n"+
var + " := M[sp+1]";
}
StoreNode::StoreNode(AST *sub) : UnaryNode(sub) { }
StoreNode::~StoreNode() {}
int StoreNode::evaluate() {
calc->store(getSubTree()->evaluate());
return calc->recall();
}
string StoreNode::compile() {
int result = getSubTree()->evaluate();
calc->store(result);
string line = getSubTree()->compile();
return line + "\n# Store \n"+
"memory := M[sp+0]";
}
ClearNode::ClearNode() :
AST()
{}
ClearNode::~ClearNode() {}
int ClearNode::evaluate() {
return calc->clear();
}
string ClearNode::compile() {
string line="";
return line + "\n# Clear \n"+
"memory := zero \n" +
"sp := sp + one \n"+
"M[sp+0] := memory";
}
PlusNode::PlusNode(AST *sub) : UnaryNode(sub) { }
PlusNode::~PlusNode() {}
int PlusNode::evaluate() {
calc->plus(getSubTree()->evaluate());
return calc->recall();
}
string PlusNode::compile() {
string line = getSubTree()->compile();
int result = getSubTree()->evaluate();
calc->plus(result);
return line + "\n# Memory Plus \n"+
"operator2 := M[sp+0]\n"+
"memory := memory + operator2"+
"M[sp+0] := memory";
}
MinusNode::MinusNode(AST *sub) : UnaryNode(sub) { }
MinusNode::~MinusNode() {}
int MinusNode::evaluate() {
calc->minus(getSubTree()->evaluate());
return calc->recall();
}
string MinusNode::compile() {
string line = getSubTree()->compile();
int result = getSubTree()->evaluate();
calc->minus(result);
return line + "\n# Memory Minus \n"+
"operator2 := M[sp+0]\n"+
"memory := memory - operator2Ρ \n" +
"M[sp+0] := memory";
}