Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ public static void print(JassExprBinary e, StringBuilder sb, boolean withSpace)
// if the precedence level on the right is smaller we have to use parentheses
useParanthesesRight = true;
} else if (precedence(right.getOp()) == precedence(e.getOp())) {
// if the precedence level is equal we have to parentheses as operators are
// left associative but for commutative operators (+, *, and, or) we do not
// need parentheses
// if the precedence level is equal we have to use parentheses as operators are
// left associative. Only and and or are associative: + and * are not on reals,
// which are 32 bit floats, so a + (b + c) is not a + b + c

if (!((right.getOp() instanceof JassOpPlus && e.getOp() instanceof JassOpPlus)
|| (right.getOp() instanceof JassOpMult && e.getOp() instanceof JassOpMult)
|| (right.getOp() instanceof JassOpOr && e.getOp() instanceof JassOpOr)
if (!((right.getOp() instanceof JassOpOr && e.getOp() instanceof JassOpOr)
|| (right.getOp() instanceof JassOpAnd && e.getOp() instanceof JassOpAnd))) {
// in other cases use parentheses
// for example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import de.peeeq.wurstio.UtilsIO;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class ExpressionTests extends WurstScriptTest {

Expand Down Expand Up @@ -515,6 +520,32 @@ public void integerDivisionAndModuloStayInt() {
);
}

/**
* A real is a 32 bit float in the game, where addition and multiplication are not associative, so the Jass keeps
* the grouping of a right operand: `a + b + c` is `(a + b) + c`. Only `and` and `or` may lose it.
*/
@Test
public void jassKeepsTheGroupingOfARealSumAndProduct() throws IOException {
test().lines(
"package test",
"native printReal(real r)",
"native printBool(boolean b)",
"real array reals",
"boolean array bools",
"init",
" let a = reals[0]",
" let b = reals[1]",
" let c = reals[2]",
" printReal(a + (b + c))",
" printReal(a * (b * c))",
" printBool(bools[0] or (bools[1] or bools[2]))",
"endpackage");
String jass = Files.readString(Path.of(TEST_OUTPUT_PATH, "ExpressionTests_jassKeepsTheGroupingOfARealSumAndProduct_no_opts.j"));
assertTrue(jass.contains("a + (b + c)"), jass);
assertTrue(jass.contains("a * (b * c)"), jass);
assertTrue(jass.contains("test_bools[0] or test_bools[1] or test_bools[2]"), jass);
}

public void assertOk(String booleanExpr) {
String prog = makeProg(booleanExpr);
testAssertOk(UtilsIO.getMethodName(1), true, prog);
Expand Down
Loading