org.snapscript.tree.operation.CalculationOperation Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap Show documentation
Show all versions of snap Show documentation
Dynamic scripting for the JVM
package org.snapscript.tree.operation;
import org.snapscript.core.Evaluation;
import org.snapscript.core.constraint.Constraint;
import org.snapscript.core.convert.StringBuilder;
import org.snapscript.core.scope.Scope;
import org.snapscript.core.variable.Value;
import org.snapscript.tree.math.NumericChecker;
import org.snapscript.tree.math.NumericOperator;
public class CalculationOperation extends Evaluation {
private final NumericOperator operator;
private final Evaluation left;
private final Evaluation right;
public CalculationOperation(NumericOperator operator, Evaluation left, Evaluation right) {
this.operator = operator;
this.left = left;
this.right = right;
}
@Override
public void define(Scope scope) throws Exception {
left.define(scope);
right.define(scope);
}
@Override
public Constraint compile(Scope scope, Constraint context) throws Exception {
left.compile(scope, null);
return right.compile(scope, null);
}
@Override
public Value evaluate(Scope scope, Object context) throws Exception {
Value leftResult = left.evaluate(scope, null);
Value rightResult = right.evaluate(scope, null);
if(operator == NumericOperator.PLUS) {
Object leftValue = leftResult.getValue();
Object rightValue = rightResult.getValue();
if(!NumericChecker.isBothNumeric(leftValue, rightValue)) {
String leftText = StringBuilder.create(scope, leftValue);
String rightText = StringBuilder.create(scope, rightValue);
String text = leftText.concat(rightText);
return Value.getTransient(text);
}
}
return operator.operate(leftResult, rightResult);
}
}