Skip to content

feat(docs): add interactive Dang WASM REPL component - #88

Merged
vito merged 2 commits into
mainfrom
dang-wasm-repl
Jun 5, 2026
Merged

vito merged 2 commits into
mainfrom
dang-wasm-repl

Conversation

@vito

@vito vito commented Jun 4, 2026 •

Copy link
Copy Markdown
Owner

What

Adds a \dang-repl Booklit component: an interactive, in-browser read-eval-print loop for Dang, built on the same tech stack as the existing \dang-playground (the cmd/dang-playground WASM module + tree-sitter highlighting) but presented as a REPL — a transcript of evaluated entries with a live input — instead of a single editor.

This addresses the docs-feedback request to "implement a Dang WASM REPL, building on the existing Dang playground… another type of component, but [on] the same tech stack: user-editable, treesitter-highlighted, but as a REPL."

How it works

  • An actual long-running session. The WASM module gains two exports, dangReplEval(sessionID, source) and dangReplReset(sessionID). Each session keeps a scope pair (type scope + value scope) alive inside the module and mutates it in place on every entry, so each input is type-checked and evaluated incrementally against the accumulated state — nothing is re-parsed or re-run. This mirrors the native CLI REPL (cmd/dang/repl_tuist.go), which reuses one scope pair for the whole session rather than replaying history. A fresh Fresher is allocated per call (its type-variable counter is monotonic), but that's independent of the session state.
  • Per-component isolation. Sessions are keyed by a handle the frontend assigns to each REPL on the page, so multiple REPLs never share state. The module creates a session lazily on first use, and dangReplReset drops it (the next eval starts from fresh standard-library scopes). The browser REPL is core-language only (no GraphQL imports), so unlike dangEval it never touches the network and stays synchronous.
  • Tradeoff. Because state is mutated in place, an entry that fails partway (a type error, or a runtime error after some forms have already bound) can leave partial state behind — exactly as the native CLI REPL behaves. The Reset button clears it.
  • Shared internals. The REPL's parse/infer/eval path lives in evalForms, distinct from the playground's GitHub-aware evalSource. The frontend lives in the existing playground.js, reusing the asset resolver, lazy WASM loader, and tree-sitter highlighter; loadDang() now resolves to all three exports.
  • REPL input. Bare Enter evaluates when bracket nesting looks balanced (so multi-line type { … } entries keep accepting newlines until closed), Shift+Enter forces a newline, and Ctrl/Cmd+Enter or the Run button always evaluate. The dang> prompt and => result mirror the CLI REPL documented in Getting Started.
  • Styling. Token colors are de-scoped from .dang-playground-highlight to plain .tok-* so the playground and REPL share one palette.

Demo

Getting Started gains a small REPL seeded with let greeting = "world", with a note that the browser REPL runs the core language only (no GraphQL imports).

No JS → it degrades to a plain code block, same as the playground. The built dang.wasm is produced by docs/build.sh (gitignored, built in CI), so no binary churn here.

Verification

GOOS=js GOARCH=wasm build + go vet pass. A native harness exercising the real dang API against one persistent scope pair confirmed the session semantics: binding carry-over, string interpolation against prior bindings, type declarations persisting and constructing across entries (e.g. type Point { … } then Point(x: 3).x), print output capture, and a type error leaving the session intact (a later entry still resolves earlier bindings).

🤖 Generated with Claude Code

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 4, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 6a6bd82
Status: ✅  Deploy successful!
Preview URL: https://aa1314a9.dang-3kk.pages.dev
Branch Preview URL: https://dang-wasm-repl.dang-3kk.pages.dev

View logs

@vito
vito force-pushed the dang-wasm-repl branch from eab6b64 to 832a33b Compare June 4, 2026 21:53
vito added 2 commits June 4, 2026 21:54
Add a \dang-repl Booklit component: an in-browser read-eval-print loop
built on the same stack as \dang-playground (the cmd/dang-playground WASM
module plus tree-sitter highlighting), but presented as a transcript of
evaluated entries rather than a single editor.

State accumulates across entries within a long-running session, like the
CLI REPL. The WASM gains dangReplEval(sessionID, source) and
dangReplReset(sessionID) exports: each session keeps a scope pair alive in
the module and mutates it in place, so every entry is type-checked and
evaluated incrementally against accumulated state — nothing is re-parsed or
re-run. Sessions are keyed per REPL component so multiple REPLs on a page
stay isolated. This mirrors the native CLI REPL (cmd/dang/repl_tuist.go);
the browser REPL is core-language only (no GraphQL imports), so it stays
synchronous. The tradeoff of in-place state is that an entry which fails
partway can leave partial state behind, which dangReplReset clears.

The frontend reuses the playground's asset loader, lazy WASM loader, and
tree-sitter highlighter. Bare Enter evaluates when bracket nesting looks
balanced, Shift+Enter forces a newline, and Ctrl/Cmd+Enter or the Run
button always evaluate. The prompt and "=>" result mirror the CLI REPL.

Token colors are de-scoped from .dang-playground-highlight to plain .tok-*
so both widgets share one palette. Getting Started gains a small REPL demo
seeded with a binding the reader can reference on the next line.

Signed-off-by: Alex Suraci <[email protected]>
The base `pre` rule forced `padding:1rem 1.25rem !important`, which beats
inline and per-component styles that aren't themselves marked important.
That silently clobbered the REPL's code/input rows (which want padding:0,
so the transcript aligns under the `dang>` prompt) and the install shell
block (which sets a tighter `.5rem .9rem` inline). It was added in the
initial scaffold with no rationale, and nothing emits inline `pre` padding
that needs overriding — chroma blocks only set color/background inline — so
the !important guards nothing. Dropping it lets the more specific
`.dang-repl-*` rules and the shell block's inline padding apply as intended;
chroma code blocks still pick up the base padding normally.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the dang-wasm-repl branch from 832a33b to 6a6bd82 Compare June 4, 2026 21:56
@vito
vito merged commit 0a6d0fa into main Jun 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant