DEV Community

Cover image for Luna πŸŒ™ - Wrote a tiny WebAssembly compiler, that also runs in browser, built for demonstrative and educational purposes.
Thomas Albertini
Thomas Albertini

Posted on


Luna πŸŒ™ - Wrote a tiny WebAssembly compiler, that also runs in browser, built for demonstrative and educational purposes.

I've been wanting to learn how to make my own programming language for a really long time, but I could not find the road in the hard theory behind compilers, parsers, ASTs etc... I found the tutorials hard and my attention was not lasting for more than 5 minutes before completely shut down the project.

Some days ago, though something changed.
I'm currently in love with WebAssembly and had this absurd idea of developing a programming language that compiles to WebAssembly.

I didn't know how WebAssembly internals worked so it was the perfect stage to learn something new and make a cool project.


Started doing some researches and I found no tutorials except two amazing articles, one was how to build a compile for a custom programming language that compiles to WebAssembly (exactly what i was looking for), the other was a series of articles on how to build a Web assembly compiler (exactly what i was looking for, again!)

But here come the problems: the first was written in Typescript and the second in Rust.

I wanted to practice Go and I don't know anything about Rust.

So long story short I studied the theory behind those two amazing articles, WebAssembly specification etc... and I started writing a compiler in Go (i love Go).

I've really found no articles about writing WebAssembly compilers with Go, so I decided to document the building of Luna so future Gophers that will have my same idea won't have to struggle as much as I did to find resources.

Luna screenshot

Luna mainly compiles WebAssembly Text Format to Wasm, but with small modifications in the tokenizer and parser, it could also serve as a blueprint for developing custom programming languages in the browser that compile to WebAssembly.

So this is Luna and in this series of articles I will explain EXACTLY how I built it.

Meanwhile you can have a look at:

Luna is really tiny and it's in early stage, feel free to contribute (update docs, open PR, issues, leaving feedback or suggestions etc...), anything is welcomed!!

Latest comments (0)

11 Tips That Make You a Better Typescript Programmer


1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!