Fix issue with precedence of % operator

This commit is contained in:
Alexey Andreev 2022-08-25 12:48:59 +02:00
parent bab6c8bddf
commit 2b671b8088
3 changed files with 21 additions and 3 deletions

View File

@ -28,6 +28,7 @@ public enum Precedence {
COMPARISON, COMPARISON,
BITWISE_SHIFT, BITWISE_SHIFT,
ADDITION, ADDITION,
MODULO,
MULTIPLICATION, MULTIPLICATION,
UNARY, UNARY,
NEW, NEW,

View File

@ -561,7 +561,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
case SUBTRACT: case SUBTRACT:
case MULTIPLY: case MULTIPLY:
case DIVIDE: case DIVIDE:
case MODULO:
case AND: case AND:
case OR: case OR:
case BITWISE_AND: case BITWISE_AND:
@ -610,8 +609,9 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor {
return Precedence.ADDITION; return Precedence.ADDITION;
case MULTIPLY: case MULTIPLY:
case DIVIDE: case DIVIDE:
case MODULO:
return Precedence.MULTIPLICATION; return Precedence.MULTIPLICATION;
case MODULO:
return Precedence.MODULO;
case AND: case AND:
return Precedence.LOGICAL_AND; return Precedence.LOGICAL_AND;
case OR: case OR:

View File

@ -496,7 +496,7 @@ public class VMTest {
} }
static class SuperClass { static class SuperClass {
static final Integer ONE = new Integer(1); static final Integer ONE = Integer.valueOf(1);
private Integer value; private Integer value;
@ -617,4 +617,21 @@ public class VMTest {
o = new int[0][]; o = new int[0][];
assertEquals(0, ((int[][]) o).length); assertEquals(0, ((int[][]) o).length);
} }
@Test
public void precedence() {
float a = count(3);
float b = count(7);
float c = 5;
assertEquals(1, a * b % c, 0.1f);
assertEquals(6, a * (b % c), 0.1f);
}
private int count(int value) {
int result = 0;
for (int i = 0; i < value; ++i) {
result += 1;
}
return result;
}
} }