feat: add block scoping for while/do/done#251
Merged
Conversation
Add scope_stack: List[List[str]] to TypeChecker and BytecodeCompiler. Push a new frame on WhileStart, pop it on WhileEnd. Variables first assigned inside a while block are tracked in the innermost scope frame. On WhileEnd those variables move to out_of_scope and any subsequent PushVar raises UndefinedName. Variables already in scope before the block (pre_scope_vars) or declared global are unaffected. Pattern bindings from `is` checks follow the same scoping rules. For-loop variables are covered automatically because for desugars to while. Add tests: while scoping, for scoping, outer mutation, sibling reuse, global bypass.
A variable first assigned inside a while block is marked out-of-scope on `done`. Reassigning the same name at the function (outer) scope did not clear that marking, so a later read was wrongly rejected as undefined — unlike sibling-while reuse, which did clear it. The pre-scope assignment path now clears the out-of-scope marking too. The scope-recording logic, previously duplicated between variable assignment and `is`-pattern bindings, is extracted into a single `record_var_scope` method so the fix lives in one place.
The bytecode compiler pushed and popped a scope_stack on while boundaries but never read it — scope enforcement lives entirely in the type checker.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
scope_stack: List[List[str]]toTypeCheckerandBytecodeCompilerWhileStart, pop onWhileEnd; variables first assigned inside a while block are block-local and inaccessible afterdonepre_scope_vars) andglobaldeclarations are unaffectedischecks follow the same rules;forloop variables covered automatically via desugar-to-whileTest plan
test_scope: while scoping, for scoping, outer mutation, sibling reuse, global bypassCloses #246