-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstantFolder.cpp
262 lines (252 loc) · 8.14 KB
/
ConstantFolder.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
#include "ConstantFolder.h"
#include "Utils.h"
void ConstantFolder::Fold(std::vector<Stmt *> &stmts)
{
for (auto &s : stmts)
s = FoldStmt(s);
}
Stmt *ConstantFolder::FoldStmt(Stmt *stmt)
{
switch (stmt->type)
{
case AstType::RETURN:
return FoldReturnStmt((ReturnStmt *)stmt);
case AstType::EXPR:
return FoldExprStmt((ExprStmt *)stmt);
case AstType::SCOPE:
return FoldScopeStmt((ScopeStmt *)stmt);
case AstType::IF:
return FoldIfStmt((IfStmt *)stmt);
case AstType::WHILE:
return FoldWhileStmt((WhileStmt *)stmt);
case AstType::STRUCT:
return FoldStructStmt((StructStmt *)stmt);
default:
return stmt;
}
}
Stmt *ConstantFolder::FoldExprStmt(ExprStmt *stmt)
{
stmt->expr = FoldExpr(stmt->expr);
return stmt;
}
Stmt *ConstantFolder::FoldIfStmt(IfStmt *stmt)
{
stmt->condition = FoldExpr(stmt->condition);
stmt->thenBranch = FoldStmt(stmt->thenBranch);
if (stmt->elseBranch)
stmt->elseBranch = FoldStmt(stmt->elseBranch);
if (stmt->condition->type == AstType::BOOL)
{
if (((BoolExpr *)stmt->condition)->value == true)
return stmt->thenBranch;
else
return stmt->elseBranch;
}
return stmt;
}
Stmt *ConstantFolder::FoldScopeStmt(ScopeStmt *stmt)
{
for (auto &s : stmt->stmts)
s = FoldStmt(s);
return stmt;
}
Stmt *ConstantFolder::FoldWhileStmt(WhileStmt *stmt)
{
stmt->condition = FoldExpr(stmt->condition);
stmt->body = FoldStmt(stmt->body);
return stmt;
}
Stmt *ConstantFolder::FoldReturnStmt(ReturnStmt *stmt)
{
stmt->expr = FoldExpr(stmt->expr);
return stmt;
}
Stmt *ConstantFolder::FoldStructStmt(StructStmt *stmt)
{
stmt->body = (StructExpr *)FoldExpr(stmt->body);
return stmt;
}
Expr *ConstantFolder::FoldExpr(Expr *expr)
{
switch (expr->type)
{
case AstType::NUM:
return FoldNumExpr((NumExpr *)expr);
case AstType::STR:
return FoldStrExpr((StrExpr *)expr);
case AstType::BOOL:
return FoldBoolExpr((BoolExpr *)expr);
case AstType::NIL:
return FoldNilExpr((NilExpr *)expr);
case AstType::IDENTIFIER:
return FoldIdentifierExpr((IdentifierExpr *)expr);
case AstType::GROUP:
return FoldGroupExpr((GroupExpr *)expr);
case AstType::ARRAY:
return FoldArrayExpr((ArrayExpr *)expr);
case AstType::INDEX:
return FoldIndexExpr((IndexExpr *)expr);
case AstType::UNARY:
return FoldUnaryExpr((UnaryExpr *)expr);
case AstType::BINARY:
return FoldBinaryExpr((BinaryExpr *)expr);
case AstType::FUNCTION_CALL:
return FoldFunctionCallExpr((FunctionCallExpr *)expr);
case AstType::STRUCT_CALL:
return FoldStructCallExpr((StructCallExpr *)expr);
case AstType::REF:
return FoldRefExpr((RefExpr *)expr);
case AstType::FUNCTION:
return FoldFunctionExpr((FunctionExpr *)expr);
case AstType::STRUCT:
return FoldStructExpr((StructExpr *)expr);
default:
return expr;
}
}
Expr *ConstantFolder::FoldBinaryExpr(BinaryExpr *expr)
{
expr->left = FoldExpr(expr->left);
expr->right = FoldExpr(expr->right);
return ConstantFold(expr);
}
Expr *ConstantFolder::FoldNumExpr(NumExpr *expr)
{
return expr;
}
Expr *ConstantFolder::FoldBoolExpr(BoolExpr *expr)
{
return expr;
}
Expr *ConstantFolder::FoldUnaryExpr(UnaryExpr *expr)
{
expr->right = FoldExpr(expr->right);
return ConstantFold(expr);
}
Expr *ConstantFolder::FoldStrExpr(StrExpr *expr)
{
return expr;
}
Expr *ConstantFolder::FoldNilExpr(NilExpr *expr)
{
return expr;
}
Expr *ConstantFolder::FoldGroupExpr(GroupExpr *expr)
{
return FoldExpr(expr->expr);
}
Expr *ConstantFolder::FoldArrayExpr(ArrayExpr *expr)
{
for (auto &e : expr->elements)
e = FoldExpr(e);
return expr;
}
Expr *ConstantFolder::FoldIndexExpr(IndexExpr *expr)
{
expr->ds = FoldExpr(expr->ds);
expr->index = FoldExpr(expr->index);
return expr;
}
Expr *ConstantFolder::FoldIdentifierExpr(IdentifierExpr *expr)
{
return expr;
}
Expr *ConstantFolder::FoldFunctionExpr(FunctionExpr *expr)
{
expr->body = (ScopeStmt *)FoldScopeStmt(expr->body);
return expr;
}
Expr *ConstantFolder::FoldFunctionCallExpr(FunctionCallExpr *expr)
{
expr->name = FoldExpr(expr->name);
for (auto &e : expr->arguments)
e = FoldExpr(e);
return expr;
}
Expr *ConstantFolder::FoldStructCallExpr(StructCallExpr *expr)
{
expr->callee = FoldExpr(expr->callee);
expr->callMember = FoldExpr(expr->callMember);
return expr;
}
Expr *ConstantFolder::FoldRefExpr(RefExpr *expr)
{
expr->refExpr = FoldExpr(expr->refExpr);
return expr;
}
Expr *ConstantFolder::FoldStructExpr(StructExpr *expr)
{
for (auto &[k, v] : expr->members)
v = FoldExpr(v);
return expr;
}
Expr *ConstantFolder::ConstantFold(Expr *expr)
{
if (expr->type == AstType::BINARY)
{
auto infix = (BinaryExpr *)expr;
if (infix->left->type == AstType::NUM && infix->right->type == AstType::NUM)
{
Expr *newExpr = nullptr;
if (infix->op == "+")
newExpr = new NumExpr(((NumExpr *)infix->left)->value + ((NumExpr *)infix->right)->value);
else if (infix->op == "-")
newExpr = new NumExpr(((NumExpr *)infix->left)->value - ((NumExpr *)infix->right)->value);
else if (infix->op == "*")
newExpr = new NumExpr(((NumExpr *)infix->left)->value * ((NumExpr *)infix->right)->value);
else if (infix->op == "/")
newExpr = new NumExpr(((NumExpr *)infix->left)->value / ((NumExpr *)infix->right)->value);
else if (infix->op == "&")
newExpr = new NumExpr((double)((int64_t)((NumExpr *)infix->left)->value & (int64_t)((NumExpr *)infix->right)->value));
else if (infix->op == "|")
newExpr = new NumExpr((double)((int64_t)((NumExpr *)infix->left)->value | (int64_t)((NumExpr *)infix->right)->value));
else if (infix->op == "^")
newExpr = new NumExpr((double)((int64_t)((NumExpr *)infix->left)->value ^ (int64_t)((NumExpr *)infix->right)->value));
else if (infix->op == "==")
newExpr = new BoolExpr(((NumExpr *)infix->left)->value == ((NumExpr *)infix->right)->value);
else if (infix->op == "!=")
newExpr = new BoolExpr(((NumExpr *)infix->left)->value != ((NumExpr *)infix->right)->value);
else if (infix->op == ">")
newExpr = new BoolExpr(((NumExpr *)infix->left)->value > ((NumExpr *)infix->right)->value);
else if (infix->op == ">=")
newExpr = new BoolExpr(((NumExpr *)infix->left)->value >= ((NumExpr *)infix->right)->value);
else if (infix->op == "<")
newExpr = new BoolExpr(((NumExpr *)infix->left)->value < ((NumExpr *)infix->right)->value);
else if (infix->op == "<=")
newExpr = new BoolExpr(((NumExpr *)infix->left)->value <= ((NumExpr *)infix->right)->value);
SAFE_DELETE(infix);
return newExpr;
}
else if (infix->left->type == AstType::STR && infix->right->type == AstType::STR)
{
auto strExpr = new StrExpr(((StrExpr *)infix->left)->value + ((StrExpr *)infix->right)->value);
SAFE_DELETE(infix);
return strExpr;
}
}
else if (expr->type == AstType::UNARY)
{
auto unary = (UnaryExpr *)expr;
if (unary->right->type == AstType::NUM && unary->op == "-")
{
auto numExpr = new NumExpr(-((NumExpr *)unary->right)->value);
SAFE_DELETE(unary);
return numExpr;
}
else if (unary->right->type == AstType::BOOL && unary->op == "not")
{
auto boolExpr = new BoolExpr(!((BoolExpr *)unary->right)->value);
SAFE_DELETE(unary);
return boolExpr;
}
else if (unary->right->type == AstType::NUM && unary->op == "~")
{
auto v = ~(int64_t)((NumExpr *)unary->right)->value;
auto numExpr = new NumExpr((double)v);
SAFE_DELETE(unary);
return numExpr;
}
}
return expr;
}