Skip to content

Commit

Permalink
Fix inlining of random
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Feb 16, 2023
1 parent 53d45fa commit e01e259
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/redempt/crunch/ExpressionCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private static void createOperation(Node node) {
if (next.getType() == TokenType.OPERATOR) {
throw new ExpressionCompilationException("Adjacent operators have no values to operate on");
}
if (next.getType() == TokenType.LITERAL_VALUE) {
if (next.getType() == TokenType.LITERAL_VALUE && op.canInline()) {
Value literal = (Value) next;
node.token = new LiteralValue(op.operate(literal.getValue()));
return;
Expand All @@ -229,7 +229,7 @@ private static void createOperation(Node node) {
if (prev.getType() == TokenType.OPERATOR || next.getType() == TokenType.OPERATOR) {
throw new ExpressionCompilationException("Adjacent operators have no values to operate on");
}
if (prev.getType() == TokenType.LITERAL_VALUE && next.getType() == TokenType.LITERAL_VALUE) {
if (prev.getType() == TokenType.LITERAL_VALUE && next.getType() == TokenType.LITERAL_VALUE && op.canInline()) {
Value lit1 = (Value) prev;
Value lit2 = (Value) next;
node.token = new LiteralValue(op.operate(lit1.getValue(), lit2.getValue()));
Expand Down
16 changes: 15 additions & 1 deletion src/redempt/crunch/token/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public enum Operator implements Token {
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),
RANDOM_DOUBLE("rand", 6, d -> ThreadLocalRandom.current().nextDouble() * d, false, false),
ROUND("round", 6, d -> Double.valueOf(Math.round(d))),
CEILING("ceil", 6, d -> Math.ceil(d)),
FLOOR("floor", 6, d -> Math.floor(d)),
Expand Down Expand Up @@ -54,6 +54,7 @@ public enum Operator implements Token {
private DoubleBinaryOperator operate;
private int priority;
private boolean internal;
private boolean canInline = true;

private Operator(String name, int priority, DoubleBinaryOperator operate) {
this(name, priority, operate, false);
Expand All @@ -79,6 +80,15 @@ private Operator(String name, int priority, DoubleUnaryOperator operate, boolean
this.internal = internal;
}

private Operator(String name, int priority, DoubleUnaryOperator operate, boolean internal, boolean canInline) {
this.name = name;
this.operate = (a, b) -> operate.applyAsDouble(a);
this.unary = true;
this.priority = priority;
this.internal = internal;
this.canInline = canInline;
}

/**
* @return The priority of the operation in evaluation - higher priority will be evaluated first
*/
Expand Down Expand Up @@ -139,4 +149,8 @@ public String toString() {
return getSymbol();
}

public boolean canInline() {
return canInline;
}

}
10 changes: 8 additions & 2 deletions test/redempt/crunch/test/CrunchTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package redempt.crunch.test;

import org.junit.jupiter.api.Test;
import redempt.crunch.CompiledExpression;
import redempt.crunch.Crunch;
import redempt.crunch.exceptions.ExpressionCompilationException;
import redempt.crunch.exceptions.ExpressionEvaluationException;
import redempt.crunch.functional.EvaluationEnvironment;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

public class CrunchTest {

Expand Down Expand Up @@ -123,4 +123,10 @@ public void scientificNotationTest() {
assertEquals(2E7, Crunch.evaluateExpression("2E7"), DELTA);
}

@Test
public void noInlineRandomTest() {
CompiledExpression expr = Crunch.compileExpression("rand1000000");
assertNotEquals(expr.evaluate(), expr.evaluate());
}

}

0 comments on commit e01e259

Please sign in to comment.