Skip to content

Commit

Permalink
Make variables no longer hold a reference to their expression
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Feb 5, 2024
1 parent 7139ec7 commit 79bb4e2
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 28 deletions.
8 changes: 4 additions & 4 deletions src/redempt/crunch/CompiledExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public int getVariableCount() {
*/
public double evaluate(double... values) {
setVariableValues(values);
return value.getValue();
return value.getValue(this.variableValues);
}

/**
Expand All @@ -64,7 +64,7 @@ public double evaluate(double... values) {
*/
public double evaluate() {
checkArgCount(0);
return value.getValue();
return value.getValue(this.variableValues);
}

/**
Expand All @@ -78,7 +78,7 @@ public double evaluate(double first) {
variableValues = new double[1];
}
variableValues[0] = first;
return value.getValue();
return value.getValue(this.variableValues);
}

/**
Expand All @@ -94,7 +94,7 @@ public double evaluate(double first, double second) {
}
variableValues[0] = first;
variableValues[1] = second;
return value.getValue();
return value.getValue(this.variableValues);
}

private void checkArgCount(int args) {
Expand Down
9 changes: 3 additions & 6 deletions src/redempt/crunch/ExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private Value parseNestedExpression() {

private Value parseAnonymousVariable() {
expectChar('$');
double value = parseLiteral().getValue();
double value = parseLiteral().getValue(new double[0]);
if (value % 1 != 0) {
error("Decimal variable indices are not allowed");
}
Expand All @@ -121,7 +121,7 @@ private Value parseAnonymousVariable() {
}
int index = (int) value - 1;
maxVarIndex = Math.max(index, maxVarIndex);
return new Variable(expression, index);
return new Variable(index);
}

private Value parseTerm() {
Expand Down Expand Up @@ -154,9 +154,6 @@ private Value parseTerm() {
if (term == null) {
error("Expected value");
}
if (term instanceof Variable) {
((Variable) term).expression = expression;
}
return term;
}

Expand All @@ -179,7 +176,7 @@ private Value parseLeadingOperation(Token token) {
UnaryOperator op = (UnaryOperator) token;
Value term = parseTerm();
if (op.isPure() && term.getType() == TokenType.LITERAL_VALUE) {
return new LiteralValue(op.getOperation().applyAsDouble(term.getValue()));
return new LiteralValue(op.getOperation().applyAsDouble(term.getValue(new double[0])));
}
return new UnaryOperation(op, term);
case FUNCTION:
Expand Down
2 changes: 1 addition & 1 deletion src/redempt/crunch/ShuntingYard.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void createOperation() {
Value right = stack.removeLast();
Value left = stack.removeLast();
if (right.getType() == TokenType.LITERAL_VALUE && left.getType() == TokenType.LITERAL_VALUE) {
stack.add(new LiteralValue(op.getOperation().applyAsDouble(left.getValue(), right.getValue())));
stack.add(new LiteralValue(op.getOperation().applyAsDouble(left.getValue(new double[0]), right.getValue(new double[0]))));
} else {
stack.add(new BinaryOperation(op, left, right));
}
Expand Down
10 changes: 4 additions & 6 deletions src/redempt/crunch/Variable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
public class Variable implements Value {

private final int index;
protected CompiledExpression expression;

public Variable(CompiledExpression expression, int index) {
this.expression = expression;
public Variable(int index) {
this.index = index;
}

Expand All @@ -18,8 +16,8 @@ public int getIndex() {
}

@Override
public double getValue() {
return expression.variableValues[index];
public double getValue(double[] variableValues) {
return variableValues[index];
}

@Override
Expand All @@ -32,7 +30,7 @@ public String toString() {
}

public Variable getClone() {
return new Variable(expression, index);
return new Variable(index);
}

}
2 changes: 1 addition & 1 deletion src/redempt/crunch/functional/ExpressionEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ExpressionEnv setVariableNames(String... names) {
varCount = names.length;
for (int i = 0; i < names.length; i++) {
checkName(names[i]);
values.set(names[i], new Variable(null, i));
values.set(names[i], new Variable(i));
}
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/redempt/crunch/functional/FunctionCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public TokenType getType() {
}

@Override
public double getValue() {
public double getValue(double[] variableValues) {
for (int i = 0; i < values.length; i++) {
numbers[i] = values[i].getValue();
numbers[i] = values[i].getValue(variableValues);
}
return function.call(numbers);
}
Expand Down
4 changes: 2 additions & 2 deletions src/redempt/crunch/token/BinaryOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public Value[] getValues() {
}

@Override
public double getValue() {
return operator.getOperation().applyAsDouble(first.getValue(), second.getValue());
public double getValue(double[] variableValues) {
return operator.getOperation().applyAsDouble(first.getValue(variableValues), second.getValue(variableValues));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/redempt/crunch/token/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TokenType getType() {
}

@Override
public double getValue() {
public double getValue(double[] variableValues) {
return value;
}

Expand Down
2 changes: 1 addition & 1 deletion src/redempt/crunch/token/LazyVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public TokenType getType() {
}

@Override
public double getValue() {
public double getValue(double[] variableValues) {
return supplier.getAsDouble();
}

Expand Down
2 changes: 1 addition & 1 deletion src/redempt/crunch/token/LiteralValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public TokenType getType() {
}

@Override
public double getValue() {
public double getValue(double[] variableValues) {
return value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/redempt/crunch/token/UnaryOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public Value getChild() {
}

@Override
public double getValue() {
return operator.getOperation().applyAsDouble(first.getValue());
public double getValue(double[] variableValues) {
return operator.getOperation().applyAsDouble(first.getValue(variableValues));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/redempt/crunch/token/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
public interface Value extends Token, Cloneable {

double getValue();
double getValue(double[] variableValues);
Value getClone();

}
7 changes: 7 additions & 0 deletions test/redempt/crunch/test/CrunchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,11 @@ void largeExpressionWithCustomFunctionTest() {
assertEquals(83080, compiled.evaluate());
}

@Test
void cloneTest() {
CompiledExpression expr = Crunch.compileExpression("$1");
assertEquals(1, expr.evaluate(1));
assertEquals(2, expr.clone().evaluate(2));
}

}

0 comments on commit 79bb4e2

Please sign in to comment.