Git Product home page Git Product logo

chasm-rs's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.