Progress update, June 19

As this is my first update, I’ll be a bit more explicit about where everything is up to, and later updates will probably be more brief. 

Braid, an ML-like language that compiles to Go, is still in the very early stages. For simplicity’s sake, I’m also writing it in Go, so both the compiler and the final language will be built on it. I’ve started with the front-end of the compiler, which means lexing and parsing source code into an AST. For lexing and parsing, I’m using Pigeon, a library for building Parsing Expression Grammars. Previously I’ve only used EBNF libraries, a fairly different way of building up grammar rules, so PEGs are a big change in approach for me. I’m still working my way along the learning curve.

My grammar supports some basic stuff so far, like assignment:

let a = 5 * (6 - 1)

If statements (and if expressions):

if a < 5 {
  # do a thing

Function definitions:

let main = func {
  # do stuff here

And function calls:

let b = add 2 3

I can already compile all of these to Go, because the concepts are quite similar. That’s the easy stuff!

For something more complicated I’ve just started work on type definitions. What in other languages is often called “generics” is accomplished in OCaml with “parameterised types”, types containing other types, but as parameters that are only given a concrete type when used. Here’s a typical example of how this is used, the Option type:

type Option 'a =
 | Some 'a
 | None

In this example, 'a is our type parameter which can stand in for any concrete type.

So far Braid can parse variant types, record types (very similar to Go structs) and alias types (again, similar), but compiling these will be harder. For our Option example above, each version with a different concrete type parameter will need to be compiled to a separate Go type.

There’s still plenty more to do even after that, including working out how best to represent other high-level and FP concepts in Go, handling imported modules, type inference, interfacing with native Go functions, writing useful error messages, and a standard library. Much that has been planned but not implemented will change, too, as I figure out ergonomics and what works best. And of course, I’ll hit plenty of dead ends because I don’t know what I’m doing.

It’ll be an interesting journey for sure! I’ll start publishing the source code on GitHub when I’m a bit further along.