v4 Semantics: loop rules and scope
- Track loop legality within the current function only.
- Separate initializer scope from body block scope.
- Analyze both operands and unreachable-looking statements before execution.
1 / 6
while (true) {
fn inner() { break; }
continue;
}
| Traversal point | Function context | Loop depth | Decision |
|---|---|---|---|
| outer while body | top-level | 1 | loop control allowed |
| enter inner function | inner | 0 | reset loop context |
| inner break | inner | 0 | break outside loop |
| leave inner | top-level | 1 | restore outer context |
| outer continue | top-level | 1 | valid |
Merely counting lexical loop nodes without respecting function boundaries would accept the invalid break. A loop declared inside inner would give inner its own valid target.
An analyzer uses one loopDepth counter across nested function calls during traversal. It accepts the worked example, then rejects the outer continue after setting depth to zero. What two operations must be paired?
Construct a for loop whose body shadows its initializer variable and a post clause that uses a body-only variable. Predict acceptance of the first and the undefined-name diagnostic for the second without executing either program.
Carry a placement-valid AST into lowering. Preserve tests for function boundaries, scope shadowing, and static traversal of skipped runtime branches.