🌙 A really tiny WebAssembly compiler for demonstration and educational purposes

  • By Thomas Albertini
  • Last update: Dec 28, 2022
  • Comments: 3

Luna 🌙

Luna is a reeeaaally tiny, yet expanding, compiler for WebAssembly Text Format, written in Go and built as one of my quest to conquer the WebAssembly dungeon.

(I just wanted to build something like wat2wasm)

luna

It is so tiny that can only make the four operations (addition, subtraction, multiplication and division) with i32 type numbers.

Why

I've built Luna because I wanted to learn how to build a compiler while learning WebAssembly. So Luna was built for DEMONSTRATION and EDUCATIONAL purposes first.

The goal of Luna is not to do fancy stuff to replace (in a long distant future) solid tools like wat2wasm, wasmer or others...

The goal of this project is to become a useful landmark for anyone approaching WebAssembly and/or for anyone that wants to develop a compiled-to-wasm programming language.

I tried to document each section of the code as much as I could (I'm still doing it) with link to resources I've studied while building this, but if you want to improve it, feel free to open issues and pull requests.

How it works

Followed the amazing articles about the Chasm compiler (a WAT compiler written in Typescript for the Chasm language) and a guide to write a WAT compiler in Rust

  • Luna takes a .wat file (or string if used in the browser)
  • Splits it into tokens ./compiler/tokenizer.go
  • Creates a very simple AST of the tokens ./compiler/parser.go
  • Compiles ./compiler/compiler.go

Use it in the browser 🌐

Luna can also be used in the browsers

Demo: https://luna-demo.vercel.app/

The

make wasm

command will build (or update) with TinyGo the ./example/main.wasm file to be imported in the browser In the ./example directory there's a working example of how to do that.

💡 - make update will simply update/replace the existing main.wasm

💡 - Check the console to see the tokenizer and the parser outputs

Aeon Runtime

Luna also implements a really tiny runtime that can run the exported functions. Read more about it in ./runtime/README.md

Requirements

  • Go
  • Tinygo
  • Make
  • syscall/js set up (or simply comment out the startLuna function and the syscall/js import)

Roadmap

  1. More interactivity

    Currently Luna supports only the renaming of the exported function and some order scrumbling
  2. More arithmetics

    Currently Luna supports only addition
  3. Expansion of Wat syntax

Contributing

To contribute simply

  • create a branch with the feature or the bug fix
  • open pull requests

Luna is by no means finished there are a lot of things that can be implemented and A LOT of things that can be improved. Any suggestions, pull requests, issues or feedback is greatly welcomed!!!

Per aspera ad astra!

Download

luna.zip

Comments(3)

  • 1

    Runtime update

    Made it so that the runtime doesn't need to parse the AST each time a function is invoked.

    Additionally

    • Updated documentation
    • Updated Examples
    • Bumped package's semantic version
  • 2

    Front end refactor

    I have in mind a huge front end refactor for Luna. Basically it will be toward an interactive guide: teaching WebAssembly with Luna (which is the main scope of this)

    I'm currently deciding which framework or libraries to utilize (leaning toward Docusaurus or Gitbook), but if you have any suggestions and want to drop something feel free to comment 💯

  • 3

    Add multi value

    Now that Luna supports subtractions and addition, should be able to call different functions like this

    (module
      (func $subtractNumbers (param i32 i32) (result i32)
        local.get 1
        local.get 0
      	i32.sub
      )
    
      (func (export "addNumbers") (param i32 i32 i32) (result i32)
        local.get 0
        local.get 1
        call $subtractNumbers
        local.get 2
        i32.add))