-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathExpressionFolding.java
More file actions
200 lines (185 loc) · 8.75 KB
/
ExpressionFolding.java
File metadata and controls
200 lines (185 loc) · 8.75 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
package liquidjava.rj_language.opt;
import liquidjava.rj_language.ast.BinaryExpression;
import liquidjava.rj_language.ast.Expression;
import liquidjava.rj_language.ast.GroupExpression;
import liquidjava.rj_language.ast.LiteralBoolean;
import liquidjava.rj_language.ast.LiteralInt;
import liquidjava.rj_language.ast.LiteralReal;
import liquidjava.rj_language.ast.UnaryExpression;
import liquidjava.rj_language.opt.derivation_node.BinaryDerivationNode;
import liquidjava.rj_language.opt.derivation_node.DerivationNode;
import liquidjava.rj_language.opt.derivation_node.UnaryDerivationNode;
import liquidjava.rj_language.opt.derivation_node.ValDerivationNode;
public class ExpressionFolding {
/**
* Performs expression folding on a derivation node by evaluating nodes when possible. Returns a new derivation node
* representing the folding steps taken
*/
public static ValDerivationNode fold(ValDerivationNode node) {
Expression exp = node.getValue();
if (exp instanceof BinaryExpression)
return foldBinary(node);
if (exp instanceof UnaryExpression)
return foldUnary(node);
if (exp instanceof GroupExpression group) {
if (group.getChildren().size() == 1) {
return fold(new ValDerivationNode(group.getChildren().get(0), node.getOrigin()));
}
}
return node;
}
/**
* Folds a binary expression node (e.g. 1 + 2 => 3)
*/
private static ValDerivationNode foldBinary(ValDerivationNode node) {
BinaryExpression binExp = (BinaryExpression) node.getValue();
DerivationNode parent = node.getOrigin();
// fold child nodes
ValDerivationNode leftNode;
ValDerivationNode rightNode;
if (parent instanceof BinaryDerivationNode binaryOrigin) {
// has origin (from constant propagation)
leftNode = fold(binaryOrigin.getLeft());
rightNode = fold(binaryOrigin.getRight());
} else {
// no origin
leftNode = fold(new ValDerivationNode(binExp.getFirstOperand(), null));
rightNode = fold(new ValDerivationNode(binExp.getSecondOperand(), null));
}
Expression left = leftNode.getValue();
Expression right = rightNode.getValue();
String op = binExp.getOperator();
binExp.setChild(0, left);
binExp.setChild(1, right);
// int and int
if (left instanceof LiteralInt && right instanceof LiteralInt) {
int l = ((LiteralInt) left).getValue();
int r = ((LiteralInt) right).getValue();
Expression res = switch (op) {
case "+" -> new LiteralInt(l + r);
case "-" -> new LiteralInt(l - r);
case "*" -> new LiteralInt(l * r);
case "/" -> r != 0 ? new LiteralInt(l / r) : null;
case "%" -> r != 0 ? new LiteralInt(l % r) : null;
case "<" -> new LiteralBoolean(l < r);
case "<=" -> new LiteralBoolean(l <= r);
case ">" -> new LiteralBoolean(l > r);
case ">=" -> new LiteralBoolean(l >= r);
case "==" -> new LiteralBoolean(l == r);
case "!=" -> new LiteralBoolean(l != r);
default -> null;
};
if (res != null)
return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op));
}
// real and real
else if (left instanceof LiteralReal && right instanceof LiteralReal) {
double l = ((LiteralReal) left).getValue();
double r = ((LiteralReal) right).getValue();
Expression res = switch (op) {
case "+" -> new LiteralReal(l + r);
case "-" -> new LiteralReal(l - r);
case "*" -> new LiteralReal(l * r);
case "/" -> r != 0.0 ? new LiteralReal(l / r) : null;
case "%" -> r != 0.0 ? new LiteralReal(l % r) : null;
case "<" -> new LiteralBoolean(l < r);
case "<=" -> new LiteralBoolean(l <= r);
case ">" -> new LiteralBoolean(l > r);
case ">=" -> new LiteralBoolean(l >= r);
case "==" -> new LiteralBoolean(l == r);
case "!=" -> new LiteralBoolean(l != r);
default -> null;
};
if (res != null)
return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op));
}
// mixed int and real
else if ((left instanceof LiteralInt && right instanceof LiteralReal)
|| (left instanceof LiteralReal && right instanceof LiteralInt)) {
double l = left instanceof LiteralInt ? ((LiteralInt) left).getValue() : ((LiteralReal) left).getValue();
double r = right instanceof LiteralInt ? ((LiteralInt) right).getValue() : ((LiteralReal) right).getValue();
Expression res = switch (op) {
case "+" -> new LiteralReal(l + r);
case "-" -> new LiteralReal(l - r);
case "*" -> new LiteralReal(l * r);
case "/" -> r != 0.0 ? new LiteralReal(l / r) : null;
case "%" -> r != 0.0 ? new LiteralReal(l % r) : null;
case "<" -> new LiteralBoolean(l < r);
case "<=" -> new LiteralBoolean(l <= r);
case ">" -> new LiteralBoolean(l > r);
case ">=" -> new LiteralBoolean(l >= r);
case "==" -> new LiteralBoolean(l == r);
case "!=" -> new LiteralBoolean(l != r);
default -> null;
};
if (res != null)
return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op));
}
// bool and bool
else if (left instanceof LiteralBoolean && right instanceof LiteralBoolean) {
boolean l = left.isBooleanTrue();
boolean r = right.isBooleanTrue();
Expression res = switch (op) {
case "&&" -> new LiteralBoolean(l && r);
case "||" -> new LiteralBoolean(l || r);
case "-->" -> new LiteralBoolean(!l || r);
case "==" -> new LiteralBoolean(l == r);
case "!=" -> new LiteralBoolean(l != r);
default -> null;
};
if (res != null)
return new ValDerivationNode(res, new BinaryDerivationNode(leftNode, rightNode, op));
}
// no folding
DerivationNode origin = (leftNode.getOrigin() != null || rightNode.getOrigin() != null)
? new BinaryDerivationNode(leftNode, rightNode, op) : null;
return new ValDerivationNode(binExp, origin);
}
/**
* Folds a unary expression node (e.g. !true => false)
*/
private static ValDerivationNode foldUnary(ValDerivationNode node) {
UnaryExpression unaryExp = (UnaryExpression) node.getValue();
DerivationNode parent = node.getOrigin();
// fold child node
ValDerivationNode operandNode;
if (parent instanceof UnaryDerivationNode unaryOrigin) {
// has origin (from constant propagation)
operandNode = fold(unaryOrigin.getOperand());
} else {
// no origin
operandNode = fold(new ValDerivationNode(unaryExp.getChildren().get(0), null));
}
Expression operand = operandNode.getValue();
String operator = unaryExp.getOp();
unaryExp.setChild(0, operand);
// unary not
if ("!".equals(operator) && operand instanceof LiteralBoolean) {
// !true => false, !false => true
boolean value = operand.isBooleanTrue();
Expression res = new LiteralBoolean(!value);
DerivationNode origin = operandNode.getOrigin() != null ? new UnaryDerivationNode(operandNode, operator)
: null;
return new ValDerivationNode(res, origin);
}
// unary minus
if ("-".equals(operator)) {
// -(x) => -x
if (operand instanceof LiteralInt) {
Expression res = new LiteralInt(-((LiteralInt) operand).getValue());
DerivationNode origin = operandNode.getOrigin() != null ? new UnaryDerivationNode(operandNode, operator)
: null;
return new ValDerivationNode(res, origin);
}
if (operand instanceof LiteralReal) {
Expression res = new LiteralReal(-((LiteralReal) operand).getValue());
DerivationNode origin = operandNode.getOrigin() != null ? new UnaryDerivationNode(operandNode, operator)
: null;
return new ValDerivationNode(res, origin);
}
}
// no folding
DerivationNode origin = operandNode.getOrigin() != null ? new UnaryDerivationNode(operandNode, operator) : null;
return new ValDerivationNode(unaryExp, origin);
}
}