-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.cpp
More file actions
233 lines (210 loc) · 8.21 KB
/
AST.cpp
File metadata and controls
233 lines (210 loc) · 8.21 KB
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
#include "AST.h"
#include <fstream>
const int WIDTH = 500;
const int HEIGHT = 500;
bool canvas[WIDTH][HEIGHT] = {false};
Value ASTNode::eval(SymTable* table) {
if (isLiteral) {
return val;
}
if (root == "VisualizeFinal") {
std::ofstream img("result.ppm");
img << "P3\n" << WIDTH << " " << HEIGHT << "\n255\n";
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (canvas[j][i]) img << "0 0 0 "; // Black
else img << "255 255 255 "; // White
}
img << "\n";
}
img.close();
cout << "Image saved to result.ppm" << endl;
return Value();
}
if (root == "BLOCK") {
if (left != nullptr) left->eval(table);
if (right != nullptr) right->eval(table);
return Value();
}
if (root == "IF") {
if (left->eval(table).boolValue) {
right->eval(table);
}
return Value();
}
if (root == "WHILE") {
while (left->eval(table).boolValue) {
right->eval(table);
}
return Value();
}
if (root == "OTHER") {
return Value(this->type, true); // returns default value for the return type
}
// Leaf node: variable lookup
if (left == nullptr && right == nullptr) {
return table->getValue(root);
}
// ID := expr
if (root == ":=") {
Value rightVal = right->eval(table);
// left->root contains the name of the identifier
table->setValue(left->root, rightVal);
return rightVal;
}
// Print(expr)
if (root == "Print") {
Value v = left->eval(table);
cout << v.toString() << endl;
return v;
}
// exp
if (root == "+") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int" && r.type == "int")
return Value(l.intValue + r.intValue);
if (l.type == "float" && r.type == "float")
return Value(l.floatValue + r.floatValue);
if (l.type == "string" && r.type == "string")
return Value(l.stringValue + r.stringValue);
if (l.type == "com" && r.type == "com")
return Value(l.comValue.real + r.comValue.real, l.comValue.imag + r.comValue.imag);
}
if (root == "-") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int" && r.type == "int") return Value(l.intValue - r.intValue);
if (l.type == "float" && r.type == "float") return Value(l.floatValue - r.floatValue);
if (l.type == "com" && r.type == "com") return Value(l.comValue.real - r.comValue.real, l.comValue.imag - r.comValue.imag);
}
if (root == "*") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int" && r.type == "int") return Value(l.intValue * r.intValue);
if (l.type == "float" && r.type == "float") return Value(l.floatValue * r.floatValue);
if (l.type == "com" && r.type == "com") {
float real = (l.comValue.real * r.comValue.real) - (l.comValue.imag * r.comValue.imag);
float imag = (l.comValue.real * r.comValue.imag) + (l.comValue.imag * r.comValue.real);
return Value(real, imag);
}
}
if (root == "/") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int" && r.type == "int") {
if (r.intValue == 0) { cout << "Runtime Error: Division by zero" << endl; return Value(0); }
return Value(l.intValue / r.intValue);
}
if (l.type == "float" && r.type == "float") {
if (r.floatValue == 0) { cout << "Runtime Error: Division by zero" << endl; return Value(0.0f); }
return Value(l.floatValue / r.floatValue);
}
if (l.type == "com" && r.type == "com") {
float denom = (r.comValue.real * r.comValue.real) + (r.comValue.imag * r.comValue.imag);
if (denom == 0) { cout << "Runtime Error: Division by zero" << endl; return Value(0.0f, 0.0f); }
float real = ((l.comValue.real * r.comValue.real) + (l.comValue.imag * r.comValue.imag)) / denom;
float imag = ((l.comValue.imag * r.comValue.real) - (l.comValue.real * r.comValue.imag)) / denom;
return Value(real, imag);
}
}
if (root == "%") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int" && r.type == "int") {
if (r.intValue == 0) { cout << "Runtime Error: Division by zero" << endl; return Value(0); }
return Value(l.intValue % r.intValue);
}
}
// Bexp
if (root == "<") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int") return Value(l.intValue < r.intValue);
return Value(l.floatValue < r.floatValue);
}
if (root == ">") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int") return Value(l.intValue > r.intValue);
return Value(l.floatValue > r.floatValue);
}
if (root == "<=") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int") return Value(l.intValue <= r.intValue);
return Value(l.floatValue <= r.floatValue);
}
if (root == ">=") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int") return Value(l.intValue >= r.intValue);
return Value(l.floatValue >= r.floatValue);
}
if (root == "==") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int") return Value(l.intValue == r.intValue);
if (l.type == "float") return Value(l.floatValue == r.floatValue);
if (l.type == "bool") return Value(l.boolValue == r.boolValue);
if (l.type == "string") return Value(l.stringValue == r.stringValue);
if (l.type == "com") return Value(l.comValue.real == r.comValue.real && l.comValue.imag == r.comValue.imag);
}
if (root == "!=") {
Value l = left->eval(table);
Value r = right->eval(table);
if (l.type == "int") return Value(l.intValue != r.intValue);
if (l.type == "float") return Value(l.floatValue != r.floatValue);
if (l.type == "bool") return Value(l.boolValue != r.boolValue);
if (l.type == "string") return Value(l.stringValue != r.stringValue);
if (l.type == "com") return Value(l.comValue.real != r.comValue.real || l.comValue.imag != r.comValue.imag);
}
if (root == "&&") {
Value l = left->eval(table);
if (l.type == "bool" && !l.boolValue) return Value(false);
Value r = right->eval(table);
return Value(l.boolValue && r.boolValue);
}
if (root == "||") {
Value l = left->eval(table);
if (l.type == "bool" && l.boolValue) return Value(true);
Value r = right->eval(table);
return Value(l.boolValue || r.boolValue);
}
if (root == "!") {
Value l = left->eval(table);
return Value(!l.boolValue);
}
if (root == "UMINUS") {
Value l = left->eval(table);
if (l.type == "int") return Value(-l.intValue);
if (l.type == "float") return Value(-l.floatValue);
if (l.type == "com") return Value(-l.comValue.real, -l.comValue.imag);
return l;
}
if (root == "MAG") {
Value l = left->eval(table);
return Value((float)sqrt(pow(l.comValue.real, 2) + pow(l.comValue.imag, 2)));
}
if (root == "REAL") {
Value l = left->eval(table);
return Value(l.comValue.real);
}
if (root == "IMAG") {
Value l = left->eval(table);
return Value(l.comValue.imag);
}
if (root == "VisualizePoint") {
Value v = left->eval(table);
if (v.type == "com") {
// map complex coordinates (-2 to 2) to pixel coordinates (0 to 500)
int x = (int)((v.comValue.real + 2.0) * (WIDTH / 4.0));
int y = (int)((v.comValue.imag + 2.0) * (HEIGHT / 4.0));
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) {
canvas[x][y] = true; // black pixel
}
}
return v;
}
return Value();
}