I’ve wanted to learn about compilers for a while, but it hasn’t been offered at Cornell since 2023, so I decided to learn on my own by making this small compiler. It was written in TypeScript and compiles a subset of OCaml (which I’ll call TinyOCaml) to WebAssembly (Wasm). Here is the link to the demo.

For more details like the exact BNF grammar of the language, check out the GitHub repo.

You can try your own code, and does parsing and type-checking (using algorithm W). You can then see the abstract syntax tree, intermediate representation, and Wasm code (and run it).

I also used the abstract syntax tree (AST) generated to make a JavaScript interpreter, which I compared against the WebAssembly in several benchmarks.

Benchmarks
Fig. 1. Several programs and their speedup in Wasm compared to interpreted Javascript.

Interestingly, the speedup was dramatically reduced and sometimes reversed when I tried running the benchmarks from a mobile browser. I think this might have to do with less optimized pipelining when it comes to mobile execution of Wasm code.