Quest for Gaming

vbfi - Visual Brainfuck Interpreter

Author avatar image

Posted by: Daerandin
Lord Paladin

When I got interested in the brainfuck programming language, I decided that one way to really get an idea for how the programming language works is to actually write an interpreter. For a more detailed look at the actual brainfuck programming language, check out my previous article on it.

If you don't know what an interpreter is, then let me give you the simple version of it. An interpreter can read code for a certain programming language, and then interpret it into machine code to be executed. So my brainfuck interprefer can read a file with brainfuck code, and execute that code. An interpreter will execute the code one instruction at a time. So if there is an error in your code, it will not be detected until that very instruction is executed.

There are already a lot of interpreters for brainfuck, but I wanted to be able to actually see what is happening as the code executes. So my interpreter has the optional feature of giving a visual representation of what is happening in memory while the code executes. I will provide a small example here with the following code:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.

A sample of the output from my interpreter would be:

Current command being run: +

Memory position        0     1     2     3
Decimal value        010   007   010   002
Hexadecimal value     0a    07    0a    02
Current position                         ^


Current command being run: +

Memory position        0     1     2     3
Decimal value        010   007   010   003
Hexadecimal value     0a    07    0a    03
Current position                         ^

The output above comes from the first loop, from instruction 33 and 34. We can see that memory position number 3 is incremented twice. The output displays the memory values in both decimal and hexadecimal. Hexadecimal is actually pretty fitting for an 8-bit byte, since each digit represent four bits, or half the byte.

If you are running Linux you can download the source code or this interpreter from Gitlab.

On Linux, you just need to unpack the archive and run 'make' inside the directory of the source code. This will create the vbfi executable. when you run the vbfi executable from a terminal, you need to also provide the path to your brainfuck code file as an argument. If you want to see how the code is changing memory, you must also include the -v argument. For example, if you are within the directory of the executable, you could run it like this:

./vbfi -v /path/to/your/brainfuckcodefile.txt

The -v argument will produce a lot of lines and it may be ideal to pipe the output into a file so you can can examine it in your favorite text editor. The line below will pipe the output into a text file called "output.txt":

./vbfi -v /path/to/your/brainfuckcodefile.txt > output.txt

Windows

Alright, if you belong to the vast majority of people, then you are running Windows, and you also don't know how to compile software. In that case, I got you covered. I have compiled both a 32-bit and 64-bit executable for windows. If you don't know which one you need, just try both, one of them will work.

32-bit download: vbfi32.exe

64-bit download: vbfi.exe

Now, this program is not a graphical program, it must be operated solely from the command line. So on windows, you will need to find "cmd", which you can search for on your start menu. It is outside the scope of this little article to tell you how to operate the command line on Windows, but assuming you know how to change directory in the command line (the command is cd), then simply navigate to the directory where you have the vbfi.exe (or vbfi32.exe). Then you simply run:

vbfi.exe c:\path\to\brainfuckcodefile.txt

If you downloaded the 32-bit exectuable, just replace vbfi.exe with vbfi32.exe. You can also redirect output into a file on windows, which is very useful when using the -v argument:

vbfi.exe -v c:\path\to\brainfuckcodefile.txt > output.txt

This will create a file called "output.txt" in the same directory as the vbfi.exe file. This file will containt the full output of the program, which is a lot since it displays the current memory values for each instruction read.

Final words

This interpreter is primarily intended to be educational in helping to see how memory is changed for each intruction executed. Currently, the -v argument will print out a huge amount of lines, which essentially makes it useless unless you redirect output into a file. But apart from this limitation, being able to check through the output and see exactly what each instruction is doing in memory, is definitely a big boon to understanding brainfuck.

It was fun to create this, and I might add more features in the future, or possibly make the memory management a bit more efficient. But for now, I am happy with this interpreter.

Categories: Programming, Free Software

You need to Log in to comment.
If you don't have an account, then feel free to Create an account