5.4.2.13 On...GoTo Statement
Note
This section describes the implementation of MS-VBAL §5.4.2.13 On...GoTo Statement.
Syntax
| AST node | Instruction kind | Notes |
|---|---|---|
| OnGoToStatementNode | JumpTable |
Instruction.Targets holds one offset per label, in source order. |
An On expression GoTo label, ... statement is represented by OnGoToStatementNode, and lowers to the
InstructionKind JumpTable
(RD-VBAL §3.5.2 Instruction).
OnGoToStatementNode holds its targets in a Labels list. Each target is an expression naming or numbering a
label or line, with no static link to the label node: the same convention as GoTo
(RD-VBAL §3.4.2 Simple Statements).
OnGoToStatementNode and OnGoSubStatementNode
are separate node types, rather than one shared node with a Kind.
Static Semantics
Lowering resolves each label in the list into Instruction.Targets
(RD-VBAL §3.5.3 Lowering Block Statements).
| Condition | Result |
|---|---|
| The procedure defines the label. | Its Targets entry is the label's offset. |
The procedure does not define the label, e.g. Missing in On n GoTo A, Missing. |
Its Targets entry is null, and lowering reports VBC09309 Label not defined. Each undefined label in the list is reported on its own. |
Runtime Semantics
On…GoTo and On…GoSub share one selector algorithm
(RD-VBAL §5.4.2.16 On...GoSub Statement). The executor applies it
to a JumpTable instruction as follows:
- The selector expression is evaluated once.
- The evaluated value is Let-coerced to
Integer. Let n be the coerced value. - If n is zero, or greater than the number of labels, execution falls through to the next instruction without branching.
- Otherwise, if n is negative or greater than 255, run-time error 5 is raised.
- Otherwise, execution branches to the n'th label (1-based) in
Instruction.Targets.
Step 3 takes precedence over step 4, in the order MS-VBAL §5.4.2.13 lists them.
| Condition | Run-time error |
|---|---|
| n is negative, or greater than 255 (step 4). | 5 — Invalid procedure call or argument |
A label with no statement after it, including the end-label of the lexically enclosing procedure declaration, resolves to the offset right past the last instruction. A branch to it completes the activation as if execution had reached the end of the procedure body (RD-VBAL §3.5.1 InstructionList).
Implementation
RDCore.Runtime.Execution.ProcedureExecutordispatchesJumpTableinstructions (RD-VBAL §3.5.4 Execution).RDCore.Runtime.Execution.JumpTableEvaluatorevaluates the selector and Let-coerces it toInteger(steps 1 and 2).ProcedureExecutorapplies steps 3 to 5, in one code path shared byJumpTableandGoSubTable.JumpTableEvaluatorLet-coerces the selector by callingVBNumericLetCoercionTypeRuntimeSemanticsdirectly, bypassing the let-coercion provider.ConditionEvaluatorlikewise bypasses the provider to force a condition toBoolean(RD-VBAL §3.5.4 Execution).
⏮️ RD-VBAL §5.4.2.12 GoTo Statement | ⏭️ RD-VBAL §5.4.2.14 GoSub Statement