Progress update, November 12

I can now successfully compile a small and boring subset of Braid to Go!

A (nonsense) example Braid module:

module Main

# test
let cheesy = func item item2 {
        # more test
    item ++ " and " ++ item2 ++ " with cheese"
}

let tester = func a {

        let result = a + 1

        if result > 5 {
                a + 1
        } else {
                a + 100
        }
}

let main = func {
        let something = func {
                4 + 9
        }
        let a = something()
        let yumPizza = cheesy("pineapple", "bbq sauce")
        # some comment
        let five = 1 / 1 + 3 * (55 - 2)

}

Compiles to:

package main

func cheesy (item string, item2 string) string {

        return item + " and " + item2 + " with cheese"
}

func tester (a int64) int64 {
        result := a + 1
        var __temp_0 int64

        if result > 5 {
                __temp_0 = a + 1

        } else {
                __temp_0 = a + 100

        }

        return __temp_0
}

func main () {
        something := func () int64 {

                return 4 + 9
        }

        _ = something()
        _ = cheesy("pineapple", ("bbq" + "sauce"))
        _ = 1 / 1 + 3 * (55 - 2)

}

There are still some edge cases to handle with type inference, and a lot more features and syntax to support, but I’ve handled the Go-specific syntax quirks (like function definition variations inside and outside function scope) and its refusal to compile if there are unused variables. Surprisingly, tracking variable usage wasn’t too hard to do. Immutability is also supported, by throwing errors if a variable is reassigned-to.

Interesting things of note:

  • The underscores in the last few lines of main, meaning we know the result is unused. This is required by the Go compiler.
  • The implicit return from the if-expression within tester, and how this compiles using a temporary variable. let result = if { expression } is also supported.

I think the next thing to do will be to handle those remaining quirks and edge cases that don’t yet compile. Then I’ll have to decide if I want to add support for importing of Go functions, meaning the standard library is opened up to us, or start implementing type inference and compilation for more involved types, like sum and record types.

Both are interesting and useful, so I’m not sure yet.

Leave a Reply

Your email address will not be published. Required fields are marked *