Package generic.expressions
Class ExpressionEvaluator
java.lang.Object
generic.expressions.ExpressionEvaluator
- Direct Known Subclasses:
AddressEvaluator
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 Summary
ConstructorsConstructorDescriptionConstructs an ExpressionEvaluator in decimal mode.ExpressionEvaluator
(boolean assumeHex) Constructs an ExpressionEvaluator in either decimal or hex mode.ExpressionEvaluator
(boolean assumeHex, Function<String, ExpressionValue> evaluator) Constructs an ExpressionEvaluator in either decimal or hex mode with a given symbol evaluator.ExpressionEvaluator
(Function<String, ExpressionValue> evaluator) Constructs an ExpressionEvaluator in decimal mode with a given symbol evaluator. -
Method Summary
Modifier and TypeMethodDescriptionprotected ExpressionValue
evaluateSymbol
(String token) static Long
evaluateToLong
(String input) Evaluates the given input as a Long value.static Long
evaluateToLong
(String input, boolean assumeHex) Evaluates the given input as a long value.protected ExpressionValue
protected ExpressionValue
parse
(String input, ExpressionValue initial) long
parseAsLong
(String input) Parses the given expression input, expecting the result to be long value.void
setAssumeHex
(boolean b) Changes the hex/decimal mode.
-
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
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
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
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
Evaluates the given input as a long value.- Parameters:
input
- the expression to be parsed into a Long valueassumeHex
- 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
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
- Throws:
ExpressionException
-
parse
- Throws:
ExpressionException
-
evaluateSymbol
-