Brainfuck

Brainfuck.
This language uses a pointer and the value it points to, which you can both increment. In C you can imagine it as an integer that points to an index within a character array where you can increment the integer pointer or the value within the array that the integer points to. There are 8 single letter commands in this language, and none of these commands require arguments. The commands are as follows:

>   Increment pointer
<   Decrement pointer
+   Increment byte at address of pointer
-   Decrement byte at address of pointer
.   Output byte at address of pointer
,   Store byte of input at address of pointer
[   Jump to command after matching ] if byte at address of pointer is 0
]   Jump to command after macthing [ if byte at address of pointer is not 0

BF can be translated to C using the following substitutions (assuming that ptr is an unsigned char* and points to an array of zeroed byte):

+-----------+----------------+
| Brainfuck |     C          |
+-----------+----------------+
|     >     |  ++ptr;        |
+-----------+----------------+
|     <     |  --ptr;        |
+-----------+----------------+
|     +     |  ++*ptr;       |
+-----------+----------------+
|     -     |  --*ptr;       |
+-----------+----------------+
|     .     | putchar(*ptr); |
+-----------+----------------+
|     ,     | *ptr=getchar;  |
+-----------+----------------+
|     [     | while (*ptr) { |
+-----------+----------------+
|     ]     |      }         |
+-----------+----------------+

Also comments in the code can just be typed out. Any character that is not 1 of the 8 bf commands are ignored.
Here’s hello world in bf from wikipedia:

+++++ +++++             initialize counter (cell #0) to 10
[                       use loop to set the next four cells to 70/100/30/10
    > +++++ ++              add  7 to cell #1
    > +++++ +++++           add 10 to cell #2 
    > +++                   add  3 to cell #3
    > +                     add  1 to cell #4
    <<< ++ .                  print 'H'
> + .                   print 'e'
+++++ ++ .              print 'l'
.                       print 'l'
+++ .                   print 'o'
>++ .                   print ' '
< .                     print 'o'
+++ .                   print 'r'
----- - .               print 'l'
----- --- .             print 'd'
> + .                   print '!'
> .                     print '\n'

I’m not an expert at c++ but I did manage to make this code for a brainfuck interpreter:

#include <iostream>
#include <stdio.h>
using namespace std;

unsigned char cells[30000];
unsigned char *dp;
char cmd;

void exec(char cmd, FILE *bfcode);

int main(int argc, char *argv[])
{
    if (argc<=1) {
        cout << "Brainfuck Interpreter by Jakash3" << endl << "Usage: bf.exe file";
    } else {
    FILE *bfcode;
    bfcode=fopen(argv[1],"r");
    dp=&cells[0];
    while ((cmd=getc(bfcode))!=EOF) {
        exec(cmd,bfcode);
    };
    fclose(bfcode);
    }
    return 0;
}

void exec(char cmd, FILE *bfcode)
{
    char c;
    int fpos;
    if (cmd=='>') {
        ++dp;
    } else if (cmd=='<') {
        --dp;
    } else if (cmd=='+') {
        ++*dp;
    } else if (cmd=='-') {
        --*dp;
    } else if (cmd=='.') {
        putchar(*dp);
    } else if (cmd==',') {
        *dp=getchar();
    } else if (cmd=='[') {
        fpos=ftell(bfcode);
        while(*dp!=NULL) {
            fseek(bfcode,fpos,SEEK_SET);
            c=getc(bfcode);
            while(c!=']'&&c!=EOF) {
                exec(c,bfcode);
                c=getc(bfcode);
            }
        }
    }
}

2 thoughts on “Brainfuck

  1. >++++++++[-]>+>+>++>[-]+[->+<]<.+++++++..+++.>
    ->+++++++.[+>]>]<<>.+++.——.——–.>>+.>++++.

    Your BFI fails with this Hellow World program.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: