Repository navigation
Expand file tree
/
Copy pathExprEval.h
More file actions
509 lines (503 loc) · 17.2 KB
/
Copy pathExprEval.h
File metadata and controls
509 lines (503 loc) · 17.2 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#pragma once
#include <stack>
#include <variant>
#include "Node.h"
class ExprEval
{
public:
enum OPERATORS {
//bi-operand
OP_ADD = 0,
OP_SUB,
OP_MULT,
OP_DIV,
OP_REM,
OP_LAND,
OP_LOR,
OP_EQ,
OP_NE,
OP_LT,
OP_GT,
OP_LE,
OP_GE,
OP_ASSIGN,
OP_TRI,
//mono operand
OP_NEG,
OP_NOT,
OP_LNOT,
OP_ADDRESSOF,
OP_POINTERTO,
OP_TYPECAST,
//helpers that come in pairs
OP_LB,
OP_TRI_L,
OP_INDEX_L,
OP_RB,
OP_TRI_R,
OP_INDEX_R,
};
const int priority[21] = { 4,4,3,3,3,11,12,7,7,6,6,6,6,14,13,2,2,2,2,2,2 };
const int rtl[21] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1 };
std::stack<std::variant<std::shared_ptr<Expression>, OPERATORS>> tmp;
std::stack<OPERATORS> oppr;
std::stack<std::pair<int,int>> op_offset;
std::stack<std::shared_ptr<Expression>> tri_ops;
std::stack<std::shared_ptr<Type>> casts;
int brackets = 0;
inline bool is_operator(OPERATORS op) {
return is_biop(op) || is_moop(op);
}
inline bool is_biop(OPERATORS op) {
return op < OP_NEG;
}
inline bool is_moop(OPERATORS op) {
return op >= OP_NEG && op < OP_LB;
}
inline bool is_helper_l(OPERATORS op) {
return op >= OP_LB && op < OP_RB;
}
inline bool is_helper_r(OPERATORS op) {
return op >= OP_RB;
}
inline bool is_helper(OPERATORS op) {
return is_helper_l(op) || is_helper_r(op);
}
inline Expression::EXPRESSION_TYPE translate(OPERATORS op) {
switch (op) {
case OP_ADD:return Expression::EXPRESSION_TYPE::EXP_ADD;
case OP_SUB:return Expression::EXPRESSION_TYPE::EXP_SUB;
case OP_MULT:return Expression::EXPRESSION_TYPE::EXP_MULT;
case OP_DIV:return Expression::EXPRESSION_TYPE::EXP_DIV;
case OP_REM:return Expression::EXPRESSION_TYPE::EXP_REM;
case OP_NEG:return Expression::EXPRESSION_TYPE::EXP_NEG;
case OP_NOT:return Expression::EXPRESSION_TYPE::EXP_NOT;
case OP_LNOT:return Expression::EXPRESSION_TYPE::EXP_LNOT;
case OP_LAND:return Expression::EXPRESSION_TYPE::EXP_LAND;
case OP_LOR:return Expression::EXPRESSION_TYPE::EXP_LOR;
case OP_EQ:return Expression::EXPRESSION_TYPE::EXP_EQ;
case OP_NE:return Expression::EXPRESSION_TYPE::EXP_NE;
case OP_LT:return Expression::EXPRESSION_TYPE::EXP_LT;
case OP_GT:return Expression::EXPRESSION_TYPE::EXP_GT;
case OP_LE:return Expression::EXPRESSION_TYPE::EXP_LE;
case OP_GE:return Expression::EXPRESSION_TYPE::EXP_GE;
case OP_ASSIGN:return Expression::EXPRESSION_TYPE::EXP_ASSIGN;
case OP_TRI:return Expression::EXPRESSION_TYPE::EXP_TRI;
case OP_ADDRESSOF:return Expression::EXPRESSION_TYPE::EXP_ADDRESSOF;
case OP_POINTERTO:return Expression::EXPRESSION_TYPE::EXP_POINTER_WRAPPER;
case OP_TYPECAST:return Expression::EXPRESSION_TYPE::EXP_TYPE_CAST;
default:
std::cerr << "Some unexpected error came up." << std::endl;
exit(2);
}
return Expression::EXPRESSION_TYPE::EXP_NUMERIC;
}
inline std::shared_ptr<Type> can_calc1(std::shared_ptr<Type> type1, Expression::EXPRESSION_TYPE op) {
switch (op)
{
case Expression::EXP_NEG:
case Expression::EXP_NOT:
case Expression::EXP_LNOT:
if (type1->kind == TYPE_INT)return TypeFactory::int_type();
return nullptr;
case Expression::EXP_ADDRESSOF:
return TypeFactory::pointer_of(type1);
case Expression::EXP_POINTER_WRAPPER:
if (type1->kind == TYPE_POINTER)return type1->point_to;
return nullptr;
case Expression::EXP_TYPE_CAST:
{
if (casts.empty()) {
std::cerr << "Some unexpected error came up." << std::endl;
exit(2);
}
auto t = casts.top();
casts.pop();
return t;
}
default:
return nullptr;
}
}
inline std::shared_ptr<Type> can_calc(std::shared_ptr<Type> type1, std::shared_ptr<Type> type2, Expression::EXPRESSION_TYPE op) {
if (type1->kind == TYPE_ARRAY || type2->kind == TYPE_ARRAY)return nullptr;
switch (op)
{
case Expression::EXPRESSION_TYPE::EXP_ADD:
if (type1->kind == TYPE_INT && type2->kind == TYPE_INT)return TypeFactory::int_type();
if (type1->kind == TYPE_POINTER && type2->kind == TYPE_INT)return type1;
if (type1->kind == TYPE_INT && type2->kind == TYPE_POINTER)return type2;
return nullptr;
break;
case Expression::EXPRESSION_TYPE::EXP_SUB:
if (type1->kind == TYPE_INT && type2->kind == TYPE_INT)return TypeFactory::int_type();
if (type1->kind == TYPE_POINTER && type2->kind == TYPE_INT)return type1;
if (type1->kind == TYPE_POINTER && type2->kind == TYPE_POINTER && *type1->point_to==*type2->point_to)return TypeFactory::int_type();
return nullptr;
break;
case Expression::EXPRESSION_TYPE::EXP_MULT:
case Expression::EXPRESSION_TYPE::EXP_DIV:
case Expression::EXPRESSION_TYPE::EXP_REM:
case Expression::EXPRESSION_TYPE::EXP_LAND:
case Expression::EXPRESSION_TYPE::EXP_LOR:
case Expression::EXPRESSION_TYPE::EXP_LT:
case Expression::EXPRESSION_TYPE::EXP_GT:
case Expression::EXPRESSION_TYPE::EXP_LE:
case Expression::EXPRESSION_TYPE::EXP_GE:
if (type1->kind == TYPE_INT && type2->kind == TYPE_INT)return TypeFactory::int_type();
else return nullptr;
case Expression::EXPRESSION_TYPE::EXP_EQ:
case Expression::EXPRESSION_TYPE::EXP_NE:
if (*type1 == *type2)return TypeFactory::int_type();
else return nullptr;
case Expression::EXPRESSION_TYPE::EXP_ASSIGN:
case Expression::EXPRESSION_TYPE::EXP_TRI:
if (*type1 == *type2)return type1;
else return nullptr;
default:
return nullptr;
}
}
inline std::shared_ptr<Expression> merge_last_expression() {
if (tmp.top().index() != 0)return nullptr;
auto exp = std::get<0>(tmp.top());
tmp.pop();
if (tmp.top().index() != 1)return nullptr;
auto op = std::get<1>(tmp.top());
if (!is_operator(op))return nullptr;
tmp.pop(); oppr.pop(); auto o = op_offset.top(); op_offset.pop();
auto calc = std::make_shared<Expression>(translate(op));
if (is_moop(op)) {
calc->op = exp;
auto rt = can_calc1(calc->op->type, calc->expr_type);
if (!rt) {
std::cerr << "Parse error at line " << o.first << std::endl;
std::cerr << ErrorParser::lines[o.first- 1] << std::endl;
std::cerr << std::string(o.second, ' ') << "^" << std::endl;
std::cerr << "Operand type mismatches." << std::endl;
exit(1);
}
if (op == OP_ADDRESSOF && (!calc->op->is_left)) {
std::cerr << "Parse error at line " << o.first << std::endl;
std::cerr << ErrorParser::lines[o.first - 1] << std::endl;
std::cerr << std::string(o.second, ' ') << "^" << std::endl;
std::cerr << "Cannot get the address of non l-values." << std::endl;
exit(1);
}
calc->type = rt;
if (calc->op->is_const) {
calc->is_const = true;
switch (op) {
case OP_NEG:
calc->val = -calc->op->val;
break;
case OP_NOT:
calc->val = ~calc->op->val;
break;
case OP_LNOT:
calc->val = !calc->op->val;
break;
default:
calc->is_const = false;
}
}
return calc;
}
else {
if (tmp.top().index() != 0)return nullptr;
auto exp1 = std::get<0>(tmp.top());
tmp.pop();
bool t = false;
if (calc->expr_type == calc->EXP_TRI) {
t = true;
if (exp1->type->kind != TYPE_INT) {
std::cerr << "Parse error at line " << o.first << std::endl;
std::cerr << ErrorParser::lines[o.first - 1] << std::endl;
std::cerr << std::string(o.second, ' ') << "^" << std::endl;
std::cerr << "Condition must be integers." << std::endl;
exit(1);
}
calc->op = tri_ops.top();
tri_ops.pop();
calc->op2 = exp;
calc->cond = exp1;
}
else {
calc->op = exp1;
calc->op2 = exp;
}
auto rt = can_calc(calc->op->type, calc->op2->type, calc->expr_type);
if (!rt) {
std::cerr << "Parse error at line " << o.first << std::endl;
std::cerr << ErrorParser::lines[o.first - 1] << std::endl;
std::cerr << std::string(o.second, ' ') << "^" << std::endl;
std::cerr << "Operand type mismatches." << std::endl;
exit(1);
}
if ((calc->expr_type == Expression::EXP_ASSIGN) && (!calc->op->is_left))
{
std::cerr << "Parse error at line " << o.first << std::endl;
std::cerr << ErrorParser::lines[o.first - 1] << std::endl;
std::cerr << std::string(o.second, ' ') << "^" << std::endl;
std::cerr << "Cannot assign to non l-values." << std::endl;
exit(1);
}
calc->type = rt;
if (calc->op->type->kind == TYPE_INT && calc->op2->type->kind == TYPE_INT) {
if (op == OP_ADD) {
if (calc->op->is_const && !calc->op2->is_const) {
auto t = calc->op;
calc->op = calc->op2;
calc->op2 = t;
}
if (calc->op->expr_type == calc->op->EXP_ADD) {
if (calc->op2->is_const && calc->op->op2->is_const) {
auto merged = std::make_shared<Expression>(Expression::EXP_ADD);
merged->op = calc->op->op2;
merged->op2 = calc->op2;
merged->is_const = true;
merged->val = merged->op->val + merged->op2->val;
calc->op->op2 = merged;
calc = calc->op;
}
if ((!calc->op2->is_const) && calc->op->op2->is_const) {
auto tmp = calc->op2;
calc->op2 = calc->op->op2;
calc->op->op2 = tmp;
}
}
}
if (op == OP_SUB) {
if (calc->op->is_const && !calc->op2->is_const) {
auto nop2 = std::make_shared<Expression>(Expression::EXP_NEG);
nop2->op = calc->op2;
calc->expr_type = calc->EXP_ADD;
calc->op2 = calc->op;
calc->op = nop2;
}
if (calc->op->expr_type == calc->op->EXP_ADD) {
if (calc->op2->is_const && calc->op->op2->is_const) {
auto merged = std::make_shared<Expression>(Expression::EXP_SUB);
merged->op = calc->op->op2;
merged->op2 = calc->op2;
merged->is_const = true;
merged->val = merged->op->val - merged->op2->val;
calc->expr_type = calc->EXP_ADD;
calc->op->op2 = merged;
calc = calc->op;
}
if ((!calc->op2->is_const) && calc->op->op2->is_const) {
auto tmp = calc->op2;
calc->op2 = calc->op->op2;
calc->op->op2 = tmp;
calc->op->expr_type = calc->op->EXP_SUB;
calc->expr_type = calc->op->EXP_ADD;
}
}
}
}
if (calc->op->is_const && calc->op2->is_const) {
calc->is_const = true;
switch (op) {
case OP_ADD:
calc->val = calc->op->val + calc->op2->val;
break;
case OP_SUB:
calc->val = calc->op->val - calc->op2->val;
break;
case OP_MULT:
calc->val = calc->op->val * calc->op2->val;
break;
case OP_DIV:
calc->val = calc->op->val / calc->op2->val;
break;
case OP_REM:
calc->val = calc->op->val % calc->op2->val;
break;
case OP_LAND:
calc->val = calc->op->val && calc->op2->val;
break;
case OP_LOR:
calc->val = calc->op->val || calc->op2->val;
break;
case OP_EQ:
calc->val = calc->op->val == calc->op2->val;
break;
case OP_NE:
calc->val = calc->op->val != calc->op2->val;
break;
case OP_LT:
calc->val = calc->op->val < calc->op2->val;
break;
case OP_GT:
calc->val = calc->op->val > calc->op2->val;
break;
case OP_LE:
calc->val = calc->op->val <= calc->op2->val;
break;
case OP_GE:
calc->val = calc->op->val >= calc->op2->val;
break;
case OP_TRI:
if (calc->cond->is_const)
calc->val = calc->cond->val ? calc->op->val : calc->op2->val;
else
calc->is_const = false;
break;
default:
calc->is_const = false;
}
}
return calc;
}
}
inline bool push_operand(std::shared_ptr<Expression> v) {
if (tmp.empty()) {
tmp.push(v);
return true;
}
if (tmp.top().index() == 0)return false;
tmp.push(v);
return true;
}
inline bool push_operator(OPERATORS op,std::pair<int,int> debug_info) {
if (is_biop(op) && (tmp.empty() || tmp.top().index() == 1)) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "You cannot expect a binary operator to appear in the beginning of an expression." << std::endl;
exit(1);
}
if (is_operator(op)) {
if (oppr.empty()) {
tmp.push(op);
oppr.push(op);
op_offset.push(debug_info);
return true;
}
/*if (is_operator(oppr.top())) {
std::cout << op << " " << oppr.top() << std::endl;
std::cout << priority[op] << " " << priority[oppr.top()] << std::endl;
}*/
if (is_operator(oppr.top()) &&(priority[op] > priority[oppr.top()] || (priority[op] == priority[oppr.top()] && op < OP_ASSIGN))) {
auto exp = merge_last_expression();
return push_operand(exp) && push_operator(op,debug_info);
}
else {
tmp.push(op);
oppr.push(op);
op_offset.push(debug_info);
return true;
}
}
else {//is helper
if (is_helper_l(op)) {
//simply push;
tmp.push(op); oppr.push(op); op_offset.push(debug_info); return true;
}
else {
if (tmp.top().index() == 1) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "Expected operand here." << std::endl;
exit(1);
}
auto exp = std::get<0>(tmp.top());
tmp.pop();
while (!tmp.empty() && (tmp.top().index() != 1 || std::get<1>(tmp.top()) != op - 3)) {
if (!push_operand(exp)) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "Error occurred when parsing the expression." << std::endl;
exit(1);
}
exp = merge_last_expression();
if (!exp) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "Error occurred when parsing the expression." << std::endl;
exit(1);
}
}
if (tmp.empty() || (tmp.top().index() != 1)) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "Error occurred when parsing the expression." << std::endl;
exit(1);
}
tmp.pop(); oppr.pop();
op_offset.pop();
if (op == OP_RB) {
return push_operand(exp);
}
else if (op == OP_TRI_R) {
//std::cout << "PUSH" << std::endl;
tri_ops.push(exp);
return push_operator(OP_TRI,debug_info);
}
else if (op== OP_INDEX_R) {
auto r = std::make_shared<Expression>(Expression::EXP_ARRAY_WRAPPER);
if (tmp.top().index() == 1) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "cannot find array name." << std::endl;
exit(1);
}
r->op = std::get<0>(tmp.top());
tmp.pop();
r->op2 = exp;
if (exp->type->kind != TYPE_INT) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "Index must be integer." << std::endl;
exit(1);
}
if (r->op->type->kind != TYPE_ARRAY && r->op->type->kind != TYPE_POINTER) {
std::cerr << "Parse error at line " << debug_info.first << std::endl;
std::cerr << ErrorParser::lines[debug_info.first - 1] << std::endl;
std::cerr << std::string(debug_info.second, ' ') << "^" << std::endl;
std::cerr << "Operand of index operator must be array or pointer." << std::endl;
exit(1);
}
r->type = r->op->type->point_to;
if (r->type->kind == TYPE_ARRAY)
r->is_left = false;
else
r->is_left = true;
if (r->op->expr_type == r->op->EXP_ARRAY_WRAPPER && r->op->op2->is_const && r->op2->is_const && r->op->op->type->kind==TYPE_ARRAY) {
int of = r->op2->val + r->op->op2->val * r->op->type->arrlenLen;
r->op->op2 = std::make_shared<Expression>(Expression::EXP_NUMERIC);
r->op->op2->val = 0;
r->op2 = std::make_shared<Expression>(Expression::EXP_NUMERIC);
r->op2->val = of;
}
return push_operand(r);
}
}
}
return false;
}
inline std::shared_ptr<Expression> eval() {
std::shared_ptr<Expression> exp1;
std::shared_ptr<Expression> exp2;
if (tmp.empty() || tmp.top().index() == 1)return nullptr;
while (tmp.size()>1) {
auto e=merge_last_expression();
if (!push_operand(e))return nullptr;
}
if (!tmp.empty())
return std::get<0>(tmp.top());
else return nullptr;
}
inline bool is_neg_not_sub() {
if (tmp.empty())return true;
if (tmp.top().index() == 0)return false;
return true;
}
};