Skip to content

PollRobots/scheme

Repository files navigation

scheme.wasm logo

Node.js CI CodeQL FOSSA Status

GitHub stars

scheme.wasm

An R7RS Scheme implemented in WebAssembly

A partial implementation of r7rs scheme, written entirely in WebAssembly using the WebAssembly Text format. The only external imports are for IO (read, write, and readFile), unicode (I have an import that reads information about 256 code-point blocks to enable case operations etc.), and process control (exit).

You can try it out at pollrobots.com/scheme/

How Complete Is It?

The aim is to write a spec complete version of r7rs, although I may skip some of the optional features.

What is done so far

  • Numerics
    • Integers (arbitrary precision)
    • Real numbers (double precision)
    • Rationals
    • Complex Numbers
  • Booleans
  • Strings
  • Characters
  • Pairs and Lists
  • Vectors
  • Bytevectors
  • Values
  • Records
  • Tail call optimization — internally eval uses a continuation passing style, so TCO comes for free.
  • call/cc and exceptions
  • Macros
    • define-syntax, syntax-rules, syntax-error
    • Hygienic over let, let*, letrec, letrec*, and lambda
    • let-syntax, letrec-syntax
  • Modules
  • Ports
  • dynamic-wind
  • Everything else

Credits

Where practical everything has been implemented from scratch, but there are places where it either wasn't practical, or where I tried and failed to implement them myself, so credit is due to:

  • xxHash: It's probably overkill, but the hashing algorithm used for hashtables, which are in turn used for environments and interning symbols, is xxHash translated from the C++ implementation at github.com/Cyan4973/xxHash
  • string->real: Strings are converted to real numbers using Algorithm M from "How to Read Floating Point Numbers Accurately", William D Clinger 1990. Which is conveniently expressed in scheme in the original paper
  • real->string: Real numbers are converted to strings using Grisu 2 by Florian Loitsch. This was translated from C++ found at github.com/romange/Grisu

Additionally inspiration came from a couple of places