Package com.gengoai.parsing
Class Evaluator<O>
- java.lang.Object
-
- com.gengoai.parsing.Evaluator<O>
-
- Type Parameters:
O
- the type parameter
- All Implemented Interfaces:
Serializable
public abstract class Evaluator<O> extends Object implements Serializable
An evaluator provides a switch-like interface for evaluating expressions. Custom evaluators can be created as follows:
Evaluator<Double> eval = new Evaluator<Double>() {{ $(BinaryOperatorExpression.class, CommonTypes.PLUS, e -> eval(e.left) + eval(e.right)); $(ValueExpression.class, e -> Double.valueOf(e.toString())); }}
$
methods allow easily adding if-like predicates and then-like functions. Theeval(Expression)
method can be used to make recursive evaluation calls.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Evaluator()
Instantiates a new Evaluator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <E extends Expression>
void$(@NonNull Class<E> expressionClass, @NonNull Tag type, @NonNull CheckedFunction<E,? extends O> function)
Adds a switch statement where the condition is that the expression is of typeexpressionClass
and the expressions's token type is an instance oftype
.protected <E extends Expression>
void$(Class<E> expressionClass, CheckedFunction<E,? extends O> function)
Adds a switch statement where the condition is that the expression is of typeexpressionClass
.O
eval(Expression expression)
Evaluates the given expression
-
-
-
Method Detail
-
$
protected final <E extends Expression> void $(@NonNull @NonNull Class<E> expressionClass, @NonNull @NonNull Tag type, @NonNull @NonNull CheckedFunction<E,? extends O> function)
Adds a switch statement where the condition is that the expression is of typeexpressionClass
and the expressions's token type is an instance oftype
. When the condition is met the expression is cast as the given expression class and the given function is applied.- Type Parameters:
E
- the type of expression- Parameters:
expressionClass
- the expression classtype
- the token typefunction
- the function to apply when the condition is met.
-
$
protected final <E extends Expression> void $(Class<E> expressionClass, CheckedFunction<E,? extends O> function)
Adds a switch statement where the condition is that the expression is of typeexpressionClass
. When the condition is met the expression is cast as the given expression class and the given function is applied.- Type Parameters:
E
- the type of expression- Parameters:
expressionClass
- the expression classfunction
- the function to apply when the condition is met.
-
eval
public final O eval(Expression expression) throws ParseException
Evaluates the given expression- Parameters:
expression
- the expression to evaluate- Returns:
- the result of evaluation
- Throws:
ParseException
- Something went wrong during evaluation
-
-