From 2b671b808803fda9a71d8689203db3bc78117996 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 25 Aug 2022 12:48:59 +0200 Subject: [PATCH] Fix issue with precedence of % operator --- .../javascript/rendering/Precedence.java | 1 + .../rendering/StatementRenderer.java | 4 ++-- tests/src/test/java/org/teavm/vm/VMTest.java | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/Precedence.java b/core/src/main/java/org/teavm/backend/javascript/rendering/Precedence.java index 087f274f0..e255bb91f 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/Precedence.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/Precedence.java @@ -28,6 +28,7 @@ public enum Precedence { COMPARISON, BITWISE_SHIFT, ADDITION, + MODULO, MULTIPLICATION, UNARY, NEW, diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java index 62f23432f..681e28ac9 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/StatementRenderer.java @@ -561,7 +561,6 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { case SUBTRACT: case MULTIPLY: case DIVIDE: - case MODULO: case AND: case OR: case BITWISE_AND: @@ -610,8 +609,9 @@ public class StatementRenderer implements ExprVisitor, StatementVisitor { return Precedence.ADDITION; case MULTIPLY: case DIVIDE: - case MODULO: return Precedence.MULTIPLICATION; + case MODULO: + return Precedence.MODULO; case AND: return Precedence.LOGICAL_AND; case OR: diff --git a/tests/src/test/java/org/teavm/vm/VMTest.java b/tests/src/test/java/org/teavm/vm/VMTest.java index 0d531163c..8b8c6fdbc 100644 --- a/tests/src/test/java/org/teavm/vm/VMTest.java +++ b/tests/src/test/java/org/teavm/vm/VMTest.java @@ -496,7 +496,7 @@ public class VMTest { } static class SuperClass { - static final Integer ONE = new Integer(1); + static final Integer ONE = Integer.valueOf(1); private Integer value; @@ -617,4 +617,21 @@ public class VMTest { o = new int[0][]; 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; + } } \ No newline at end of file