Class Interpreter

java.lang.Object
com.libdbm.cel.Interpreter
All Implemented Interfaces:
Expression.Visitor<Object>

public class Interpreter extends Object implements Expression.Visitor<Object>
Interpreter for evaluating CEL expressions.

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 Details

    • Interpreter

      public Interpreter(Map<String,Object> variables, Functions functions)
      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

      public Object evaluate(Expression expr)
      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

      public Object visitLiteral(Literal expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitLiteral in interface Expression.Visitor<Object>
      Parameters:
      expr - the literal expression node to visit
      Returns:
      the result of the visit operation, type T
    • visitIdentifier

      public Object visitIdentifier(Identifier expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitIdentifier in interface Expression.Visitor<Object>
      Parameters:
      expr - the identifier expression node to visit
      Returns:
      the result of the visit operation, type T
    • visitSelect

      public Object visitSelect(Select expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitSelect in interface Expression.Visitor<Object>
      Parameters:
      expr - the select expression node to visit
      Returns:
      the result of processing the select expression
    • visitCall

      public Object visitCall(Call expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitCall in interface Expression.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

      public Object visitList(ListExpression expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitList in interface Expression.Visitor<Object>
      Parameters:
      expr - the list expression node to visit
      Returns:
      the result of the visit operation, type T
    • visitMap

      public Object visitMap(MapExpression expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitMap in interface Expression.Visitor<Object>
      Parameters:
      expr - the map expression node to visit
      Returns:
      the result of the visit operation as defined by the visitor implementation
    • visitStruct

      public Object visitStruct(Struct expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitStruct in interface Expression.Visitor<Object>
      Parameters:
      expr - the struct expression node to visit
      Returns:
      the result of the visit operation, type T
    • visitComprehension

      public Object visitComprehension(Comprehension expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitComprehension in interface Expression.Visitor<Object>
      Parameters:
      expr - the comprehension expression node to visit
      Returns:
      the result of processing the comprehension expression
    • visitUnary

      public Object visitUnary(Unary expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitUnary in interface Expression.Visitor<Object>
      Parameters:
      expr - the unary expression node to visit
      Returns:
      the result of the visit operation, type T
    • visitBinary

      public Object visitBinary(Binary expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitBinary in interface Expression.Visitor<Object>
      Parameters:
      expr - the binary expression node to visit
      Returns:
      the result of the visit operation, type T
    • visitConditional

      public Object visitConditional(Conditional expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitConditional in interface Expression.Visitor<Object>
      Parameters:
      expr - the conditional expression to visit
      Returns:
      the result of the visit operation
    • visitIndex

      public Object visitIndex(Index expr)
      Description copied from interface: Expression.Visitor
      Visits 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:
      visitIndex in interface Expression.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