AST to WebAssembly
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
wabt @ b877bb8
.gitignore
.gitmodules
Ast.hs
CONTRIBUTING.md
Check.hs
Codegen.hs
LICENSE
Main.hs
Makefile
Parse.hs
README.md
Setup.hs
Symtab.hs
Tokenise.hs
Util.hs
Wasm.hs
Wat.hs
ast2wasm.cabal
calls.hl
ctof.hl
demo-wasm.html
examples.hl
fib.hl
fib_handwritten.wat
let.hl
load.js
quad.hl

README.md

ast2wasm

I'm experimenting with WebAssembly, an intermediate code representation for the web (and elsewhere). So I'm writing a code generator that targets it.

Copyright 2018 Paul Bone License: MIT

This repository contains a simple (and naive) compiler for a functional language, the compiler is written in Haskell. The most interesting file is probably Codegen.hs which transforms the AST (Ast.hs) into WebAssembly (Wasm.hs). The WebAssembly is written out in its text format and assembled with the wat2wasm external tool.

Once you build the project you can open Demo-wasm.html in your web browser and try it out. Several example programs are in the files with the .hl file extension.

Presentation

I developed this as an example of a code generator for a talk I gave at Compose :: Melbourne 2018.

Dependencies

You'll need some kind of C++ toolchain, whatever wabt (see below) requires. Plus Haskell and Cabal. And a modern browser that support WebAssembly (pretty much anything current & mainstream except IE) I recommend Firefox ;-).

Building

I tested this on Linux.

You'll need the wabt git submodule, it contains the WebAssembly Binary Toolkit, mainly the wat2wasm tool, after you checkout do:

$ git submodule init .
$ git submodule update --checkout .

Part of the build includes downloading Haskell packages (into the local directory via cabal). You'll need to update cabal package lists first:

$ cabal update

You should be able to build the toolkit and this with:

$ make

It may need more tools or libraries installed such as cmake, C header files and libraries.

Links

I used these resources:

While researching this I also found, but didn't use:

This could be extended in these ways:

  • Basic type checks
  • Higher order values/calls
  • Cons cells?
  • Environments?