lumbda/wasm/app/demos/mandelbrot.lsp
russell@unturf.com 346b873247
wasm: three-tier Lumbda to WebAssembly + browser playground
Adds a parallel build of all three Lumbda implementations to WASM, a
single-page playground at www/playground/, and a verified test suite.

Tiers
  - Python: Pyodide (CPython-in-WASM) hosting lumbda.py
  - C:      Emscripten build of c/ (tree-walker + bytecode VM; jit.c
            stubbed, gc.c uses its existing no-Boehm fallback)
  - Asm:    hand-written asm/lumbda.wat — parallel impl to asm/lumbda.s.
            Reader, eval (lambda/define/if/cond/let/and/or/quote/set!),
            recursion across mutated top-level env, bump allocator with
            memory.grow, 24 primitives. ~1200 lines of raw WAT.

SPA (wasm/app/, deployed to www/playground/)
  - CodeMirror 6 editor (Scheme highlighting) on left, output on right
  - Radios: 4 demos (Mandelbrot, Fib+Ack, Sieve, self-interp meta-eval)
            x 4 tiers (Python | C | Asm | All three)
  - All-three mode renders the three tier outputs side by side with
    per-tier elapsed timing

Tests (38 verified assertions)
  - 20 unit (Node): per-tier module loads, eval smoke
  - 8 integration (Node): each demo on c+asm WASM byte-matches the
                          canonical native Python run
  - 10 functional (Playwright headless Chromium): page mounts, every
                          demo runs on every tier, all-three renders

Makefile
  - Root targets: wasm-build, wasm-test, wasm-test-fn, wasm-serve,
                  wasm-deploy, wasm-clean
  - wasm/Makefile orchestrates the three tier builds; deploy copies
    dist/ into www/playground/

Asm tier notes
  - WAT linear symbol intern + linear env lookup is MOAD-0001 at scale;
    documented in the asm/lumbda.wat header and in the SPA footer. The
    demos hit ~30 globals so the linear walks are cheap enough.
  - Bump allocator never frees (matches asm/lumbda.s heap discipline);
    memory.grow expands by 1 MB chunks. Browser tab tears down at unload.

Toolchain (developer prerequisites)
  - Emscripten 6.0.0 via emsdk at ~/git/emsdk
  - wabt 1.0.36 at ~/git/wabt
  - Playwright for functional tests (symlinked from ~/git/agnt)
2026-06-14 11:40:34 -04:00

46 lines
1.2 KiB
Text

; Mandelbrot — fixed-point ASCII render.
; Runs identically on Python, C, and asm WASM tiers.
; The asm tier has no float support so we scale all coords by 1024.
(define SCALE 1024)
(define SCALE4 4096) ; 4 * SCALE (escape threshold |z|^2)
(define WIDTH 32)
(define HEIGHT 12)
(define MAXITER 12)
(define (escape-count cx cy)
(define (loop zr zi n)
(let ((zr2 (/ (* zr zr) SCALE))
(zi2 (/ (* zi zi) SCALE)))
(cond ((>= n MAXITER) MAXITER)
((> (+ zr2 zi2) SCALE4) n)
(else
(loop (+ (- zr2 zi2) cx)
(+ (/ (* 2 (/ (* zr zi) SCALE)) 1) cy)
(+ n 1))))))
(loop 0 0 0))
(define (shade n)
(cond ((>= n MAXITER) (display "#"))
((> n 12) (display "@"))
((> n 7) (display "*"))
((> n 4) (display "+"))
((> n 2) (display "."))
(else (display " "))))
(define (row py)
(define cy (- (/ (* py 2048) HEIGHT) 1024))
(define (col px)
(if (< px WIDTH)
(begin
(shade (escape-count (- (/ (* px 3072) WIDTH) 2048) cy))
(col (+ px 1)))
(newline)))
(col 0))
(define (render py)
(if (< py HEIGHT)
(begin (row py) (render (+ py 1)))
(print "done")))
(render 0)