Brainfuck

What is brainfuck?

Brainfuck is a turing-complete language, composed of 8 symbols which operate on a single pointer.

For those who are unfamiliar with c or c++, a pointer is merely a type that holds the address of another piece of data. When we increase or decrease the pointer, we increase or decrease the address that the pointer is pointing to. When we increase or decrease what's at the pointer, we increase the value that the pointer is pointing to.

So, for example, if we have a string that reads "hello", and we have a pointer pointing to H, (lets call this 1 to keep things simple), we can do: *p ++; This would increment what p points to, (in this case 'h'), to an 'i'. We can also do: p++; Which will make p point to the next element in the string, which at this time happens to be 'e'.

The symbols that Brainfuck uses are as follows:

symbol description C equivalent
> Increments the data pointer by 1. p++;
< Decrements the data pointer by 1. p--;
+ Increments the value at the pointer *p++;
- Decrements the value at the pointer. *p--;
. Writes the value at the pointer to stdout printf("%c", *p);
, Reads one character from input and sets it as the value at the pointer. *p = getchar();
[ Loops until the value at the pointer is 0. while (*p){
] Closes the loop. }

An example

With all this said, we're in for an example. So I don't break tradition, we'll use a basic hello world.
++++++++++++[>++++++++<-]>++++++++.[-]<[-]
++++++++++[>++++++++++<-]>+.[-]<[-]
++++++++++[>++++++++++<-]>++++++++.[-]<[-]
++++++++++[>++++++++++<-]>++++++++.[-]<[-]
+++++++++++[>++++++++++<-]>+.[-]<[-]
+++[>++++++++++<-]>++.[-]<[-]
+++++++++++[>++++++++++<-]>+++++++++.[-]<[-]
+++++++++++[>++++++++++<-]>+.[-]<[-]
+++++++++++[>++++++++++<-]>++++.[-]<[-]
++++++++++[>++++++++++<-]>++++++++.[-]<[-]
++++++++++[>++++++++++<-]>.[-]<[-]
++++++++++.[-]

So what does all this do? First, we set a pointer to a value we can multiply. So for h, we set the value to 12. Then in a loop, we increment the pointer to point to the field after the 12, increment by 8, move the pointer back and subtract. Because the loop runs as long as the value at the pointer isn't 0, we affectively multiply 12 by 8 to get 96. After that, we just add 8 more to make up the 104, which is the ascii value for 8. After we have the needed byte, we print, then use a loop to set the value of the current pointer and the one after it to 0, so that we are able to print our next character.

Tools and utilities

bf.cpp

Bf.cpp is a small brainfuck interpreter I wrote in c++. It compiles great with g++ and visual studio. Just feed it the name of the file to interpret and it will execute it.
Download it here:
bf.cpp

bf2c.cpp

BF2c will take a program written in Brainfuck and convert it so that it can be compiled by a c compiler. While this is fairly trivial, we took another cool step and combined like operations. For example:
+++++ becomes:
*p+=5; Download it here:
bf2c.cpp