Subscribe to gabehabe's on-topic ramblings        RSS Feed

My First Interpreter: Brainfuck

Icon 2 Comments
I love a bit of brainfuck. I've been working on this interpreter for about a total of an hour, and it's already finished. It's fully functional, and interprets any brainfuck application.

The best part is, it's only 61 lines long! Interpreting the brainfuck code itself takes only 12 lines~ But I've added some more functionality to my application so that it can be called from the command line, and interpret at runtime. No translated code, no output files~ It simply executes the BF code.

I've added two commands to it:
BF-i -? will display a little bit about the program~ Who it was written by, which language it was written in, etc etc~

BF-i -i C:\ will interpret the file located at C:\

Simple to use! :D

It's available to download here, including the MS executable, C++ source code, and a small about file.

I'll write up a tutorial on creating an interpreter soon. :)

Lastly, here is the code:
 * A brainfuck interpreter
 * Author: Danny Battison
 * Contact: [email protected]

#include <iostream>
#include <fstream>
using namespace std;

void DisplayHelp();

int main(int argc, char *argv[]) {
    if (argc < 2 || argc > 3) {
        return EXIT_FAILURE;
    if (!strcmp(argv[1], "-?")) {
                 << "This program is designed to interpret a brainfuck application." << endl
                 << "Written in C++" << endl
                 << "Contact the author: gabehabe (at) gmail (dot) com";

    else if (!strcmp(argv[1],"-i")) {
            if (argc < 2 || argc > 3) {
                return EXIT_FAILURE;
            } else {
                ifstream file;
                if (!file.is_open()) {
                    cout << "ERROR: File could not be found.";
                    return EXIT_FAILURE;
                string bf = "";
                char buffer;
                bf += buffer;
                while (!file.eof()) {file.get(buffer); bf += buffer;}
                char* ptr = new char[5000];
                int sub;
                for (sub = 0; sub < 5000; sub++) ptr[sub] = 0;
                sub = 0;

                unsigned int i;
                int loopPosition = -1;
                for (i = 0; i < bf.length(); i++) {
                    switch (bf[i]) {
                        case '+': ptr[sub]++; break;
                        case '-': ptr[sub]--; break;
                        case '>': sub++; break;
                        case '<': sub--; break;
                        case '.': cout << ptr[sub]; break;
                        case ',': ptr[sub] = getchar(); if (ptr[sub] == 13) {ptr[sub] = '\0'; loopPosition = -1;} break;
                        case '[': loopPosition = i; break;
                        case ']': if (loopPosition != -1) {if (ptr[sub] != '\0') i = loopPosition-1; else loopPosition = -1;} break;
            return EXIT_SUCCESS;

void DisplayHelp() {
    cout << "Invalid arguments. Example Usages:" << endl
             << "\tInterpret:\tbf.exe -i C:\\" << endl
             << "\tabout:\t\tbf.exe -?";

2 Comments On This Entry

Page 1 of 1


03 November 2008 - 08:03 PM
Great! You've done an interpreter, now do a compiler that makes it an .exe on it's own!


04 November 2008 - 04:49 AM
That's the next step. I'm looking for some books on compiler theory. ;)
Page 1 of 1

May 2018

202122232425 26

Request A Topic!

Want me to blog about something? Perhaps a language? A piece of software? A specific topic? Let me know! Even guests can post here on my blog!

If you would like to request a topic, please post a comment here and I'll get on it right away! smile.gif

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

gabehabe's off-topic ramblings

Follow me on Twitter!
lol, my other blog died a horrible lonely death. Ah well.

Smiley of the [however often I change it]

IPB Image

Contact Me

e-mail: [email protected]

Google Talk: [email protected]
MSN: [email protected]
Yahoo: gabehabe (rarely used)
AIM: gabehabe (rarely used)

Skype: gabehabe

Want me to work for you? [click]