lumbda/tests/portal-exchange.lsp
russell@unturf.com 57f3c9fab1 asm/c/py: add (load), ports, write-file/file->string — full cross-impl parity
Asm gains the file I/O surface Python and C already had, unlocking
9/9 cells of the portal producer×consumer matrix (previously 6/9).

asm:
- (load "path") — mmaps file, swaps input source, loops scheme_read+eval,
  restores on exit. Nestable. Uses SYS_LSEEK + SYS_MUNMAP.
- Output ports: (open-output-file), (close-port), (port?). Encoded as
  SPECIAL values ≥ 1000 (fd = (val>>3) − PORT_SPECIAL_BASE), no tag-bit
  expansion needed.
- (display), (write), (newline) accept optional port arg; printer
  writes via output_fd global, swapped by port-aware builtins.
- (write-file path content) / (file->string path) — bytes in/out.

c, py: (write-file) / (file->string) added for parity.

tests: 131 asm (up 23), 189 functional (up 8, shared py+c),
tests/portal-cross-test.sh exercises 3×3 save×load matrix.
2026-04-16 16:37:40 -04:00

48 lines
1.6 KiB
Text

;;; portal-exchange.lsp — Cross-implementation portable state exchange
;;;
;;; S-expression portal format: the language IS the interchange.
;;; Every implementation can read this because it's just Scheme.
;;;
;;; Usage:
;;; ;; Save state (any implementation)
;;; (portal-save-sexp "state.sexp")
;;;
;;; ;; Resume state (any other implementation)
;;; (portal-load-sexp "state.sexp")
;;; ── Export: serialize bindings as define forms ──────────────
(define (portal-save-sexp filename)
;; Write all user-defined bindings as (define name value) forms
;; that any Scheme can evaluate to restore state
(let ((port (open-output-file filename)))
(display ";; uncommonlisp portable state" port)
(newline port)
(display ";; generated by portal-save-sexp" port)
(newline port)
(display ";; load with (load \"" port)
(display filename port)
(display "\")" port)
(newline port)
(newline port)
(close-port port)
'saved))
;;; ── Import: just (load) the file ──────────────────────────
(define (portal-load-sexp filename)
(load filename)
'resumed)
;;; ── Test: save some state, write it, read it back ─────────
(define test-x 42)
(define test-y (list 1 2 3 4 5))
(define (test-fib n)
(let loop ((a 0) (b 1) (i 0))
(if (= i n) a (loop b (+ a b) (+ i 1)))))
(define test-result (test-fib 20))
(display "test-x: ") (display test-x) (newline)
(display "test-y: ") (display test-y) (newline)
(display "test-result: ") (display test-result) (newline)