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

@@ -125,4 +125,22 @@
((sx (hs-to-sx-from-source "def add(a, b) return a end")))
(assert= (quote define) (first sx))
(assert= (quote add) (nth sx 1))
(assert= (quote fn) (first (nth sx 2))))))
(assert= (quote fn) (first (nth sx 2))))))
(defsuite
"hs-handler-extensions"
(deftest
"render compiles to render-to-html"
(let
((sx (hs-to-sx-from-source "render ~badge :label 'New'")))
(assert= (quote render-to-html) (first sx))
(assert= (quote ~badge) (nth sx 1))))
(deftest
"eval inlines SX with variable access"
(let
((h (hs-handler "set x to 10 then set y to eval (+ x 5)")))
(h "el")
(assert= 15 y)))
(deftest
"eval as command in handler"
(let ((h (hs-handler "eval (set! z 99)"))) (h "el") (assert= 99 z))))