Step 5 piece 6: migrate 23 .sx files to define-library/import

Wraps all core .sx files in R7RS define-library with explicit export
lists, plus (import ...) at end for backward-compatible global re-export.

Libraries registered:
  (sx bytecode)      — 83 opcode constants
  (sx render)        — 15 tag registries + render helpers
  (sx signals)       — 23 reactive signal primitives
  (sx r7rs)          — 21 R7RS aliases
  (sx compiler)      — 42 compiler functions
  (sx vm)            — 32 VM functions
  (sx freeze)        — 9 freeze/thaw functions
  (sx content)       — 6 content store functions
  (sx callcc)        — 1 call/cc wrapper
  (sx highlight)     — 13 syntax highlighting functions
  (sx stdlib)        — 47 stdlib functions
  (sx swap)          — 13 swap algebra functions
  (sx render-trace)  — 8 render trace functions
  (sx harness)       — 21 test harness functions
  (sx canonical)     — 12 canonical serialization functions
  (web adapter-html) — 13 HTML renderer functions
  (web adapter-sx)   — 13 SX wire format functions
  (web engine)       — 33 hypermedia engine functions
  (web request-handler) — 4 request handling functions
  (web page-helpers) — 12 page helper functions
  (web router)       — 36 routing functions
  (web deps)         — 19 dependency analysis functions
  (web orchestration) — 59 page orchestration functions

Key changes:
- define-library now inherits parent env (env-extend env instead of
  env-extend make-env) so library bodies can access platform primitives
- sx_server.ml: added resolve_library_path + load_library_file for
  import resolution (maps library specs to file paths)
- cek_run_with_io: handles "import" locally instead of sending to
  Python bridge

2608/2608 tests passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-03 21:48:54 +00:00
parent 397d0f39c0
commit 2d7dd7d582
26 changed files with 858 additions and 14 deletions

View File

@@ -19,6 +19,94 @@
;; --------------------------------------------------------------------------
;; Stack / Constants
(define-library (sx bytecode)
(export
OP_CONST
OP_NIL
OP_TRUE
OP_FALSE
OP_POP
OP_DUP
OP_LOCAL_GET
OP_LOCAL_SET
OP_UPVALUE_GET
OP_UPVALUE_SET
OP_GLOBAL_GET
OP_GLOBAL_SET
OP_JUMP
OP_JUMP_IF_FALSE
OP_JUMP_IF_TRUE
OP_CALL
OP_TAIL_CALL
OP_RETURN
OP_CLOSURE
OP_CALL_PRIM
OP_APPLY
OP_LIST
OP_DICT
OP_APPEND_BANG
OP_ITER_INIT
OP_ITER_NEXT
OP_MAP_OPEN
OP_MAP_APPEND
OP_MAP_CLOSE
OP_FILTER_TEST
OP_HO_MAP
OP_HO_FILTER
OP_HO_REDUCE
OP_HO_FOR_EACH
OP_HO_SOME
OP_HO_EVERY
OP_SCOPE_PUSH
OP_SCOPE_POP
OP_PROVIDE_PUSH
OP_PROVIDE_POP
OP_CONTEXT
OP_EMIT
OP_EMITTED
OP_RESET
OP_SHIFT
OP_DEFINE
OP_DEFCOMP
OP_DEFISLAND
OP_DEFMACRO
OP_EXPAND_MACRO
OP_STR_CONCAT
OP_STR_JOIN
OP_SERIALIZE
OP_ADD
OP_SUB
OP_MUL
OP_DIV
OP_EQ
OP_LT
OP_GT
OP_NOT
OP_LEN
OP_FIRST
OP_REST
OP_NTH
OP_CONS
OP_NEG
OP_INC
OP_DEC
OP_ASER_TAG
OP_ASER_FRAG
BYTECODE_MAGIC
BYTECODE_VERSION
CONST_NUMBER
CONST_STRING
CONST_BOOL
CONST_NIL
CONST_SYMBOL
CONST_KEYWORD
CONST_LIST
CONST_DICT
CONST_CODE
opcode-name)
(begin
(define OP_CONST 1) ;; u16 pool_idx — push constant
(define OP_NIL 2) ;; push nil
(define OP_TRUE 3) ;; push true
@@ -161,3 +249,9 @@
(= op 50) "RETURN" (= op 52) "CALL_PRIM"
(= op 128) "DEFINE" (= op 144) "STR_CONCAT"
:else (str "OP_" op))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx bytecode))

View File

@@ -77,6 +77,12 @@
;; 2. call/cc — call with current continuation
;; --------------------------------------------------------------------------
(define-library (sx callcc)
(export
sf-callcc)
(begin
(define sf-callcc
(fn (args env)
;; Single argument: a function to call with the current continuation.
@@ -243,3 +249,9 @@
;; dispatch in eval-list (same path as lambda calls).
;;
;; --------------------------------------------------------------------------
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx callcc))

View File

@@ -14,6 +14,53 @@
;; --------------------------------------------------------------------------
;; Constant pool builder
;; --------------------------------------------------------------------------
(define-library (sx compiler)
(export
make-pool
pool-add
make-scope
scope-define-local
scope-resolve
make-emitter
emit-byte
emit-u16
emit-i16
emit-op
emit-const
current-offset
patch-i16
compile-expr
compile-symbol
compile-dict
compile-list
compile-if
compile-when
compile-and
compile-or
compile-begin
compile-let
compile-letrec
compile-lambda
compile-define
compile-set
compile-quote
compile-cond
compile-case
compile-case-clauses
compile-match
compile-thread
compile-thread-step
compile-defcomp
compile-defmacro
compile-quasiquote
compile-qq-expr
compile-qq-list
compile-call
compile
compile-module)
(begin
(define make-pool (fn () {:entries (if (primitive? "mutable-list") (mutable-list) (list)) :index {:_count 0}}))
(define
@@ -871,3 +918,9 @@
(compile-expr em (last exprs) scope false)
(emit-op em 50)
{:constants (get (get em "pool") "entries") :bytecode (get em "bytecode")})))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx compiler))

