Home
Brainf**k Interpreter
What is Brainf**k?
Brainf**k (BF) is an esolang centred around a single array of bytes

There are only 8 operators in BF:
  • > Increment the pointer position
  • < Deincrement the pointer position
  • + Increment the byte at the pointer
  • - Deincrement the byte at the pointer
  • . Output the byte at the data pointer
  • , Accept one byte of input, storing it in the byte at the pointer
  • [ If the byte at the pointer is zero, goto the corresponding ] operator
  • ] If the byte at the pointer is nonzero, goto the corresponding [ operator
Despite only having these 8 operators BF is (with an infinitely large array) turing complete
Implementing Brainf**k
Around the time that I found out about BF I was also learning the programming language Rust

I decided to write my BF interpreter in Rust as it provided a great opportunity to start getting to grips with Rust's match statements and iterators

My brainf**k interpreter has 3 main parts:
  1. Read and parse the program
  2. Read input
  3. Interpret the program
Lets break down how I solved each of these presentations
Reading and parsing the program
The program functions as an interactive session interpreter, so it will keep taking lines of user input from the terminal until the user enters "run"

For every line of user input the program will iterate through all the characters and filter out any that aren't one of the 8 operators

Rust code showing iterator I used
Reading user input
I made the design decision of having all user input entered before the program is interpreted.

This meant that all input could be entered quickly on a single line
Interpreting the program
The program uses a while loop to keep running through the characters in the program until the end has been reached

Inside the while loop a match statement is used to match the current operator to the current code to run for that operator