Interface Evaluator

All Known Implementing Classes:
AstEvaluator

public interface Evaluator
Evaluator Interface 求值器接口

Base interface for expression evaluators that traverse and evaluate AST nodes.

表达式求值器的基础接口,用于遍历和求值 AST 节点。

Features | 主要功能:

  • Evaluate AST nodes with context - 使用上下文求值AST节点
  • Type-safe evaluation with automatic conversion - 类型安全求值与自动转换
  • Capability check for node support - 节点支持能力检查

Usage Examples | 使用示例:

Evaluator evaluator = AstEvaluator.getInstance();
Node node = Parser.parse("1 + 2");
Object result = evaluator.evaluate(node, ctx);
int typed = evaluator.evaluate(node, ctx, Integer.class);

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Depends on implementation - 空值安全: 取决于实现

Performance | 性能特性:

  • Time complexity: O(n) per evaluate call where n is the number of AST nodes - 时间复杂度: evaluate 为 O(n),n为 AST 节点数量
  • Space complexity: O(d) call stack where d is the expression nesting depth - 空间复杂度: O(d) 调用栈,d为表达式嵌套深度
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default boolean
    Check if this evaluator can evaluate the given node 检查此求值器是否可以求值给定节点
    Evaluate an AST node 求值 AST 节点
    default <T> T
    evaluate(Node node, EvaluationContext context, Class<T> targetType)
    Evaluate an AST node with type conversion 求值 AST 节点并转换类型
  • Method Details

    • evaluate

      Object evaluate(Node node, EvaluationContext context)
      Evaluate an AST node 求值 AST 节点
      Parameters:
      node - the AST node | AST 节点
      context - the evaluation context | 求值上下文
      Returns:
      the evaluation result | 求值结果
    • evaluate

      default <T> T evaluate(Node node, EvaluationContext context, Class<T> targetType)
      Evaluate an AST node with type conversion 求值 AST 节点并转换类型
      Type Parameters:
      T - the target type | 目标类型
      Parameters:
      node - the AST node | AST 节点
      context - the evaluation context | 求值上下文
      targetType - the target type | 目标类型
      Returns:
      the evaluation result | 求值结果
    • canEvaluate

      default boolean canEvaluate(Node node)
      Check if this evaluator can evaluate the given node 检查此求值器是否可以求值给定节点
      Parameters:
      node - the AST node | AST 节点
      Returns:
      true if can evaluate | 如果可以求值返回 true