Interface Evaluator
- All Known Subinterfaces:
StackEvaluator
,StandardEvaluator
,StandardStackEvaluator
,StandardTreeEvaluator
,TreeEvaluator
- All Known Implementing Classes:
AbstractEvaluator
,AbstractStandardEvaluator
,DefaultStackEvaluator
,DefaultTreeEvaluator
- Author:
- Curtis Rueden
-
Method Summary
Modifier and TypeMethodDescriptionEvaluates an infix expression.evaluate
(LinkedList<Object> queue) Evaluates a postfix token queue.evaluate
(SyntaxTree syntaxTree) Evaluates a syntax tree.Gets the value of a variable.default Object
Gets the value of a variable.Gets the parser used when evaluating expressions.boolean
isStrict()
Gets whether the evaluator is operating in strict mode.void
Sets the value of a variable.default void
Sets the value of a variable.void
Assigns variables en masse.void
setStrict
(boolean strict) Sets whether the evaluator is operating in strict mode.default Object
Gets the value of a token.default Variable
Casts the given token to a variable.
-
Method Details
-
getParser
ExpressionParser getParser()Gets the parser used when evaluating expressions.- Returns:
- The expression parser used by this evaluator.
-
isStrict
boolean isStrict()Gets whether the evaluator is operating in strict mode.- Returns:
- True iff the evaluator is operating in strict mode.
- See Also:
-
setStrict
void setStrict(boolean strict) Sets whether the evaluator is operating in strict mode. Evaluators operate in strict mode by default.When evaluating strictly, usage of an unassigned variable token in a place where its value is needed will generate an
IllegalArgumentException
with an "Unknown variable" message; in non-strict mode, such a variable will instead be resolved to an object of typeUnresolved
with the same name as the original variable.In cases such as assignment, this may be sufficient to complete the evaluation; for example, the expression
foo=bar
will complete successfully in non-strict mode, with the variablefoo
containing an object of typeUnresolved
and token value"bar"
. But in cases where the unresolved value is needed as an input for additional operations, the evaluation may still ultimately fail if the operation in question is not defined for unresolved values. For example, theDefaultStackEvaluator
will fail with an "Unsupported binary operator" exception when given the expressionfoo+bar
, sincefoo
andbar
are unresolved variables, and the+
operator cannot handle such objects.- Parameters:
strict
- True iff the evaluator should operate in strict mode.
-
evaluate
Evaluates an infix expression.- Parameters:
expression
- The infix expression to evaluate.- Returns:
- The result of the evaluation.
-
evaluate
Evaluates a postfix token queue.- Parameters:
queue
- The postfix token queue to evaluate.- Returns:
- The result of the evaluation.
-
evaluate
Evaluates a syntax tree.- Parameters:
syntaxTree
- The syntax tree to evaluate.- Returns:
- The result of the evaluation.
-
value
Gets the value of a token. For variables, returns the value of the variable, throwing an exception if the variable is not set. For literals, returns the token itself.- Parameters:
token
- The token whose value you want.- Returns:
- The token's value.
-
var
Casts the given token to a variable.- Parameters:
token
- The token to cast to aVariable
.- Returns:
Variable
representation of the token.- Throws:
IllegalArgumentException
- if the given token is not aVariable
.
-
get
Gets the value of a variable.- Parameters:
name
- The name of the variable whose value you want.- Returns:
- The variable's value.
- Throws:
IllegalArgumentException
- If the variable's value is not set, and the evaluator is operating instrict mode
.
-
set
Sets the value of a variable.- Parameters:
name
- The name of the variable whose value you want to set.value
- The value to assign to the variable.
-
get
Gets the value of a variable.- Parameters:
v
- The variable whose value you want.- Returns:
- The variable's value.
- Throws:
IllegalArgumentException
- If the variable's value is not set, and the evaluator is operating instrict mode
.
-
set
Sets the value of a variable.- Parameters:
v
- The variable whose value you want to set.value
- The value to assign to the variable.
-
setAll
Assigns variables en masse.- Parameters:
map
- A map from variable names to variable values.
-