Skip to content

Commit fe7ac6a

Browse files
authored
Исправлено упрощение выражений
1 parent a4d5b43 commit fe7ac6a

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/main/java/com/annimon/ownlang/parser/optimization/ExpressionSimplification.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,26 @@ public Node visit(BinaryExpression s, Void t) {
5858
if (expr1IsZero || isIntegerValue(s.expr2, 0)) {
5959
switch (s.operation) {
6060
case ADD:
61-
// 0 + x2 to x2, x1 + 0 to x1
61+
// 0 + x = x + 0 = x
6262
simplificationsCount++;
6363
return expr1IsZero ? s.expr2 : s.expr1;
6464

6565
case SUBTRACT:
6666
simplificationsCount++;
6767
if (expr1IsZero) {
68-
// 0 - x2 to -x2
68+
// 0 - x = -x
6969
return new UnaryExpression(UnaryExpression.Operator.NEGATE, s.expr2);
7070
}
71-
// x1 - 0 to x1
71+
// x - 0 = x
7272
return s.expr1;
7373

7474
case MULTIPLY:
75-
// 0 * x2 to 0, x1 * 0 to 0
75+
// 0 * x = x * 0 = 0
7676
simplificationsCount++;
7777
return new ValueExpression(0);
7878

7979
case DIVIDE:
80-
// 0 / x2 to 0
80+
// 0 / x = 0
8181
if (expr1IsZero) {
8282
simplificationsCount++;
8383
return new ValueExpression(0);
@@ -91,12 +91,12 @@ public Node visit(BinaryExpression s, Void t) {
9191
if (expr1IsOne || isIntegerValue(s.expr2, 1)) {
9292
switch (s.operation) {
9393
case MULTIPLY:
94-
// 1 * x2 to x2, x1 * 1 to x1
94+
// 1 * x = x * 1 = x
9595
simplificationsCount++;
9696
return expr1IsOne ? s.expr2 : s.expr1;
9797

9898
case DIVIDE:
99-
// x1 / 1 to x1
99+
// x / 1 = x
100100
if (!expr1IsOne) {
101101
simplificationsCount++;
102102
return s.expr1;
@@ -105,19 +105,26 @@ public Node visit(BinaryExpression s, Void t) {
105105
}
106106
}
107107

108-
// x1 / -1 to -x1
109-
if (isIntegerValue(s.expr2, -1)) {
108+
// x / -1 = -x
109+
if (isIntegerValue(s.expr2, -1) && s.operation == BinaryExpression.Operator.DIVIDE) {
110110
simplificationsCount++;
111111
return new UnaryExpression(UnaryExpression.Operator.NEGATE, s.expr1);
112112
}
113113

114-
// x - x to 0
114+
// -1 * x = x * -1 = -x
115+
final boolean expr1IsMinusOne = isIntegerValue(s.expr1, -1);
116+
if ((expr1IsMinusOne || isIntegerValue(s.expr2, -1)) && s.operation == BinaryExpression.Operator.MULTIPLY) {
117+
simplificationsCount++;
118+
return new UnaryExpression(UnaryExpression.Operator.NEGATE, expr1IsMinusOne ? s.expr2 : s.expr1);
119+
}
120+
121+
// x - x = 0
115122
if (isSameVariables(s.expr1, s.expr2) && s.operation == BinaryExpression.Operator.SUBTRACT) {
116123
simplificationsCount++;
117124
return new ValueExpression(0);
118125
}
119126

120-
// x >> 0 to x, x << 0 to x
127+
// x >> 0 = x, x << 0 = x
121128
if (isIntegerValue(s.expr2, 0) &&
122129
(s.operation == BinaryExpression.Operator.LSHIFT ||
123130
s.operation == BinaryExpression.Operator.RSHIFT)) {
@@ -134,12 +141,12 @@ public Node visit(ConditionalExpression s, Void t) {
134141
return super.visit(s, t);
135142
}
136143
if (isIntegerValue(s.expr1, 0) && s.operation == ConditionalExpression.Operator.AND) {
137-
// 0 && x2 to 0
144+
// 0 && x = 0
138145
simplificationsCount++;
139146
return new ValueExpression(0);
140147
}
141148
if (isIntegerValue(s.expr1, 1) && s.operation == ConditionalExpression.Operator.OR) {
142-
// 1 || x2 to 1
149+
// 1 || x = 1
143150
simplificationsCount++;
144151
return new ValueExpression(1);
145152
}

0 commit comments

Comments
 (0)