Class ForRangeGenerator

java.lang.Object
org.ek9lang.compiler.phase7.generator.AbstractGenerator
org.ek9lang.compiler.phase7.generator.ForRangeGenerator
All Implemented Interfaces:
Function<EK9Parser.ForStatementExpressionContext, List<IRInstr>>

public final class ForRangeGenerator extends AbstractGenerator implements Function<EK9Parser.ForStatementExpressionContext, List<IRInstr>>
Generates IR for for-range loops using FOR_RANGE_POLYMORPHIC instruction.

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.