Skip to main content
NB
Screenshot of Go code in a text editor, displaying several functions for parsing recipes,
  booleans, strings, and arrays.

Why Make An Interpreter In GO

I’ve been curious about creating a programming language for years now but never curious enough to search out the materials that would teach me. I learned how to program at community college so I never got a class talking about languages or computing at that level.

Then one day I came across the book ”Writing an Interpreter in GO” by Thorsten Ball and it just clicked. This was the perfect opportunity to not only learn about interpreters but I could also create my own (small) language and learn some simple GO programming.

Why Cottage Pie ?

Because it’s my favourite kind of pie!

I decided to go with a baking theme for my language and that just happened to be the name that stuck. I thought up a few (that I can no longer remember) and couldn’t really say why Cottage Pie won out over the others. It felt fun and quirky (and maybe I was a little hungry at the time too) which meant it would keep me invested longer.

I didn’t want to follow the book word-for-word because I wouldn’t learn that way, the code would go in one hand and out the other.

How I Learn Best: Make It Mine!

When I’m following along with any tutorial I learn best by changing parts of what I’m making so that I can put a bit of myself into it. This also allows me to run into errors the “happy path” of a tutorial would avoid.

One example of where I changed things up and had to figure out how to adjust was in Cottage Pie a value is assigned to a variable with the to keyword instead of an =. This required me to change the defined token from ”=” to “to”. I also changed a few of the keywords you would normally use in popular languages to fit the theme a little better.

The main problem this brought up was it forced me to really think about the examples in the book and how they translated to my project. Sometimes the different language caused extra confusion but it also helped me solidify what I was learning.

Example Language Usage

Variables are defined using the keywords bake and to.

bake age to 22;
bake name to "CottagePie";
bake result to 10 * (20 / 2);

Besides integers, booleans and strings, the CottagePie interpreter also support arrays and hashes. Here’s what binding an array of integers to a name looks like:

bake my_array to [1, 2, 3, 4, 5];

And here is a hash, where values are associated with keys:

bake niko to {"name": "Niko", "age": 22};

Accessing the elements in arrays and hashes is done with index expressions:

myArray[0]       // => 1
niko["name"]     // => "Niko"

The bake statements can also be used to bind recipes (functions) to names. Here’s a small recipe that adds two numbers:

bake add to recipe(a, b) { serves a + b; };

CottagePie not only supports serves (return) statements, implicit serves values are also possible ! Which means we can leave out the serves keyword if we want to:

bake add to rc(a, b) { a + b; };

Future Plans?

I had a few ideas for features to add to Cottage Pie but never really had the time to pick the project back up. Here the few are:

Function declaration support

I wanted to support standalone function declarations alongside assigning functions to variables because this is my preferred method of defining functions when I’m writing code. This was an area I identified immediately as something I wanted to implement and it sits at the top of the dusty queue.

Comment support (”//“)

Currently Cottage Pie doesn’t support comments but I wish it did. It one day might if I have some time to devote to it.

Add line numbers (and maybe column numbers) to error messages

For longer files having line and column numbers included in the error messages would make debugging much easier and less stressful. I know I hate being hit with a giant error message that doesn’t have much information so I would like to include as much of it as I can in the Cottage Pie error messages.