Class Interpreter
- All Implemented Interfaces:
Expression.Visitor<Object>
Implements the Visitor pattern to traverse and evaluate the AST produced by the parser. Supports all CEL operations including macros, type conversions, and complex expressions.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new interpreter with default empty variable and function mappings.Interpreter(Map<String, Object> variables, Functions functions) Constructs an interpreter with the specified variables and functions. -
Method Summary
Modifier and TypeMethodDescriptionevaluate(Expression expr) Evaluates a CEL expression and returns its result.visitBinary(Binary expr) Visits a binary expression node in the AST.Visits a function call or method invocation expression node.Visits a comprehension expression node in the AST.visitConditional(Conditional expr) Visits a conditional expression node in the AST.visitIdentifier(Identifier expr) Visits an identifier expression node in the AST.visitIndex(Index expr) Visits an index expression node in the AST.visitList(ListExpression expr) Visits a list expression node in the AST.visitLiteral(Literal expr) Visits a literal expression node in the AST.visitMap(MapExpression expr) Visits a map expression node in the AST.visitSelect(Select expr) Visits a select expression node in the AST.visitStruct(Struct expr) Visits a struct expression node in the AST.visitUnary(Unary expr) Visits a unary expression node in the AST.
-
Constructor Details
-
Interpreter
Constructs an interpreter with the specified variables and functions.- Parameters:
variables- A map of variable names to their values. If null, an empty map is used.functions- An instance of Functions to handle function calls. If null, StandardFunctions is used.
-
Interpreter
public Interpreter()Creates a new interpreter with default empty variable and function mappings.This constructor initializes the interpreter with empty collections for variables and functions, using the standard function library.
-
-
Method Details
-
evaluate
Evaluates a CEL expression and returns its result.- Parameters:
expr- The expression to evaluate- Returns:
- The result of the evaluation
- Throws:
com.libdbm.cel.EvaluationError- if evaluation fails
-
visitLiteral
Description copied from interface:Expression.VisitorVisits a literal expression node in the AST.This method is called by the accept method of a Literal expression to dispatch the visit operation to the appropriate visitor implementation.
- Specified by:
visitLiteralin interfaceExpression.Visitor<Object>- Parameters:
expr- the literal expression node to visit- Returns:
- the result of the visit operation, type T
-
visitIdentifier
Description copied from interface:Expression.VisitorVisits an identifier expression node in the AST.This method is called by the accept method of an Identifier expression node to dispatch the visit to the appropriate visitor implementation.
- Specified by:
visitIdentifierin interfaceExpression.Visitor<Object>- Parameters:
expr- the identifier expression node to visit- Returns:
- the result of the visit operation, type T
-
visitSelect
Description copied from interface:Expression.VisitorVisits a select expression node in the AST.Processes a field selection operation, which can represent either a value fetch (operand.field) or a presence test (has(operand.field)) depending on the isTest flag.
- Specified by:
visitSelectin interfaceExpression.Visitor<Object>- Parameters:
expr- the select expression node to visit- Returns:
- the result of processing the select expression
-
visitCall
Description copied from interface:Expression.VisitorVisits a function call or method invocation expression node.This method is called by the visitor pattern implementation when processing a Call expression node in the abstract syntax tree. It allows for custom handling of function calls and method invocations during traversal of the expression tree.
- Specified by:
visitCallin interfaceExpression.Visitor<Object>- Parameters:
expr- the call expression node to visit, containing the target expression, function name, arguments, and macro flag- Returns:
- the result of processing the call expression, type depends on the specific visitor implementation
-
visitList
Description copied from interface:Expression.VisitorVisits a list expression node in the AST.This method is called by the accept method of ListExpression to dispatch the visit operation to the appropriate visitor implementation.
- Specified by:
visitListin interfaceExpression.Visitor<Object>- Parameters:
expr- the list expression node to visit- Returns:
- the result of the visit operation, type T
-
visitMap
Description copied from interface:Expression.VisitorVisits a map expression node in the AST.Invoked by the accept method of a MapExpression to dispatch the visit to the appropriate visitor implementation.
- Specified by:
visitMapin interfaceExpression.Visitor<Object>- Parameters:
expr- the map expression node to visit- Returns:
- the result of the visit operation as defined by the visitor implementation
-
visitStruct
Description copied from interface:Expression.VisitorVisits a struct expression node in the AST.This method is called by the accept method of a Struct expression node to dispatch the visit operation to the appropriate visitor implementation.
- Specified by:
visitStructin interfaceExpression.Visitor<Object>- Parameters:
expr- the struct expression node to visit- Returns:
- the result of the visit operation, type T
-
visitComprehension
Description copied from interface:Expression.VisitorVisits a comprehension expression node in the AST.Processes a comprehension expression which represents operations that generate lists or maps through iteration and accumulation. The comprehension includes an iteration variable and range, an accumulator variable with its initialization, a loop condition and step expression, and a final result expression.
- Specified by:
visitComprehensionin interfaceExpression.Visitor<Object>- Parameters:
expr- the comprehension expression node to visit- Returns:
- the result of processing the comprehension expression
-
visitUnary
Description copied from interface:Expression.VisitorVisits a unary expression node in the AST.Processes a unary operation such as logical NOT (!) or numeric negation (-) by delegating to the appropriate visitor method.
- Specified by:
visitUnaryin interfaceExpression.Visitor<Object>- Parameters:
expr- the unary expression node to visit- Returns:
- the result of the visit operation, type T
-
visitBinary
Description copied from interface:Expression.VisitorVisits a binary expression node in the AST.Processes a binary operation by delegating to the appropriate visitor method based on the operator and operands.
- Specified by:
visitBinaryin interfaceExpression.Visitor<Object>- Parameters:
expr- the binary expression node to visit- Returns:
- the result of the visit operation, type T
-
visitConditional
Description copied from interface:Expression.VisitorVisits a conditional expression node in the AST.Processes a conditional expression that evaluates a condition and returns one of two possible values based on the result. The conditional expression follows the form "condition ? then : otherwise".
- Specified by:
visitConditionalin interfaceExpression.Visitor<Object>- Parameters:
expr- the conditional expression to visit- Returns:
- the result of the visit operation
-
visitIndex
Description copied from interface:Expression.VisitorVisits an index expression node in the AST.Processes an index operation that accesses elements from collections such as lists, maps, or strings using the specified operand and index expressions.
- Specified by:
visitIndexin interfaceExpression.Visitor<Object>- Parameters:
expr- the index expression to visit, containing an operand and an index- Returns:
- the result of processing the index expression, type determined by the visitor implementation
-