View File

@@ -11,6 +11,17 @@
;; localStorage or IPFS by providing their own store backend.
;; ==========================================================================
(define-library (sx content)
(export
content-store
content-hash
content-put
content-get
freeze-to-cid
thaw-from-cid)
(begin
(define content-store (dict))
(define content-hash :effects []
@@ -46,3 +57,9 @@
(when sx-text
(thaw-from-sx sx-text)
true))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx content))

View File

@@ -20,6 +20,20 @@
;; ==========================================================================
;; Registry of freeze scopes: name → list of {name signal} entries
(define-library (sx freeze)
(export
freeze-registry
freeze-signal
freeze-scope
cek-freeze-scope
cek-freeze-all
cek-thaw-scope
cek-thaw-all
freeze-to-sx
thaw-from-sx)
(begin
(define freeze-registry (dict))
;; Register a signal in the current freeze scope
@@ -92,3 +106,9 @@
(when (not (empty? parsed))
(let ((frozen (first parsed)))
(cek-thaw-scope (get frozen "name") frozen))))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx freeze))

View File

@@ -1,3 +1,22 @@
(define-library (sx highlight)
(export
sx-specials
sx-special?
hl-digit?
hl-alpha?
hl-sym-char?
hl-ws?
hl-escape
hl-span
tokenize-sx
sx-token-classes
render-sx-tokens
highlight-sx
highlight)
(begin
(define
sx-specials
(list
@@ -298,3 +317,9 @@
(or (= lang "lisp") (= lang "sx") (= lang "sexp") (= lang "scheme"))
(highlight-sx code)
(list (quote code) code))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx highlight))

View File

@@ -1,3 +1,30 @@
(define-library (sx r7rs)
(export
make-error-object
error-object?
error-message
error-object-irritants
with-exception-handler
car
cdr
cadr
cddr
caar
cdar
caddr
cadddr
null?
pair?
procedure?
boolean=?
symbol->string
string->symbol
number->string
string->number)
(begin
(define make-error-object (fn (message irritants) {:irritants irritants :type "error-object" :message message}))
(define
@@ -51,3 +78,9 @@
(define
string->number
(fn (s) (if (string-contains? s ".") (parse-float s) (parse-int s))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx r7rs))

View File

@@ -1,3 +1,17 @@
(define-library (sx render-trace)
(export
*render-trace*
*render-trace-log*
*render-trace-depth*
render-trace-reset!
render-trace-push!
render-trace-enter!
render-trace-exit!
format-render-trace)
(begin
(define *render-trace* false)
(define *render-trace-log* (list))
@@ -49,3 +63,9 @@
(result (get entry :result)))
(str indent kind " " detail " → " result)))
*render-trace-log*))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx render-trace))

View File

@@ -13,6 +13,58 @@
;; Replacing them with SX lambdas changes behavior inside shift/reset
;; because the transpiled evaluator code uses them directly.
(define-library (sx stdlib)
(export
eq?
eqv?
equal?
boolean?
number?
string?
list?
dict?
continuation?
zero?
odd?
even?
empty?
abs
ceil
round
min
max
clamp
first
last
rest
nth
cons
append
reverse
flatten
range
chunk-every
zip-pairs
vals
has-key?
assoc
dissoc
into
upcase
downcase
string-length
substring
string-contains?
starts-with?
ends-with?
contains?
pluralize
escape
parse-datetime
assert)
(begin
(define eq? (fn (a b) (= a b)))
(define eqv? (fn (a b) (= a b)))
(define equal? (fn (a b) (= a b)))
@@ -273,3 +325,9 @@
(when (not condition)
(error (or message "Assertion failed")))
true))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx stdlib))

View File

@@ -1,3 +1,22 @@
(define-library (sx swap)
(export
_skip-string
_find-close
_skip-ws
_skip-token
_skip-value
_find-children-start
_scan-back
find-element-by-id
sx-swap
_extract-attr-value
find-oob-elements
strip-oob
apply-response)
(begin
(define
_skip-string
(fn
@@ -298,3 +317,9 @@
(get oob "content"))
(rest items))))))
(_apply-oobs result oobs)))))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx swap))

View File

@@ -1,3 +1,41 @@
(define-library (sx vm)
(export
make-upvalue-cell
uv-get
uv-set!
make-vm-code
make-vm-closure
make-vm-frame
make-vm
vm-push
vm-pop
vm-peek
frame-read-u8
frame-read-u16
frame-read-i16
vm-push-frame
code-from-value
vm-closure?
vm-call
frame-local-get
frame-local-set
frame-upvalue-get
frame-upvalue-set
vm-global-get
vm-resolve-ho-form
vm-call-external
vm-global-set
env-walk
env-walk-set!
vm-create-closure
vm-run
vm-step
vm-call-closure
vm-execute-module)
(begin
(define make-upvalue-cell (fn (value) {:uv-value value}))
(define uv-get (fn (cell) (get cell "uv-value")))
@@ -556,3 +594,9 @@
(dict-set! vm "frames" (list frame))
(vm-run vm)
(vm-pop vm)))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (sx vm))