Class GuardedConditionEvaluator

java.lang.Object
org.ek9lang.compiler.phase7.generator.AbstractGenerator
org.ek9lang.compiler.phase7.generator.GuardedConditionEvaluator

public final class GuardedConditionEvaluator extends AbstractGenerator
Evaluates conditions with optional guard variables for all control flow constructs.

Handles 4 distinct cases:

  1. Guard WITH condition AND needs check (e.g., <-, ?=) - Generates LOGICAL_AND_BLOCK with short-circuit evaluation
  2. Guard WITH condition but NO check needed (e.g., :=, =) - Just evaluates the condition
  3. Condition only (no guard) - Just evaluates the condition
  4. Guard only (implicit isSet check) AND needs check - Generates QUESTION_OPERATOR for NULL + isSet checks

This provides consistent guard semantics across all control flow constructs (IF, WHILE, DO-WHILE, SWITCH, TRY) with 90-95% null safety enforcement.

The guard double-check pattern ensures:

  1. IS_NULL check (if null → return false)
  2. _isSet() check (if not null → check if set)
  3. Variable usage (only if both checks pass)
  • Constructor Details

  • Method Details

    • evaluate

      public ConditionEvaluation evaluate(EK9Parser.ExpressionContext conditionExpr, EK9Parser.PreFlowStatementContext preFlowStmt, VariableDetails conditionResult, String scopeId, DebugInfo debugInfo)
      Evaluates a condition expression that may have guard variables.

      This is the main entry point that handles all 4 cases of guard/condition combinations. The returned ConditionEvaluation contains both the IR instructions for evaluating the condition and the name of the primitive boolean variable for backend branching.

      Parameters:
      conditionExpr - Optional explicit condition expression (may be null for guard-only)
      preFlowStmt - Optional guard variable declaration/assignment (may be null for condition-only)
      conditionResult - Variable to store the condition result (EK9 Boolean type)
      scopeId - Scope ID where condition evaluation happens
      debugInfo - Debug information for error reporting
      Returns:
      ConditionEvaluation containing instructions and primitive condition variable name
      Throws:
      CompilerException - if neither condition nor guard is provided
    • getGuardOperatorType

      Integer getGuardOperatorType(EK9Parser.PreFlowStatementContext preFlowStmt)
      Determines the guard operator type from a preFlowStatement. Returns the operator token type, or null if not a guard pattern. Package-private to allow IfStatementGenerator to use for guard variable collection.
      Parameters:
      preFlowStmt - The preFlowStatement context
      Returns:
      The operator token type (LEFT_ARROW, ASSIGN, ASSIGN2, GUARD, ASSIGN_UNSET), or null
    • requiresGuardCheck

      boolean requiresGuardCheck(Integer operatorType)
      Checks if the given operator requires guard checks (null check + isSet check). Returns true for operators that need guard checks: <-, ?= Returns false for operators that don't: :=, = Package-private to allow IfStatementGenerator to use for guard variable collection.
      Parameters:
      operatorType - The operator token type
      Returns:
      true if guard checks are required, false otherwise