Class ForRangeGenerator
- All Implemented Interfaces:
Function<EK9Parser.ForStatementExpressionContext, List<IRInstr>>
POLYMORPHIC DESIGN: Works with ANY type that implements required operators: - <=> operator for direction detection and loop conditions - ++ operator for increment (when no BY clause, ascending) - -- operator for decrement (when no BY clause, descending) - += operator (when BY clause present - works for both directions)
RUNTIME DIRECTION DETECTION: Uses start <=> end to determine: - Negative result (-1): ascending range → use ++ or += - Positive result (1): descending range → use -- or += - Zero result (0): equal range → single iteration
FAIL-FAST ASSERTIONS: Validates all inputs are set to prevent silent bugs in expression form where unset inputs could produce misleading results.
Range type is obtained from resolved symbols, NOT hardcoded. Supports Integer, Float, Date, Duration, and custom types.
NEW IR STRUCTURE: Uses FOR_RANGE_POLYMORPHIC instruction which stores user body ONCE (not 3x like nested CONTROL_FLOW_CHAIN approach). Backends read body once and emit it multiple times for different directions.
-
Field Summary
Fields inherited from class AbstractGenerator
debugInfoCreator, instructionBuilder, stackContext -
Constructor Summary
ConstructorsConstructorDescriptionForRangeGenerator(IRGenerationContext stackContext, GeneratorSet generators) -
Method Summary
Modifier and TypeMethodDescriptionMain orchestration method for for-range loop IR generation.Methods inherited from class AbstractGenerator
createTempVariable, getRecordedSymbolOrException
-
Constructor Details
-
ForRangeGenerator
ForRangeGenerator(IRGenerationContext stackContext, GeneratorSet generators)
-
-
Method Details
-
apply
Main orchestration method for for-range loop IR generation. Generates FOR_RANGE_POLYMORPHIC instruction with initialization and body.Scope structure (follows CONTROL_FLOW_CHAIN pattern):
Outer Scope: Loop wrapper for future guards Loop Scope: Initialization temps (start, end, by, direction, current) Body Scope: Body iteration temps, enters/exits each iteration
- Specified by:
applyin interfaceFunction<EK9Parser.ForStatementExpressionContext, List<IRInstr>>
-