feat(compiler): lower for...in to while in all forms#68
Conversation
Add for...in iteration in every form — array single, index (i, item), map (k, v) and numeric range with step and descending — each lowered to a while machine over the Increment B loop-context model. There is no FOR opcode; everything is a lowering. Type checker: validate the iteration subject (array, map or numeric range; else E0501), require the two-identifier map form (E0502 on a single identifier, suggesting `for k in m.keys`), reject a literal descending range with no negative step (E0503), and forbid reassigning an iterator variable in the body (E0504). Iteration variables infer as the element type (item), int (the index i) and string (the map key k); value and array-element types stay unknown until generics (Sprint 5), so comparison against an unknown operand is now permissive. Compiler: each form lowers over synthetic for-scope locals (counter, limit, step, keys array, map) with the visible iteration variables re-bound per iteration in a body scope. continue forward-jumps to the increment step via a new settable continue-site list on LoopContext, so the counter always advances; break and the synthetic PopN cleanup land at the loop exit. Array literals now compile to NewArray so arrays are constructible from source. VM: implement the NewArray, GetIndex (array by int with E5101, map by string returning nil on a miss) and GetProperty (array length, map keys) arms the lowering relies on. GrobMap now uses OrderedDictionary so the key set walks in insertion order. Map iteration has no source construction path in v1 (there is no map literal in the parser — out-of-scope parser work), so the map form is covered through the type checker via a fn-param annotation, a hand-built compiler AST and a hand-built VM lowering; array and range run end-to-end from source. New code is covered at 97%+; the only uncovered lines are defensive slot-overflow guards. Registers E0501-E0504 in the error registry and ErrorCatalog (98 codes). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Warning Review limit reached
More reviews will be available in 43 minutes and 56 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThis PR implements Changesfor...in loop full pipeline
Sequence DiagramssequenceDiagram
actor Source
participant TypeChecker
participant Compiler
participant VirtualMachine
Source->>TypeChecker: for v in xs { }
TypeChecker->>TypeChecker: ResolveIterationVariableTypes(xs)
note over TypeChecker: Dispatch: array → int index + Unknown elem<br>range → int counter/limit/step<br>map → string key + Unknown val
TypeChecker->>TypeChecker: PushScope, register iteration vars
TypeChecker->>TypeChecker: VisitBody (_loopDepth++)
TypeChecker-->>Compiler: typed AST, no diagnostics
Compiler->>Compiler: VisitForIn → EmitArrayForIn / EmitRangeForIn / EmitMapForIn
Compiler->>Compiler: EmitForInLoop: condition + ConditionalJump(exit)
Compiler->>Compiler: LoopContext(hasForwardContinue=true)
Compiler->>Compiler: body scope + PopN cleanup
Compiler->>Compiler: Backpatch continue sites → increment step
Compiler->>Compiler: Loop backward jump + patch exit/break
Compiler-->>VirtualMachine: Chunk bytecode
VirtualMachine->>VirtualMachine: NewArray: pop N values, push GrobArray
VirtualMachine->>VirtualMachine: GetProperty: array.length / map.keys
VirtualMachine->>VirtualMachine: GetIndex: arr[i] bounds-check (E5101) / map[k] nil-on-missing
VirtualMachine-->>Source: stdout output
Estimated code review effort🎯 5 (Critical) | ⏱️ ~120 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/Grob.Compiler/TypeChecker.Expressions.cs`:
- Around line 356-358: Fix the typographical error in the error message string
parameter of the EmitError method call for ErrorCatalog.E0503. The quoted
example in the message has mismatched quotes: '..' step -1' should be corrected
to either '.. step -1' (with a single opening quote before the dots) or .. step
-1 (with quotes removed entirely) to maintain proper quote balancing in the
diagnostic message text.
In `@src/Grob.Vm/VirtualMachine.cs`:
- Around line 484-485: The LINQ Select() call in the GrobArray initialization
near the map!.InsertionOrderKeys line is allocating an iterator wrapper on every
dispatch iteration, violating the hot path performance guidelines. Replace the
.Select(GrobValue.FromString) LINQ expression with a manual loop (such as a
foreach loop) that converts each key from InsertionOrderKeys to a GrobValue
using FromString, building a collection or array to pass to the GrobArray
constructor without the allocation overhead of the iterator wrapper.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: f6d60db2-2b15-423c-9005-425006057bbe
📒 Files selected for processing (17)
docs/design/grob-error-codes.mdsrc/Grob.Compiler/Compiler.ControlFlow.cssrc/Grob.Compiler/Compiler.Expressions.cssrc/Grob.Compiler/Compiler.cssrc/Grob.Compiler/TypeChecker.ControlFlow.cssrc/Grob.Compiler/TypeChecker.Expressions.cssrc/Grob.Compiler/TypeChecker.Statements.cssrc/Grob.Compiler/TypeChecker.cssrc/Grob.Core/ErrorCatalog.cssrc/Grob.Core/GrobMap.cssrc/Grob.Core/GrobType.cssrc/Grob.Vm/VirtualMachine.cstests/Grob.Compiler.Tests/CompilerForInTests.cstests/Grob.Compiler.Tests/TypeCheckerForInTests.cstests/Grob.Compiler.Tests/TypeCheckerTests.cstests/Grob.Integration.Tests/Sprint4IncrementCTests.cstests/Grob.Vm.Tests/VirtualMachineForInTests.cs
The descending-range diagnostic showed the example as '..' step -1' with a stray quote between the dots and step. Use the balanced, concrete example '3..0 step -1'. Raised by CodeRabbit on PR #68. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
GrobMap.InsertionOrderKeys returned _entries.Keys.ToList(), copying the key collection on a property access (Sonar S2365), and the VM's GetProperty 'keys' arm wrapped it in a LINQ Select on the dispatch path (against the no-LINQ-on-hot-paths guideline). The property now returns the backing OrderedDictionary's live ordered-key view without copying, and the VM builds the GrobArray with a manual indexed loop. The caller snapshots into a GrobArray, so exposing the live view is safe. No behaviour change. Raised by CodeRabbit and Sonar (S2365) on PR #68. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|



Add for...in iteration in every form — array single, index (i, item), map (k, v) and numeric range with step and descending — each lowered to a while machine over the Increment B loop-context model. There is no FOR opcode; everything is a lowering.
Type checker: validate the iteration subject (array, map or numeric range; else E0501), require the two-identifier map form (E0502 on a single identifier, suggesting
for k in m.keys), reject a literal descending range with no negative step (E0503), and forbid reassigning an iterator variable in the body (E0504). Iteration variables infer as the element type (item), int (the index i) and string (the map key k); value and array-element types stay unknown until generics (Sprint 5), so comparison against an unknown operand is now permissive.Compiler: each form lowers over synthetic for-scope locals (counter, limit, step, keys array, map) with the visible iteration variables re-bound per iteration in a body scope. continue forward-jumps to the increment step via a new settable continue-site list on LoopContext, so the counter always advances; break and the synthetic PopN cleanup land at the loop exit. Array literals now compile to NewArray so arrays are constructible from source.
VM: implement the NewArray, GetIndex (array by int with E5101, map by string returning nil on a miss) and GetProperty (array length, map keys) arms the lowering relies on. GrobMap now uses OrderedDictionary so the key set walks in insertion order.
Map iteration has no source construction path in v1 (there is no map literal in the parser — out-of-scope parser work), so the map form is covered through the type checker via a fn-param annotation, a hand-built compiler AST and a hand-built VM lowering; array and range run end-to-end from source. New code is covered at 97%+; the only uncovered lines are defensive slot-overflow guards.
Registers E0501-E0504 in the error registry and ErrorCatalog (98 codes).
Summary by CodeRabbit
Release Notes
New Features
for...inloops to iterate over arrays, maps, and numeric ranges with automatic iterator variable bindingbreakandcontinuestatements now work withinfor...inloopsDocumentation
for...inusage including non-iterable subjects, incorrect single-identifier map iteration, descending ranges without explicit steps, and immutable iterator variables