Class CompiledExpression

java.lang.Object
studio.magemonkey.codex.util.eval.CompiledExpression

public final class CompiledExpression extends Object
A math expression that was parsed once and can be evaluated many times with different variable values.

Get one from Evaluator.compile(String, String...):

CompiledExpression ce = Evaluator.compile("max(d / 5, d - 4 * x / max(1, t + 8))", "d", "x", "t");
double v = ce.eval(def, dmg, toughness);
The syntax, operators, functions and constants are those of the bundled Javaluator DoubleEvaluator, and for any expression both accept the result is the same double, bit for bit. The differences:
  • NaN and infinities are returned as they are. DoubleEvaluator throws when a function returns NaN (for example sqrt(-1)); here that is the caller's check.
  • random() gives a new random number on every call, as it does there.
An instance is immutable and may be shared and evaluated from any number of threads at once.
  • Method Details

    • eval

      public double eval(double... values)
      Evaluates the expression. Does not parse, does not allocate, and does not modify values.
      Parameters:
      values - one value for each variable name, in the order the names were given to compile
      Returns:
      the result. May be NaN or infinite; checking is up to the caller.
      Throws:
      IllegalArgumentException - if the number of values differs from the number of variable names
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getSource

      public String getSource()
      The expression as it was given to Evaluator.compile(String, String...).
    • getVariableNames

      public List<String> getVariableNames()
      The variable names, in the order eval(double...) expects their values. Unmodifiable.