Step 18 (part 7): Extensions — render components + SX escape

Two hyperscript extensions beyond stock:

render ~component :key val [into|before|after target]
  Tokenizer: ~ + ident → component token type
  Parser: render command with kwargs and optional position
  Compiler: emits (render-to-html ~comp :key val) or
            (hs-put! (render-to-html ...) pos target)
  Bridges hyperscript flow to SX component rendering

eval (sx-expression) — SX escape hatch
  Inside eval (...), content is SX syntax (not hyperscript)
  Parser: collect-sx-source extracts balanced parens from raw source
  Compiler: sx-parse at compile time, inlines AST directly
  Result: SX runs in handler scope — hyperscript variables visible!
  Also supports string form: eval '(+ 1 2)' for backward compat

  set name to "Giles"
  set greeting to eval (str "Hello " name)  -- name is visible!

16 new tests (parser + compiler + integration).
3127/3127 full build, zero regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-06 09:10:28 +00:00
parent f5da2bcfd5
commit 770c7fd821
6 changed files with 251 additions and 6 deletions

View File

@@ -649,6 +649,64 @@
(assert= "reset" (nth ast 1))
(assert= 0 (len (nth ast 2))))))
(defsuite
"hs-parse-render"
(deftest
"render component"
(let
((ast (hs-compile "render ~card")))
(assert= (quote render) (first ast))
(assert= "~card" (nth ast 1))
(assert= 0 (len (nth ast 2)))))
(deftest
"render with kwargs"
(let
((ast (hs-compile "render ~card :title 'Hello'")))
(assert= "~card" (nth ast 1))
(let
((kw (nth ast 2)))
(assert= "title" (first kw))
(assert= "Hello" (nth kw 1)))))
(deftest
"render into target"
(let
((ast (hs-compile "render ~card :title 'Hi' into #box")))
(assert= "into" (nth ast 3))
(assert= (quote query) (first (nth ast 4)))))
(deftest
"component token in expression"
(let
((ast (hs-compile "set x to ~myComp")))
(let
((val (nth ast 2)))
(assert= (quote component) (first val))
(assert= "~myComp" (nth val 1))))))
(defsuite
"hs-parse-sx-eval"
(deftest
"eval with parens extracts raw SX"
(let
((ast (hs-compile "set x to eval (+ 1 2)")))
(let
((val (nth ast 2)))
(assert= (quote sx-eval) (first val))
(assert= "(+ 1 2)" (nth val 1)))))
(deftest
"eval with string fallback"
(let
((ast (hs-compile "eval '(log 42)'")))
(assert= (quote sx-eval) (first ast))
(assert= "(log 42)" (nth ast 1))))
(deftest
"eval nested parens"
(let
((ast (hs-compile "set x to eval (map (fn (x) (+ x 1)) items)")))
(let
((val (nth ast 2)))
(assert= (quote sx-eval) (first val))
(assert= "(map (fn (x) (+ x 1)) items)" (nth val 1))))))
(defsuite
"hs-parse-every-modifier"
(deftest