Skip to content

mjgil-wasm/schemewasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Scheme Wasm

A Rust Scheme runtime with WebAssembly bindings, a raw Wasm ABI, and a direct-Wasm backend for the documented public subset.

Overview

Scheme Wasm is a Scheme interpreter that compiles to Wasm using wasm-bindgen. It provides:

  • Wasm Public API: A Scheme struct exposed to JavaScript for evaluating Scheme code
  • Raw Wasm ABI: Low-level exports for non-JS Wasm hosts
  • Bytecode VM: A stack-based virtual machine for executing compiled Scheme

Quick Start

Build

# Build native (tests)
cargo build

# Build Wasm packages
wasm-pack build schemewasm --target web --out-dir pkg
wasm-pack build schemewasm --target nodejs --out-dir pkg-node

Run

./run.sh

Test

cargo test

Usage (Browser)

import init, { Scheme } from './pkg/schemewasm.js';

await init();
const scheme = new Scheme();
console.log(scheme.eval('(+ 2 3)')); // "5"

Usage (Node.js)

const { Scheme } = require('./pkg-node/schemewasm.js');
const scheme = new Scheme();
console.log(scheme.eval('(+ 2 3)')); // "5"

API

Scheme.eval(source: string): string

Evaluates Scheme source code. Returns result as string, throws JsValue on error.

Scheme.eval_structured(source: string): string

Like eval but returns structured JSON with type tags.

Scheme.reset()

Resets the Scheme interpreter to initial state.

Supported Scheme Features (Implemented)

  • Numbers (f64), booleans, characters, symbols, strings
  • Pairs and lists, vectors
  • Closures (lambda) with lexical scope
  • Arithmetic: +, -, *, /, =, <, >, <=, >=
  • Special forms: if, quote, lambda, define, set!, begin
  • Local bindings via let
  • let*, letrec, cond, and, or
  • Quasiquote, unquote, unquote-splicing

Not Supported

The following R7RS features are NOT supported (see feature-registry.md for full list):

  • Continuations: call/cc, call-with-values
  • File I/O and ports: open-input-file, open-output-file, etc.
  • Dynamic wind: dynamic-wind
  • Libraries: import, library declarations
  • Delayed evaluation: delay, force
  • Threads: thread-related operations
  • Bytevectors: bytevector operations
  • Bitwise operations: logand, logor, logxor, etc.

Architecture

Source Code
    ↓
Tokenizer (reader.rs)
    ↓
Parser (reader.rs - produces Value/Sexp)
    ↓
Expander (expand.rs - produces Core)
    ↓
Analyzer (analyze.rs - produces AnalyzedExpr)
    ↓
Compiler (compiler.rs - produces BytecodeFn)
    ↓
VM (vm.rs - executes bytecode)

Documentation

  • Raw Wasm ABI - Low-level Wasm interface documentation
  • Examples - Browser and Node.js usage examples

Project Structure

schemewasm/src/
  analyze.rs      # AST analysis (AnalyzedExpr, AnalyzedKind)
  ast.rs          # Core AST types (Core, Sexp, Spanned)
  bytecode.rs     # Instruction set and BytecodeFn
  compiler.rs     # Compile AnalyzedExpr → BytecodeFn
  error.rs        # Error types
  expand.rs       # Sexp → Core expansion
  gc.rs           # Garbage collection
  heap.rs         # Heap management
  lib.rs          # Library root
  prim.rs         # Primitive procedures
  reader.rs       # Tokenizer and parser
  scheme.rs       # Wasm public API
  syntax_rules.rs # Syntax-rules macro implementation
  value.rs        # Value enum and Handle
  vm.rs           # Virtual machine

examples/
  browser.html    # Browser demo
  node-example.js  # Node.js example
  raw-wasm.c       # C host example
  raw-wasm-host.rs # Rust host example

License

BSD Zero Clause License (0BSD) - see LICENSE file

About

scheme in the browser

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages