A simple compile-to-WebAssembly language rewritten in Rust

Related tags

WebAssembly chasm-rs
Overview

chasm

A very simple compile-to-WebAssembly language

You can play with chasm online.

This is a rewrite in Rust of the compiler for the language chasm. chasm is a very simple programming language with a two-fold purpose:

  1. Introduce the basic building blocks of compilers - and show that they aren't that scary or difficult!
  2. Reveal some of the inner workings of WebAssembly.

chasm was first created by Colin Eberhardt to accompany a talk at FullStack Conference NYC. You can see the original implementation of the compiler here. My implementation loosely follow his blog post. His implementation uses an AST, but mine is a single-pass compiler.

Example

Below is an example of a Mandelbrot fractal renderer. Other examples can be found in the ./examples folder. You can also test these examples in the online demo.

var y = 0
while (y < 100)
  var x  = 0
  while (x < 100)
    var cr = ((y / 50) - 1.5)
    var ci = ((x / 50) - 1)

    var i = 0
    var j = 0
    var iter = 0

    while ((((i * i) + (j * j)) < 4) && (iter < 255))
      var ni = (((i * i) - (j * j)) + cr)
      j = (((2 * i) * j) + ci)
      i = ni
      iter = (iter + 1)
    endwhile
    setpixel (x, y, iter)
    x = (x + 1)
  endwhile
  y = (y + 1)
endwhile

Build and Run

CLI

You can pass a file as an argument to the CLI to run and render chasm code in the file. For example, to render the Julia set in ./examples/julia.chasm, you can run the command (assuming you are in the repo root):

cargo run -- examples/julia.chasm

If you pass any second argument it will render in the terminal as ASCII, and if you pass no argument it will run a bad REPL.

WebAssembly

To build the compiler for WebAssembly, you need to use wasm-pack:

cd chasm-wasm
wasm-pack build --release --target web

This will output a folder ./chasm-wasm/pkg with the files chasm_wasm.js and chasm_wasm_bg.wasm (and some others). You can then move these two files to ./docs to test them on the web page ./docs/index.html.

You might also like...
bn.js bindings for Rust & WebAssembly with primitive-types support

bn.rs bn.js bindings for Rust & WebAssembly with primitive-types support Write Rust code that uses BN use std::str::FromStr; use primitive_types::{H1

A handy calculator, based on Rust and WebAssembly.
A handy calculator, based on Rust and WebAssembly.

qubit 💥 Visit Website To Use Calculator Example 💥 Visit Website To Use Calculator 2 + 2

Stylist is a CSS-in-Rust styling solution for WebAssembly Applications.

Stylist Stylist is a CSS-in-Rust styling solution for WebAssembly Applications. This is a fork of css-in-rust. Install Add the following to your Cargo

Rust WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain
Rust WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain

Limelight Limelight is a WebGL2 wrapper with a focus on making high-performance WebAssembly graphics code easier to write and maintain. demo.mov live

WebAssembly serialization/deserialization in rust

parity-wasm Low-level WebAssembly format library. Documentation Rust WebAssembly format serializing/deserializing Add to Cargo.toml [dependencies] par

This is a webpack loader that loads Rust code as a WebAssembly module

rust-native-wasm-loader This is a webpack loader that loads Rust code as a WebAssembly module. It uses the native Rust support for compiling to wasm32

Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly.

⚡ Zaplib Zaplib is an open-source library for speeding up web applications using Rust and WebAssembly. It lets you write high-performance code in Rust

A template for kick starting a Rust and WebAssembly project using wasm-pack.

A template for kick starting a Rust and WebAssembly project using wasm-pack.

Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly

TelemetryDeck Client Client for integrating private analytics in fast and reliable libraries and apps using Rust and WebAssembly The library provides

A curly-braces infix language that compiles to WebAssembly

CurlyWas CurlyWas is a (still WIP) curly-braces, infix synatx for WebAssembly. The goal is to have as to a 1:1 mapping to the resulting wasm instructi

null 22 Dec 24, 2022
Simple file sharing with client-side encryption, powered by Rust and WebAssembly

Hako Simple file sharing with client-side encryption, powered by Rust and WebAssembly Not feature-packed, but basic functionalities are just working.

Jaehyeon Park 30 Nov 25, 2022
A simple Rust and WebAssembly real-time implementation of the Vigénere Cipher utilizing the Sycamore reactive library.

WebAssembly Vigenère Cipher A simple Rust and WebAssembly real-time implementation of the Vigenère Cipher utilizing the Sycamore reactive library, Tru

Rodrigo Santiago 6 Oct 11, 2022
A simple event-driven library for parsing WebAssembly binary files

The WebAssembly binary file decoder in Rust A Bytecode Alliance project The decoder library provides lightweight and fast decoding/parsing of WebAssem

Yury Delendik 8 Jul 27, 2022
A simple tool to convert a WebAssembly module to a WASI component (component model, WASI-preview2).

A simple tool to convert a WebAssembly module (wasm32-wasi, wasm32-freestanding) to a WASI component (component model, WASI-preview2). Installation In

Frank Denis 5 Feb 18, 2024
Sealed boxes implementation for Rust/WebAssembly.

Sealed boxes for Rust/WebAssembly This Rust crate provides libsodium sealed boxes for WebAssembly. Usage: // Recipient: create a new key pair let reci

Frank Denis 16 Aug 28, 2022
WebAssembly on Rust is a bright future in making application runs at the Edge or on the Serverless technologies.

WebAssembly Tour WebAssembly on Rust is a bright future in making application runs at the Edge or on the Serverless technologies. We spend a lot of ti

Thang Chung 129 Dec 28, 2022
A console and web-based Gomoku written in Rust and WebAssembly

?? rust-gomoku A console and web-based Gomoku written in Rust and WebAssembly Getting started with cargo & npm Install required program, run # install

namkyu1999 2 Jan 4, 2022
darkforest is a console and web-based Roguelike written in Rust and WebAssembly.

darkforest darkforest is a console and web-based Roguelike written in Rust and WebAssembly. Key Features TBA Quick Start TBA How To Contribute Contrib

Chris Ohk 5 Oct 5, 2021
A Rust ESP stack trace decoder that can also runs in your browser thanks to WebAssembly

ESP Stack Trace Decoder A Rust ESP stack trace decoder that can also runs in your browser thanks to WebAssembly. It is composed of a ⌨️ Rust library,

Maxime BORGES 20 Oct 5, 2022