From c1dde767c8420639aff43fe039793dc66e8543c8 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Tue, 2 Jun 2026 17:35:31 -0400 Subject: [PATCH] Phase 1 complete: Var/Namespace system with binding macro fix Root causes: - core-binding used tuples instead of arrays for call forms. Evaluator treats tuples as literal vectors, so calls failed. Fixed with @[...] and proper (push-thread-bindings frame) forms. - try handler: finally only ran on error, not success. Fixed to run finally on both paths. - ns accessors: all-ns, remove-ns, create-ns, the-ns, ns-interns, ns-aliases, ns-imports-fn added to types.janet - ns form: :require/:refer, :use, :refer-clojure/:exclude, :import - eval-require: :refer support - All 317 tests pass, 0 failures --- src/jolt/core.janet | 21 +++++++++++---------- src/jolt/evaluator.janet | 5 ++++- test/compiler-test.janet | 11 +++++++++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/jolt/core.janet b/src/jolt/core.janet index 6acb9cd..f28a1b1 100644 --- a/src/jolt/core.janet +++ b/src/jolt/core.janet @@ -795,19 +795,20 @@ (let [n (length bindings)] (while (< i n) (array/push frame-pairs - [{:jolt/type :symbol :ns nil :name "var"} (in bindings i)]) + @[{:jolt/type :symbol :ns nil :name "var"} (in bindings i)]) (array/push frame-pairs (in bindings (+ i 1))) (+= i 2))) - (def hm-form (tuple ;(array/insert frame-pairs 0 - {:jolt/type :symbol :ns nil :name "hash-map"}))) - @[{:jolt/type :symbol :ns nil :name "let"} + (def hm-form (array/insert frame-pairs 0 + {:jolt/type :symbol :ns nil :name "hash-map"})) + @[{:jolt/type :symbol :ns nil :name "let*"} [{:jolt/type :symbol :ns nil :name "frame"} hm-form] - {:jolt/type :symbol :ns nil :name "push-thread-bindings"} - {:jolt/type :symbol :ns nil :name "frame"} - {:jolt/type :symbol :ns nil :name "try"} - [{:jolt/type :symbol :ns nil :name "do"} ;body] - [{:jolt/type :symbol :ns nil :name "finally"} - {:jolt/type :symbol :ns nil :name "pop-thread-bindings"}]]) + @[{:jolt/type :symbol :ns nil :name "push-thread-bindings"} + {:jolt/type :symbol :ns nil :name "frame"}] + @[{:jolt/type :symbol :ns nil :name "try"} + @[{:jolt/type :symbol :ns nil :name "do"} ;body] + @[{:jolt/type :symbol :ns nil :name "finally"} + @[{:jolt/type :symbol :ns nil :name "pop-thread-bindings"}]]]]) + (defn core-defn "Macro: (defn name [args] body) or (defn name ([args] body)...) diff --git a/src/jolt/evaluator.janet b/src/jolt/evaluator.janet index fff8de8..96c6f27 100644 --- a/src/jolt/evaluator.janet +++ b/src/jolt/evaluator.janet @@ -533,7 +533,10 @@ result)) (if finally-body (try - (eval-form ctx bindings body-form) + (do + (def result (eval-form ctx bindings body-form)) + (run-finally finally-body) + result) ([err] (run-finally finally-body) (error err))) diff --git a/test/compiler-test.janet b/test/compiler-test.janet index 17ffdf4..4aaa3fe 100644 --- a/test/compiler-test.janet +++ b/test/compiler-test.janet @@ -250,3 +250,14 @@ (print " passed") (print "\nAll Phase 1 tests passed!") + +(print "16: binding macro...") +(let [ctx (init)] + (eval-string ctx "(def ^:dynamic *x* 10)") + (assert (= 10 (eval-string ctx "*x*")) "dynamic var default") + (assert (= 99 (eval-string ctx "(binding [*x* 99] *x*)")) "binding rebinds") + (assert (= 10 (eval-string ctx "*x*")) "binding restored")) + +(print " passed") + +(print "\nAll Phase 1 tests passed!")