Evaluate expressions with your custom config. Originally made for Tensai VFX engine.
<!-- pom.xml -->
<dependency>
<groupId>dev.phomc</groupId>
<artifactId>expressions-eval</artifactId>
<version>0.0.1-SNAPSHOT</versi
</dependency>
Expression expr = Expression.parse("(1 + 2 * 3 + 4) / 5");
expr.eval(new SimpleEvalContext() {}, null); // The expected result is 2 in integer type
Expression expr2 = Expression.parse("x * 1.2");
expr.eval(new SimpleEvalContext() {}, VariablesInterface.of(Map.of("x", 123.456)));
If you are willing to call eval()
multiple times, it is a good idea to compile it to virtual machine code with Expression.compile()
or VirtualMachineExpression.compile()
and use eval()
on it:
Expression expr = Expression.compile("x * 1.2");
// ...
expr.eval(evalContext, variables);
You can create your own evaluate context to accepts custom types and provide global variables to all expressions:
public class MyEvalContext implements SimpleEvalContext {
@Override
public Object applyOperator(Object a, Operator op, Object b) {
if (a instanceof Vector3 va && b instanceof Vector3 vb) {
return switch (op) {
case ADD -> va.add(vb);
case SUBTRACT -> va.sub(vb);
case MULTIPLY -> va.mul(vb);
case DIVIDE -> va.div(vb);
default -> null;
};
}
return super.applyOperator();
}
}