Unify CEK callable dispatch, add named-let transpiler, full stdlib

Three changes that together enable the full 46-function stdlib migration:

1. CEK callable unification (spec/evaluator.sx):
   cek-call now routes both native callables and SX lambdas through
   continue-with-call, so replacing a native function with an SX lambda
   doesn't change shift/reset behavior.

2. Named-let transpiler support (hosts/javascript/transpiler.sx):
   (let loop ((i 0)) body...) now transpiles to a named IIFE:
   (function loop(i) { body })(0)
   This was the cause of the 3 test regressions (produced [object Object]).

3. Full stdlib via runtime eval (hosts/javascript/bootstrap.py):
   stdlib.sx is eval'd at runtime (not transpiled) so its defines go
   into PRIMITIVES without shadowing module-scope variables that the
   transpiled evaluator uses directly.

stdlib.sx now contains all 46 library functions:
  Logic: not
  Comparison: != <= >= eq? eqv? equal?
  Predicates: boolean? number? string? list? dict? continuation?
    zero? odd? even? empty?
  Arithmetic: inc dec abs ceil round min max clamp
  Collections: first last rest nth cons append reverse flatten
    range chunk-every zip-pairs
  Dict: vals has-key? assoc dissoc into
  Strings: upcase downcase string-length substring string-contains?
    starts-with? ends-with? split join replace contains?
  Text: pluralize escape parse-datetime assert

All hosts: JS 957+1080, Python 744, OCaml 952 — zero regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-17 17:11:09 +00:00
parent 4c54843542
commit f9e65e1d17
6 changed files with 324 additions and 92 deletions

View File

@@ -109,6 +109,9 @@
"dom-document" "domDocument"
"dom-window" "domWindow"
"dom-head" "domHead"
"!=" "notEqual_"
"<=" "lte_"
">=" "gte_"
"*batch-depth*" "_batchDepth"
"*batch-queue*" "_batchQueue"
"*store-registry*" "_storeRegistry"
@@ -1112,19 +1115,50 @@
(define js-emit-let
(fn (expr)
(let ((bindings (nth expr 1))
(body (rest (rest expr))))
(let ((binding-lines (js-parse-let-bindings bindings))
(body-strs (list)))
(begin
(for-each (fn (b) (append! body-strs (str " " (js-statement b))))
(slice body 0 (- (len body) 1)))
(append! body-strs (str " return " (js-expr (last body)) ";"))
(str "(function() {\n"
(join "\n" binding-lines)
(if (empty? binding-lines) "" "\n")
(join "\n" body-strs)
"\n})()"))))))
;; Detect named let: (let name ((x init) ...) body...)
(if (= (type-of (nth expr 1)) "symbol")
(js-emit-named-let expr)
(let ((bindings (nth expr 1))
(body (rest (rest expr))))
(let ((binding-lines (js-parse-let-bindings bindings))
(body-strs (list)))
(begin
(for-each (fn (b) (append! body-strs (str " " (js-statement b))))
(slice body 0 (- (len body) 1)))
(append! body-strs (str " return " (js-expr (last body)) ";"))
(str "(function() {\n"
(join "\n" binding-lines)
(if (empty? binding-lines) "" "\n")
(join "\n" body-strs)
"\n})()")))))))
;; Named let: (let loop-name ((param init) ...) body...)
;; Emits a named IIFE: (function loop(p1, p2) { body })(init1, init2)
(define js-emit-named-let
(fn (expr)
(let ((loop-name (symbol-name (nth expr 1)))
(bindings (nth expr 2))
(body (slice expr 3))
(params (list))
(inits (list)))
;; Parse bindings — Scheme-style ((name val) ...)
(for-each
(fn (b)
(let ((pname (if (= (type-of (first b)) "symbol")
(symbol-name (first b))
(str (first b)))))
(append! params (js-mangle pname))
(append! inits (js-expr (nth b 1)))))
bindings)
;; Emit body statements + return last
(let ((body-strs (list))
(mangled-name (js-mangle loop-name)))
(for-each (fn (b) (append! body-strs (str " " (js-statement b))))
(slice body 0 (- (len body) 1)))
(append! body-strs (str " return " (js-expr (last body)) ";"))
(str "(function " mangled-name "(" (join ", " params) ") {\n"
(join "\n" body-strs)
"\n})(" (join ", " inits) ")")))))
(define js-parse-let-bindings
(fn (bindings)