Class SwitchStatementGenerator
java.lang.Object
org.ek9lang.compiler.phase7.generator.AbstractGenerator
org.ek9lang.compiler.phase7.generator.SwitchStatementGenerator
- All Implemented Interfaces:
Function<EK9Parser.SwitchStatementExpressionContext, List<IRInstr>>
public final class SwitchStatementGenerator
extends AbstractGenerator
implements Function<EK9Parser.SwitchStatementExpressionContext, List<IRInstr>>
Generates IR for switch statements and expressions using CONTROL_FLOW_CHAIN.
Follows the same pattern as IfStatementGenerator.
Supports both statement and expression forms:
- Statement form:
switch expr- no return value - Expression form:
result <- switch expr- returns value assigned to return variable
Current implementation supports:
- Literal case comparisons (case 1, case 2, etc.)
- Integer type evaluation variable
- Default case
- Guard variables with all operators (<-, :=, :=?, ?=)
- Expression form with ARC-safe result assignment
Preflow handling differs by form:
- Statement form — a guard with a check (
<-,:=?,?=) skips the whole switch when the guard is unset (consistent with WHILE/FOR/TRY), viaLoopGuardHelper.evaluateGuardVariable(EK9Parser.PreFlowStatementContext, String)+ guard-entry-check wrapping. - Expression form — a body-skipping guard (
<-,:=?,?=) gates the switch exactly as in statement form (routed throughGuardedScopeOrchestrator); the return variable's init is hoisted outside the guard check so thelhstakes it on the skip path. A non-guard preflow (:=, or none) ALWAYS executes and is emitted unconditionally viaLoopGuardHelper.evaluatePreFlowSetupOnly(EK9Parser.PreFlowStatementContext)before the switch — it must be emitted, not dropped, or the control expression LOADs an undeclared variable (JVM VerifyError / native empty output).
NOT yet supported: - Enum case comparisons - Multiple literals per case (case 1, 2, 3) - Expression cases (case < 12, case > 50)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecord to hold evaluation variable information. -
Field Summary
Fields inherited from class AbstractGenerator
debugInfoCreator, instructionBuilder, MAX_MESSAGE_LENGTH, stackContext, typeNameOrException -
Constructor Summary
ConstructorsConstructorDescriptionSwitchStatementGenerator(IRGenerationContext stackContext, GeneratorSet generators) -
Method Summary
Modifier and TypeMethodDescriptionMethods inherited from class AbstractGenerator
addCoverageProbe, createTempVariable, createTempVariableFromContext, extractReturnType, getRecordedSymbolOrException, processBlockStatements, processBlockStatements, truncateMessage, truncateMessage
-
Constructor Details
-
SwitchStatementGenerator
-
-
Method Details
-
apply
- Specified by:
applyin interfaceFunction<EK9Parser.SwitchStatementExpressionContext, List<IRInstr>>
-