Skip to content

Commit

Permalink
Adjust priority of <, <=, >, >=, =, &, and |
Browse files Browse the repository at this point in the history
- Improves clarity for expressions performing multiple boolean operations chained with & and |
boxbeam committed Feb 13, 2022
1 parent fb99128 commit 3bc4224
Showing 2 changed files with 8 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/redempt/crunch/token/Operator.java
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@
*/
public enum Operator implements Token {

BOOLEAN_OR("|", 8, (a, b) -> (a == 1 || b == 1) ? 1d : 0d),
BOOLEAN_AND("&", 8, (a, b) -> (a == 1 && b == 1) ? 1d : 0d),
GREATER_THAN(">", 0, (a, b) -> a > b ? 1d : 0d),
LESS_THAN("<", 0, (a, b) -> a < b ? 1d : 0d),
EQUAL_TO("=", 0, (a, b) -> a == b ? 1d : 0d),
GREATER_THAN_OR_EQUAL_TO(">=", 0, (a, b) -> a >= b ? 1d : 0d),
LESS_THAN_OR_EQUAL_TO("<=", 0, (a, b) -> a <= b ? 1d : 0d),
BOOLEAN_OR("|", 0, (a, b) -> (a == 1 || b == 1) ? 1d : 0d),
BOOLEAN_AND("&", 0, (a, b) -> (a == 1 && b == 1) ? 1d : 0d),
GREATER_THAN(">", 1, (a, b) -> a > b ? 1d : 0d),
LESS_THAN("<", 1, (a, b) -> a < b ? 1d : 0d),
EQUAL_TO("=", 1, (a, b) -> a == b ? 1d : 0d),
GREATER_THAN_OR_EQUAL_TO(">=", 1, (a, b) -> a >= b ? 1d : 0d),
LESS_THAN_OR_EQUAL_TO("<=", 1, (a, b) -> a <= b ? 1d : 0d),
BOOLEAN_NOT("!", 9, d -> d == 0 ? 1d : 0d),
RANDOM_DOUBLE("rand", 6, d -> ThreadLocalRandom.current().nextDouble() * d),
ROUND("round", 6, d -> Double.valueOf(Math.round(d))),
1 change: 1 addition & 0 deletions test/redempt/crunch/test/CrunchTest.java
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ public void booleanLogicTest() {
assertEquals(1, Crunch.evaluateExpression("true & true"), "Boolean and");
assertEquals(1, Crunch.evaluateExpression("true | false"), "Boolean or");
assertEquals(0, Crunch.evaluateExpression("true & (true & false | false)"), "More complex boolean expression");
assertEquals(1, Crunch.evaluateExpression("1 = 1 & 3 = 3"), "Arithmetic comparisons");
}

@Test

0 comments on commit 3bc4224

Please sign in to comment.