From df1e836cda005e2175d331989edf03cc2dd1d5a4 Mon Sep 17 00:00:00 2001 From: Yogthos Date: Wed, 3 Jun 2026 13:19:56 -0400 Subject: [PATCH] =?UTF-8?q?Phase=2013:=20Protocol=20Completion=20=E2=80=94?= =?UTF-8?q?=20reify=20dispatch,=20#()=20reader,=20IFn=20protocol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - reader.janet: rewrite read-anon-fn to handle % arg references % → gensym, %1/%2 → sorted gensyms, replaces all matching % refs - evaluator.janet: IFn protocol support in default invocation arm Before erroring "Cannot call X as a function", checks for: 1) type-registry IFn/-invoke method (extend-type protocols) 2) :jolt/protocol-methods :-invoke (reified objects) - test/phase13-test.janet: 4 test sections (28-31) 28: reify dispatch — protocol methods on reified objects 29: #() anon-fn — % and %1/%2 arg handling 30: extend-type — protocol method dispatch for deftypes 31: clojure.walk loading — keywordize-keys loads correctly - All pass: 316 ok, 1 fail (pre-existing, unchanged) --- .dirge/memory/MEMORY.md | 8 ++-- .dirge/memory/PITFALLS.md | 8 ++-- .dirge/skills/.usage.json | 68 ++++++++++++++++----------------- .dirge/skills/jolt-dev/SKILL.md | 6 +++ src/jolt/evaluator.janet | 10 ++++- src/jolt/reader.janet | 30 ++++++++++++++- test/phase13-test.janet | 44 +++++++++++++++++++++ 7 files changed, 130 insertions(+), 44 deletions(-) create mode 100644 test/phase13-test.janet diff --git a/.dirge/memory/MEMORY.md b/.dirge/memory/MEMORY.md index c62c8b9..40b1a3d 100644 --- a/.dirge/memory/MEMORY.md +++ b/.dirge/memory/MEMORY.md @@ -1,7 +1,7 @@ -Protocol system: Type registry in context env (:type-registry) maps type-tag→proto-name→method-name→fn. Three dispatch special forms: protocol-dispatch (resolves method via registry or reified methods), register-method (stores impl in registry), make-reified (creates anonymous object with :jolt/protocol-methods). fn* forms emitted by extend-type/extend-protocol MUST be @[...] (array) for eval-list dispatch. Protocols are maps with :jolt/type :jolt/protocol and :methods map. -§ -Project stats after Phase 10: 7,370 total lines across 35 source/test files. Key sources: compiler.janet (869 lines), evaluator.janet (869 lines), core.janet (1373 lines), types.janet (441 lines), reader.janet (463 lines), main.janet (125 lines), api.janet (93 lines), loader.janet (79 lines), phm.janet (199 lines). Test suite: 315 ok, 2 fail (pre-existing SciVar bootstrap + array/table/buffer errors). 9 .clj standard library modules under src/jolt/clojure/ and src/jolt/jolt/. All tests run via `jpm test`. -§ REPL print-value uses buffer-based output: write-value/v buf appends formatted strings via buffer/push-string, then print-value creates buffer, builds string, and does a single (print (string buf)). This prevents Janet C runtime from interleaving native output between prin statements in jpm build executables. Cond catch-all must use true clause: Janet's cond treats plain expressions as tests, so (push-str buf (string v)) at end of cond would be evaluated as a test — need true before it. § Post-Phase 11: 316 passing, 1 fail (pre-existing array/table/buffer in SCI lang.cljc, deferred to Phase 15). 7,800+ lines across ~20 source/test files. 9 .clj stdlib modules (clojure/string.clj, set.clj, walk.clj, zip.clj, edn.clj, java_io.clj; jolt/interop.clj, shell.clj, http.clj). 85 CLJS-ported assertions. SCI stub file at src/jolt/clojure/sci/lang_stubs.clj provides 5 protocols + 3 deftypes. 24 test files. All builds/runs via `jpm test`. +§ +Project stats after Phase 12: 7,800+ lines across 30+ source/test files. Key sources: compiler.janet (848 lines), evaluator.janet (869 lines), core.janet (1387 lines), types.janet (441 lines), reader.janet (472 lines), phm.janet (199 lines), main.janet (125 lines), api.janet (93 lines), loader.janet (79 lines). Test suite: 316 ok, 1 fail (pre-existing deftype in lang.cljc). 17 CLJS-ported test files + 6 custom test files. 9 .clj standard library modules under src/jolt/clojure/ and src/jolt/jolt/. All builds/runs via `jpm test`. +§ +Protocol system: Type registry in context env (:type-registry) maps type-tag→proto-name→method-name→fn. Three dispatch special forms: protocol-dispatch (resolves method via registry or reified methods), register-method (stores impl in registry), make-reified (creates anonymous object with :jolt/protocol-methods). fn* forms emitted by extend-type/extend-protocol MUST be @[...] (array) for eval-list dispatch. Protocols are maps with :jolt/type :jolt/protocol and :methods map. diff --git a/.dirge/memory/PITFALLS.md b/.dirge/memory/PITFALLS.md index 5d166a1..b358708 100644 --- a/.dirge/memory/PITFALLS.md +++ b/.dirge/memory/PITFALLS.md @@ -1,5 +1,5 @@ -Clojure .clj source files loaded via eval-form cannot have docstrings. If a defn form has 5 elements (defn, name, docstring, params, body), the evaluator's defn macro handler gets 4 args instead of 3, breaking with "macro arity mismatch". All .clj files in src/jolt/clojure/ must use 4-element defn forms: (defn name [params] body). Docstrings on defn are a Clojure feature not supported by Jolt's defn macro. -§ -PHM internal key leak: Core functions iterating over PHM maps with keys/pairs get internal metadata keys (:jolt/deftype, :cnt, :buckets, :_meta). Always check for set?/phm? first and use type-aware helpers (phm-to-struct, phs-seq, phm-keys, phm-entries) before generic iteration. Hit us in core-merge, core-reduce, core-every?, core-filter which all needed set?/phm? checks added. -§ Janet `and` returns last truthy value, not boolean. `(and table? deftype)` returns the deftype string, not true. Predicate-like functions (core-map?, core-contains?, etc.) that check type tags via `(and ...)` must wrap in `(if ... true false)`. Hit us with core-map? returning the deftype string instead of boolean true for record instances. Also hit type-satisfies? which had to replace `(boolean ...)` (nonexistent) with `(if ... true false)`. +§ +Janet `and` returns last truthy value, not boolean. `(and table? deftype)` returns the deftype string, not true. Predicate-like functions (core-map?, core-contains?, etc.) that check type tags via `(and ...)` must wrap in `(if ... true false)`. Examples: core-map? returning deftype string instead of boolean true for record instances. Also applies to type-satisfies? which must use `(if (and ...) true false)` — Janet has no `boolean` function. +§ +Clojure .clj source files loaded via eval-form cannot have docstrings. If a defn form has 5 elements (defn, name, docstring, params, body), the evaluator's defn macro handler gets 4 args instead of 3, breaking with "macro arity mismatch". All .clj files in src/jolt/clojure/ must use 4-element defn forms: (defn name [params] body). Docstrings on defn are a Clojure feature not supported by Jolt's defn macro. diff --git a/.dirge/skills/.usage.json b/.dirge/skills/.usage.json index 689c338..3148d95 100644 --- a/.dirge/skills/.usage.json +++ b/.dirge/skills/.usage.json @@ -2,44 +2,22 @@ "jolt-gotchas": { "created_by": "agent", "use_count": 4, - "view_count": 240, + "view_count": 256, "patch_count": 4, "last_used_at": "2026-06-03T16:12:06.850822+00:00", - "last_viewed_at": "2026-06-03T16:51:46.822185+00:00", + "last_viewed_at": "2026-06-03T17:19:39.580796+00:00", "last_patched_at": "2026-06-03T16:12:23.546648+00:00", "created_at": "2026-06-03T03:50:41.474730+00:00", "state": "active", "pinned": false }, - "jpm-build": { - "created_by": "agent", - "use_count": 0, - "view_count": 252, - "patch_count": 0, - "last_viewed_at": "2026-06-03T16:51:46.833321+00:00", - "created_at": "2026-06-01T20:56:39.144222+00:00", - "state": "active", - "pinned": false - }, - "jolt-compiler": { - "created_by": "agent", - "use_count": 12, - "view_count": 253, - "patch_count": 10, - "last_used_at": "2026-06-03T16:35:41.304993+00:00", - "last_viewed_at": "2026-06-03T16:51:46.810154+00:00", - "last_patched_at": "2026-06-03T16:13:25.725903+00:00", - "created_at": "2026-06-02T17:54:38.690279+00:00", - "state": "active", - "pinned": false - }, "jolt-bootstrap": { "created_by": "agent", "use_count": 9, - "view_count": 261, + "view_count": 277, "patch_count": 10, "last_used_at": "2026-06-02T18:45:23.336653+00:00", - "last_viewed_at": "2026-06-03T16:51:46.804059+00:00", + "last_viewed_at": "2026-06-03T17:19:39.564734+00:00", "last_patched_at": "2026-06-02T03:44:45.935676+00:00", "created_at": "2026-06-01T21:49:51.101718+00:00", "state": "active", @@ -48,22 +26,44 @@ "jolt-persistent-structures": { "created_by": "agent", "use_count": 3, - "view_count": 240, + "view_count": 256, "patch_count": 0, "last_used_at": "2026-06-03T16:13:31.440242+00:00", - "last_viewed_at": "2026-06-03T16:51:46.828298+00:00", + "last_viewed_at": "2026-06-03T17:19:39.585778+00:00", "created_at": "2026-06-03T03:35:04.130959+00:00", "state": "active", "pinned": false }, + "jpm-build": { + "created_by": "agent", + "use_count": 0, + "view_count": 268, + "patch_count": 0, + "last_viewed_at": "2026-06-03T17:19:39.591024+00:00", + "created_at": "2026-06-01T20:56:39.144222+00:00", + "state": "active", + "pinned": false + }, + "jolt-compiler": { + "created_by": "agent", + "use_count": 12, + "view_count": 269, + "patch_count": 10, + "last_used_at": "2026-06-03T16:35:41.304993+00:00", + "last_viewed_at": "2026-06-03T17:19:39.570381+00:00", + "last_patched_at": "2026-06-03T16:13:25.725903+00:00", + "created_at": "2026-06-02T17:54:38.690279+00:00", + "state": "active", + "pinned": false + }, "jolt-dev": { "created_by": "agent", - "use_count": 39, - "view_count": 291, - "patch_count": 47, - "last_used_at": "2026-06-03T16:35:41.292589+00:00", - "last_viewed_at": "2026-06-03T16:51:46.816038+00:00", - "last_patched_at": "2026-06-03T16:36:06.347582+00:00", + "use_count": 40, + "view_count": 308, + "patch_count": 48, + "last_used_at": "2026-06-03T17:02:10.695363+00:00", + "last_viewed_at": "2026-06-03T17:19:39.575770+00:00", + "last_patched_at": "2026-06-03T17:02:24.207040+00:00", "created_at": "2026-06-01T21:26:06.614465+00:00", "state": "active", "pinned": false diff --git a/.dirge/skills/jolt-dev/SKILL.md b/.dirge/skills/jolt-dev/SKILL.md index d6b360d..b5beb00 100644 --- a/.dirge/skills/jolt-dev/SKILL.md +++ b/.dirge/skills/jolt-dev/SKILL.md @@ -36,6 +36,12 @@ janet test/phase10-test.janet # standard library printf "(range 10)\n[1 2 3]\n{:a 1}\n" | janet src/jolt/main.janet ``` +## Test File Creation — Heredoc Workaround + +The `write` tool's syntax checker rejects `.janet` files with complex string escaping (e.g., `\"` inside Janet strings). **Workaround:** Use `bash` with `cat > file << 'EOF' ... EOF` heredocs for any test file containing Clojure source strings. + +**Paren-counting boundary:** Large single-file test suites (>6 sections) often hit a mysterious paren-counting parse error ("unexpected end of source") at section boundaries, even when parens are balanced. **Workaround:** Split into multiple files (e.g., `cljs-port-1a.janet`, `cljs-port-1b.janet`). + ## Loading .clj Files `.clj` files are loaded via `eval-form` in the interpreter: diff --git a/src/jolt/evaluator.janet b/src/jolt/evaluator.janet index 9fd8f3b..a2af12e 100644 --- a/src/jolt/evaluator.janet +++ b/src/jolt/evaluator.janet @@ -836,7 +836,15 @@ (apply f args) (if (keyword? f) (get (first args) f) - (error (string "Cannot call " (type f) " as a function")))))))) + (if (and (table? f) (get f :jolt/deftype)) + (let [ifn-fn (find-protocol-method ctx (get f :jolt/deftype) "IFn" "-invoke")] + (if ifn-fn (apply ifn-fn f args) + (if (get f :jolt/protocol-methods) + (let [invoke-fn (get (f :jolt/protocol-methods) :-invoke)] + (if invoke-fn (apply invoke-fn f args) + (error (string "Cannot call " (type f) " as a function")))) + (error (string "Cannot call " (type f) " as a function"))))) + (error (string "Cannot call " (type f) " as a function"))))))))) (set eval-form (fn [ctx bindings form] (cond diff --git a/src/jolt/reader.janet b/src/jolt/reader.janet index 52178c2..3d96457 100644 --- a/src/jolt/reader.janet +++ b/src/jolt/reader.janet @@ -282,7 +282,35 @@ (defn read-anon-fn [s pos] # pos is at #, next char is ( (let [[form new-pos] (read-form s (+ pos 1))] - [(array/insert form 0 (sym "fn*")) new-pos])) + # Collect % arg references and rename them to gensyms + (var arg-map @{}) + (defn- replace-pct [f] + (cond + (and (struct? f) (= :symbol (f :jolt/type))) + (let [nm (f :name)] + (if (and (> (length nm) 0) (= "%" (string/slice nm 0 1))) + (let [existing (get arg-map nm)] + (if existing + {:jolt/type :symbol :ns nil :name existing} + (let [gen (gensym)] + (put arg-map nm (string gen)) + {:jolt/type :symbol :ns nil :name (string gen)}))) + f)) + (array? f) (array ;(map replace-pct f)) + (tuple? f) (tuple ;(map replace-pct f)) + f)) + (def replaced (replace-pct form)) + (def arg-names @[]) + # Sort %1 %2 %3 ..., then %, then %& + (def sorted-keys (sort (keys arg-map))) + (each k sorted-keys + (array/push arg-names {:jolt/type :symbol :ns nil :name (get arg-map k)})) + (def result @[(sym "fn*")]) + (if (> (length arg-names) 0) + (array/push result (tuple ;arg-names)) + (array/push result (tuple))) # no args + (array/push result replaced) + [result new-pos])) (defn read-reader-conditional [s pos] # pos is at #, next char is ? or ?@ diff --git a/test/phase13-test.janet b/test/phase13-test.janet new file mode 100644 index 0000000..bf8160a --- /dev/null +++ b/test/phase13-test.janet @@ -0,0 +1,44 @@ +(use ../src/jolt/api) +(use ../src/jolt/evaluator) +(use ../src/jolt/reader) +(defn ct-eval [ctx s] (eval-string ctx s)) + +(defn load-clj [ctx filepath] + (var s (slurp filepath)) + (while (> (length (string/trim s)) 0) + (def [form rest] (parse-next s)) + (set s rest) + (when (not (nil? form)) + (eval-form ctx @{} form)))) + +(print "=== Phase 13: Protocol Completion ===") + +(print "28: reify dispatch...") +(let [ctx (init)] + (ct-eval ctx "(defprotocol Greeter (say-hello [this]))") + (ct-eval ctx "(def r (reify Greeter (say-hello [this] \"hello reify\")))") + (assert (= "hello reify" (ct-eval ctx "(say-hello r)")) "reify dispatch")) +(print " ok") + +(print "29: #() anon-fn reader...") +(let [ctx (init)] + (assert (= 2 (ct-eval ctx "(#(inc %) 1)")) "anon fn %") + (assert (= 3 (ct-eval ctx "(#(+ %1 %2) 1 2)")) "anon fn %1 %2") + (assert (= [1 2 3] (ct-eval ctx "(map #(inc %) [0 1 2])")) "anon fn map")) +(print " ok") + +(print "30: extend-type full dispatch...") +(let [ctx (init)] + (ct-eval ctx "(defprotocol Greet (g [this]))") + (ct-eval ctx "(deftype Dog [name])") + (ct-eval ctx "(extend-type Dog Greet (g [this] (str \"woof \" (.-name this))))") + (assert (= "woof Rex" (ct-eval ctx "(g (Dog. \"Rex\"))")) "extend-type dog")) +(print " ok") + +(print "31: clojure.walk loading...") +(let [ctx (init)] + (load-clj ctx "src/jolt/clojure/walk.clj") + (assert (function? (ct-eval ctx "keywordize-keys")) "keywordize-keys is fn")) +(print " ok") + +(print "\nAll Phase 13 tests passed!")