Class GuardedConditionEvaluator
java.lang.Object
org.ek9lang.compiler.phase7.generator.AbstractGenerator
org.ek9lang.compiler.phase7.generator.GuardedConditionEvaluator
Evaluates conditions with optional guard variables for all control flow constructs.
Handles 4 distinct cases:
- Guard WITH condition AND needs check (e.g.,
<-,?=) - Generates LOGICAL_AND_BLOCK with short-circuit evaluation - Guard WITH condition but NO check needed (e.g.,
:=,=) - Just evaluates the condition - Condition only (no guard) - Just evaluates the condition
- 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:
- IS_NULL check (if null → return false)
- _isSet() check (if not null → check if set)
- Variable usage (only if both checks pass)
-
Field Summary
Fields inherited from class AbstractGenerator
debugInfoCreator, instructionBuilder, stackContext, typeNameOrException -
Constructor Summary
ConstructorsConstructorDescriptionGuardedConditionEvaluator(IRGenerationContext stackContext, GeneratorSet generators) -
Method Summary
Modifier and TypeMethodDescriptionevaluate(EK9Parser.ExpressionContext conditionExpr, EK9Parser.PreFlowStatementContext preFlowStmt, VariableDetails conditionResult, String scopeId, DebugInfo debugInfo) Evaluates a condition expression that may have guard variables.(package private) IntegergetGuardOperatorType(EK9Parser.PreFlowStatementContext preFlowStmt) Determines the guard operator type from a preFlowStatement.(package private) booleanrequiresGuardCheck(Integer operatorType) Checks if the given operator requires guard checks (null check + isSet check).
-
Constructor Details
-
GuardedConditionEvaluator
-
-
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 happensdebugInfo- Debug information for error reporting- Returns:
- ConditionEvaluation containing instructions and primitive condition variable name
- Throws:
CompilerException- if neither condition nor guard is provided
-
getGuardOperatorType
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
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
-