-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression_simplify.cpp
140 lines (103 loc) · 2.58 KB
/
Expression_simplify.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
#include "Expression_Internal.h"
using namespace Expression_Internal;
ExpressionPtr SimplifyVisitor::visit(const UnaryExpression &e) {
// e's children have been simplified at this point.
return e.simplify_me();
}
ExpressionPtr SimplifyVisitor::visit(const BinaryExpression &e) {
// e's children have been simplified at this point.
return e.simplify_me();
}
#pragma mark - simplify
ExpressionPtr Expression::simplify() {
SimplifyVisitor v;
return accept(v);
}
#if 0
ExpressionPtr Expression::simplify() {
return this;
}
ExpressionPtr UnaryExpression::simplify() {
std::transform(_children.begin(), _children.end(), _children.begin(), [](ExpressionPtr e){
return e->simplify();
});
return simplify_me();
}
ExpressionPtr BinaryExpression::simplify() {
std::transform(_children.begin(), _children.end(), _children.begin(), [](ExpressionPtr e){
return e->simplify();
});
return simplify_me();
}
ExpressionPtr VectorExpression::simplify() {
std::transform(_children.begin(), _children.end(), _children.begin(), [](ExpressionPtr e){
return e->simplify();
});
return this;
}
#endif
ExpressionPtr UnaryExpression::simplify_me() const {
uint32_t value;
if (_children[0]->is_integer(value)) {
return Expression::Integer(unary_op(_op, value));
}
return to_expression_ptr(*this);
}
ExpressionPtr BinaryExpression::simplify_me() const {
uint32_t a,b;
if (_children[0]->is_integer(a) && _children[1]->is_integer(b)) {
return Expression::Integer(binary_op(_op, a, b));
}
// shortcut logical expressions...
if (_children[0]->is_integer(a)) {
switch(_op) {
case '||':
if (a) return Expression::Integer(1);
break;
case '&&':
if (!a) return Expression::Integer(0);
break;
case '+':
case '-':
// int + rel
{
uint32_t pc;
if (_children[1]->is_rel(pc)) {
if (_op == '-') a = -a;
return Expression::Rel(a + pc);
}
}
}
}
// need to re-check since may not have been called above.
if (_children[1]->is_integer(b)) {
switch(_op) {
case '||':
if (b) return Expression::Integer(1);
break;
case '&&':
if (!b) return Expression::Integer(0);
break;
case '+':
case '-':
// rel + int
{
uint32_t pc;
if (_children[0]->is_rel(pc)) {
if (_op == '-') b = -b;
return Expression::Rel(pc + b);
}
}
}
}
// relative - relative -> integer.
// relative [any other op] relative is an error.
if (_children[0]->is_rel(a) && _children[1]->is_rel(b)) {
switch (_op) {
case '-':
return Expression::Integer(a - b);
// logical ops?
}
}
return to_expression_ptr(*this);
}