Class ExpressionEvaluator

java.lang.Object
generic.expressions.ExpressionEvaluator
Direct Known Subclasses:
AddressEvaluator

public class ExpressionEvaluator extends Object
Class for evaluating numeric expressions. See ExpressionOperator for the full list of supported operators. All values are interpreted as longs. Optionally, an ExpressionEvalualuator can be constructed with a symbol evaluator that will be called on any string that can't be evaluated as an operator or number.

ExpressionEvaluators can operate in either decimal or hex mode. If in hex mode, all numbers are assumed to be hexadecimal values. In decimal mode, numbers are assumed to be decimal values, but hexadecimal values can still be specified by prefixing them with "0x".

There are also two convenience static methods that can be called to evaluate expressions. These methods will either return a Long value as the result or null if there was an error evaluating the expression. To get error messages related to parsing the expression, instantiate an ExpressionEvaluator and call parse(String) which will throw a ExpressionException when the expression can't be evaluated.

  • Constructor Details

    • ExpressionEvaluator

      public ExpressionEvaluator()
      Constructs an ExpressionEvaluator in decimal mode.
    • ExpressionEvaluator

      public ExpressionEvaluator(boolean assumeHex)
      Constructs an ExpressionEvaluator in either decimal or hex mode.
      Parameters:
      assumeHex - if true, the evaluator will assume all values are hexadecimal.
    • ExpressionEvaluator

      public ExpressionEvaluator(Function<String,ExpressionValue> evaluator)
      Constructs an ExpressionEvaluator in decimal mode with a given symbol evaluator.
      Parameters:
      evaluator - A function that can convert a string token into a value (Must be Long ExpressionValues, unless this is being called by a subclass that can handle other types of operand values)
    • ExpressionEvaluator

      public ExpressionEvaluator(boolean assumeHex, Function<String,ExpressionValue> evaluator)
      Constructs an ExpressionEvaluator in either decimal or hex mode with a given symbol evaluator.
      Parameters:
      assumeHex - if true, the evaluator will assume all values are hexadecimal.
      evaluator - A function that can convert a string token into a value (Must be Long ExpressionValues, unless this is being called by a subclass that can handle other types of operand values)
  • Method Details

    • evaluateToLong

      public static Long evaluateToLong(String input)
      Evaluates the given input as a Long value. This call assumes all numbers are decimal unless prefixed with a "0x".
      Parameters:
      input - the expression to be parsed into a Long value
      Returns:
      the resulting Long value or null if the expression could not be evaluated.
    • evaluateToLong

      public static Long evaluateToLong(String input, boolean assumeHex)
      Evaluates the given input as a long value.
      Parameters:
      input - the expression to be parsed into a Long value
      assumeHex - if true, numbers will be assumed to be hexadecimal values.
      Returns:
      the resulting Long value or null if the expression could not be evaluated.
    • parseAsLong

      public long parseAsLong(String input) throws ExpressionException
      Parses the given expression input, expecting the result to be long value.
      Parameters:
      input - the expression string
      Returns:
      the long value result.
      Throws:
      ExpressionException - if the expression could not be evaluated to a long value.
    • setAssumeHex

      public void setAssumeHex(boolean b)
      Changes the hex/decimal mode.
      Parameters:
      b - if true, all numbers will be assumed to be hexadecimal
    • parse

      protected ExpressionValue parse(String input) throws ExpressionException
      Throws:
      ExpressionException
    • parse

      protected ExpressionValue parse(String input, ExpressionValue initial) throws ExpressionException
      Throws:
      ExpressionException
    • evaluateSymbol

      protected ExpressionValue evaluateSymbol(String token)