How to Write a Brainfuck Interpreter in C#
Tags: csharp
Published: 2019-10-13 16:16:17

I’ve always been somewhat interested in how programming languages are written. Last month I bought [Writing An Interpreter In Go](https://interpreterbook.com/), and am about 40% of the way through that book (though my version is in C#). I’ve hand built a tokenizer and am starting to write my own parser.

It’s gotten me curious about programming languages, and I wanted to try my hand at building a brainfuck interpreter. Lets dive in.

#Brainfuck
[Brainfuck](https://en.wikipedia.org/wiki/Brainfuck) is an [esoteric programming langauge](https://en.wikipedia.org/wiki/Esoteric_programming_language). It’s designed to be confusing, hence the name. The language only consists of 8 characters, `>` `<` `+` `-` `.` `,` `[` `]`. With an internal structure defined by [wikipedia](https://en.wikipedia.org/wiki/Brainfuck#Language_design) as:

*The brainfuck language uses a simple machine model consisting of the program and instruction pointer, as well as an array of at least 30,000 byte cells initialized to zero; a movable data pointer (initialized to point to the leftmost byte of the array); and two streams of bytes for input and output (most often connected to a keyboard and a monitor respectively, and using the ASCII character encoding).*

Each character in the language is defined as:

`<` : increment the data pointer (to point to the next cell to the right)
`>` : decrement the data pointer (to point to the next cell to the left)
`+` : increment (increase by one) the byte at the data pointer 
`-` : decrement (decrease by one) the byte at the data pointer. 
`.` : output the byte at the data pointer
`,` : accept on byte of input, storing its value in the byte at the data pointer
`[` : if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
`]` : if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command. 

Here is a program that prints out “Hello World”:

{% gist https://gist.github.com/MorganW09/522f19039b85c6f5ef74dca9147d04e1 %}

If this is your first esoteric programming language, this definitely throws you for a loop. Since this article is about documenting how to build an interpreter, here’s a [great video](https://www.youtube.com/watch?v=-3C200nCwpk) describing brainfuck to anyone to wants a more visual representation.

So lets see some basic code examples.

#Examples
If I wanted to write a single exclamation point, here is a program that would do that:

{% gist https://gist.github.com/MorganW09/4d1c655878e5a69c95a37c79fe7996d3 %}

It uses the `+` character to add 1 to the current data pointer 33 times, 33 being the ASCII representation for an exclamation point. Then uses the `.` character to print that single character.

Here are three different programs that print out `!?`:

{% gist https://gist.github.com/MorganW09/c7595c589d535726927f135f7b6f8a6c %}

{% gist https://gist.github.com/MorganW09/84bdaa8daacc8a61b1630bf4aff5d3a0 %}

{% gist https://gist.github.com/MorganW09/0b0ba74dbcdac2ad22c3e9a296399734 %}

The first version generates and prints the exclamation pointer as before, then moves to the next cell and increments data pointer to 63, the ASCII representation for ?, and then prints it.

The second version reuses the cell of the exclamation mark, so it only needs to increment that cell 30 more times. 33 (!) + 30 = 63 (?)

The third version uses the `[` and `]` brackets to repeat sections of code resulting in smaller programs.

#The Interpreter
I’ve named my interpreter [Dizzy](https://github.com/MorganW09/Dizzy), because programming in brainfuck can make you dizzy.

Well that aside, there’s not a whole lot of complexity to the interpreter.

Lets start off by creating a class called `Interpreter` that will be interpreting our program.

{% gist https://gist.github.com/MorganW09/bf898bb5dd971863c6904c2dd1cd7054 %}

It has three instance variables: 1. A byte array to hold any data we might need, 2. A pointer telling us where we are in that array, 3. The input converted into a char array.

Then lets create a scaffold of our Run method, which is a switch statement for each of the 8 valid characters in our language.

{% gist https://gist.github.com/MorganW09/0a24058639bc16e0e6a6b4d4ea880bfe %}

*In brainfuck, any other characters are just ignored, so anything not in this set of 8 will just be skipped over.*

Given our definition above, for `>` and `<`, we need to increment and decrement the pointer.

{% gist https://gist.github.com/MorganW09/ab27d2aafdf8008c0e69b4ed4fd71c98 %}

Then for `+` and `-`, we need to increment and decrement the current value in our tape referenced by the pointer.

{% gist https://gist.github.com/MorganW09/8ef34bf7c5234cd25ab9a3f9d55c68ab %}

Then our IO characters are also pretty simple:

{% gist https://gist.github.com/MorganW09/d43edb275390c8ba6bab7094cb05ef3d %}

Finally we need to implement our brackets. Brackets help us repeat or skip sections of a brainfuck program, kind of like a goto statement. That’s why the third program for writing !? was so much shorter, brackets allowed the same section code to be run multiple times.

The only complication here is nested brackets. For a program like `[+[+]+]>+`, which skips from the first open bracket to the last close bracket, we need to keep track which open/close brackets belong together.

We introduce an int called `unmatchedBracketCounter`, (defined outside the for loop) which tracks how many pairs of brackets we’ve encountered. If we’re looking for a `]` and encounter another `[`, we know we will have to now pass two `]` before we can stop.

{% gist https://gist.github.com/MorganW09/b25aa2854345a2b9eedc06a2756e2b3a %}

Combining all these elements we end up with a single class that can interpret a brainfuck program.

{% gist https://gist.github.com/MorganW09/478f0e9fa9058120a7509e4400881bb9 %}

#Testing

Now for testing. As someone who codes for a living, I feel very comfortable writing programs in normal programming languages. Brainfuck programs are another matter.

Fortunately I found this [great website](https://copy.sh/brainfuck/) which has brainfuck programs, can run brainfuck programs, and can generate brainfuck programs as well.

Here are some fun programs that I found on that website.

##CALCULATE PI

{% gist https://gist.github.com/MorganW09/ceff7686433d89e8381d00c4893bfce1 %}

This calculates Pi to the 15 decimal place. When run the output is `3.14070455282885`, which the first 15 digits of PI are `3.14159265358979`, which I am using an 8 bit cell, so it seems like I’m experiencing some overflows as called out in the program.

##CALCULATE SQUARES

{% gist https://gist.github.com/MorganW09/65d0ee0d81c2dcd95c94e24ec8801ee6 %}

When run this programs outputs all squares up to 10000, 0, 1, 4, 9 16, …, 9801, 1000.

##99 BOTTLES OF BEER

{% gist https://gist.github.com/MorganW09/ec2260a6b607356d9b315e24e695dc14 %}

When run this prints out the 99 bottles of beer chanty, from 99 all the way to 0.

##PRINT ONE ENTERED CHARACTER
And finally a program that takes advantage of user input. Most brainfuck programs that I found didn’t use the input character ,, I wrote one with the help of the text generator mentioned earlier.

{% gist https://gist.github.com/MorganW09/db16a088df80c2b2288b1a8925502569 %}

#Closing Thoughts
There you have it. A interpreter that will run brainfuck programs.

I definitely enjoyed writing this interpreter. The first one that I’ve written from scratch. It was a fun and pretty different programming challenge from what I’m used to.

I hope you enjoyed it and continue to experiment with things that you’re interested in.

*Originally published at [thesharperdev.com](https://thesharperdev.com/how-to-write-a-brainfuck-interpreter-in-c/).*