Add AST dumper and improve upvalue analysis

The `UpvalueAnalyzer` now correctly identifies which lambdas capture
which variable declarations, rather than just marking declarations that
need boxing. This information is stored in a `capture_map`.

The `Binder` has been updated to use this new `capture_map` instead of a
`HashSet` of boxed declarations. The `DefLocal` node in `bound_nodes.rs`
now stores a `captured_by` field, which is a list of lambda identities
that capture the local variable.

A new `dumper` module has been added to provide a human-readable
representation of the bound AST, including information about captured
variables.

The `VM` has been updated to use the `captured_by` field to determine if
a local variable needs to be stored in a `Cell`, rather than relying on
a boolean `is_boxed` flag.

An example script `extreme_capture.myc` has been added to test deep
nesting and variable capture.

A new "Dump AST" button has been added to the UI, which uses the new
`Dumper` to display the bound AST.
This commit is contained in:
Michael Schimmel
2026-02-18 00:35:08 +01:00
parent 4d9adccab5
commit 25e3bc1d01
10 changed files with 269 additions and 49 deletions
+25
View File
@@ -0,0 +1,25 @@
;; Output: 36
(do
;; Excessive capture test
;; This script creates deep nesting where the inner-most lambda
;; grabs variables from every single outer scope.
(def excessive (fn [v1]
(do
(def v2 2)
(fn [v3]
(do
(def v4 4)
(fn [v5]
(do
(def v6 6)
(fn [v7]
(do
(def v8 8)
;; v1, v3, v5, v7 are parameters (captured as upvalues)
;; v2, v4, v6, v8 are locals (captured from outer scopes)
(+ v1 v2 v3 v4 v5 v6 v7 v8))))))))))
;; Execution: (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) = 36
((((excessive 1) 3) 5) 7)
)