Refactor Binder to use new scope structure

This commit refactors the Binder implementation to better align with the
updated scope structure.
Key changes include:

- Introducing `ExprContext` to distinguish between statement and
  expression contexts.
- Modifying `define_variable` to handle both global and local scope
  definitions more robustly.
- Updating `resolve_variable` to correctly handle upvalue captures,
  especially for global addresses.
- Adjusting `bind` and related functions to use the new `ExprContext`.
- Updating the refactoring log to reflect completed phases.
This commit is contained in:
Michael Schimmel
2026-03-10 17:20:47 +01:00
parent 3b063dc2c9
commit a78e72d074
2 changed files with 803 additions and 759 deletions
+16 -9
View File
@@ -20,16 +20,16 @@ Implement strict **Block Scoping** and distinguish between **Statements** and **
## Implementation Plan
### Phase 1: Binder Infrastructure (In Progress)
1. [ ] Update `CompilerScope` to support hierarchical nesting via a stack in `FunctionCompiler`.
2. [ ] Implement `push_scope` and `pop_scope` in `Binder`.
3. [ ] Refactor `resolve_variable` to traverse the scope stack (inner-to-outer).
4. [ ] Update `define_variable` to enforce "No Shadowing" at the current scope level.
### Phase 1: Binder Infrastructure (Completed)
1. [x] Update `CompilerScope` to support hierarchical nesting via a stack in `FunctionCompiler`.
2. [x] Implement `push_scope` and `pop_scope` in `Binder`.
3. [x] Refactor `resolve_variable` to traverse the scope stack (inner-to-outer).
4. [x] Update `define_variable` to enforce "No Shadowing" at the current scope level.
### Phase 2: Statement/Expression Validation
1. [ ] Introduce a mechanism to track "Context" (Expression vs. Statement) during binding.
2. [ ] Validate that `def` is only used in statement positions.
3. [ ] Enforce that the last node in a `BoundKind::Block` is an expression.
### Phase 2: Statement/Expression Validation (Completed)
1. [x] Introduce a mechanism to track "Context" (Expression vs. Statement) during binding.
2. [x] Validate that `def` is only used in statement positions.
3. [x] Enforce that the last node in a `BoundKind::Block` is an expression.
### Phase 3: VM Compatibility
1. [ ] Ensure the VM handles "Void" results from statements correctly.
@@ -43,3 +43,10 @@ Implement strict **Block Scoping** and distinguish between **Statements** and **
- Analyzed `design-flaw.myc`.
- Defined the "Statement" concept to solve the uninitialized variable problem.
- Initialized this log.
### Phase 1 & 2 Completed
- Introduced scope stack into `FunctionCompiler`.
- Introduced `ExprContext` (`Expression` / `Statement`) in the `Binder`.
- `def` now acts strictly as a statement.
- Variables defined inside nested scopes (e.g. inside `if` or `do`) do not leak into outer local scopes anymore.
- Outstanding behavior to discuss: `def` inside nested scopes at the *root* level still leaks into globals.
+787 -750
View File
File diff suppressed because it is too large Load Diff