sx: step 10 — compiler command + as converter registries

Add `_hs-command-registry` and `_hs-converter-registry` dicts plus
`hs-register-command!` / `hs-register-converter!` to
`lib/hyperscript/compiler.sx`. Inside `hs-to-sx`, before the existing
`cond` over head symbols, check both registries: an `as` form whose
type-name has a registered converter dispatches to that converter; any
list head whose name (`(str head)`) is in the command registry
dispatches to that compile-fn. On registry miss, the original ~180
hardcoded branches handle the form.

Each registered fn receives a ctx dict (built per call) exposing
`:hs-to-sx` for recursion plus the AST fields the dispatch needs
(`:ast :head` for commands; `:ast :value-ast :type-name` for
converters). Mirrors Step 9's parser feature registry shape.

Smoke tested: register custom command + converter, both dispatch;
built-in `(as x \"Int\")` still produces `(hs-coerce x \"Int\")`.

Mirror `shared/static/wasm/sx/hs-compiler.sx` copied byte-identical.
OCaml: 4545/1339, JS: 2591/2465 — both match baseline, zero regressions.

Second piece of plans/designs/hs-plugin-system.md (Step 11 next).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 00:50:53 +00:00
parent 00121e137e
commit d22361e471
2 changed files with 131 additions and 14 deletions

View File

@@ -7,6 +7,22 @@
;; (hs-to-sx (hs-compile "on click add .active to me"))
;; → (hs-on me "click" (fn (event) (dom-add-class me "active")))
;; ── Compiler plugin registries ────────────────────────────────────
;; Plugins call (hs-register-command! "head" compile-fn) and
;; (hs-register-converter! "TypeName" convert-fn) at load time. Both
;; compile-fn and convert-fn receive a ctx dict (built per call inside
;; hs-to-sx) exposing :hs-to-sx for recursion plus the AST node fields
;; the dispatch needs. Compile-fn returns an SX expression.
(begin
(define _hs-command-registry {})
(define _hs-converter-registry {})
(define
hs-register-command!
(fn (name compile-fn) (dict-set! _hs-command-registry name compile-fn)))
(define
hs-register-converter!
(fn (name convert-fn) (dict-set! _hs-converter-registry name convert-fn))))
(define
hs-to-sx
(let
@@ -952,6 +968,22 @@
(true
(let
((head (first ast)))
(let
((reg-cmd-fn (dict-get _hs-command-registry (str head)))
(reg-conv-fn
(and
(= head (quote as))
(dict-get _hs-converter-registry (nth ast 2)))))
(cond
(reg-conv-fn
(reg-conv-fn
{:hs-to-sx hs-to-sx
:ast ast
:value-ast (nth ast 1)
:type-name (nth ast 2)}))
(reg-cmd-fn
(reg-cmd-fn {:hs-to-sx hs-to-sx :ast ast :head head}))
(true
(cond
((= head (quote __bind-from-detail__))
(let
@@ -2667,7 +2699,7 @@
(quote begin)
(list (quote set!) (quote it) (quote __hs-js))
(quote __hs-js))))))
(true ast)))))))))
(true ast))))))))))))
;; ── Convenience: source → SX ─────────────────────────────────
(define