Brainfuck 

Brainfuck and higher language instructions
Date: 15-11-03
Author: Michael Hielscher
Homepage: www.genesis-x7.de

How to write C++ like code structures in BF

1. Values and variables
To create a integer variable you can just use one of the 30000 BF array bytes. This is comparable to a char variable in C++. To only problem: You need to remember where you stored the value in the BF array.

char c = 10;

[-]++++++++++

This would be the solution with BF - just 10 times + to increase the value we first cleared to 0.
So keep in mind: [-] clears a array value at current pointer position.

We placed the value (10) at the current pointer position - that means we need to remember where this position was when we later want to use this variable again.

If we now want to declare 2 char variables we need to change the pointer position:

char c1 = 5;

char c2 = 3;

[-]+++++>[-]+++

To add two variables we need to store one of them at a temporary position because original data will be lost:

c1 = c1 + c2;

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

A short explanation for this example (requires that pointer points to c2):
First we need a free temp variable to "remember" c2. So we go one steep forward and clear that place for our temp value. Now we go back to c2 and decrement the value in a loop. In that loop we go one steep down to c1 and add 1 to it. After that we go up to temp and add 1 there too. Now we go back to c2 and decrement it. This loop works until c2 is 0. Than we go to the temp value (one steep forward between the brackets). We loop again and "shift" back the value from temp to c2. Now we have c1 = c1 + c2, c2 is still the same and temp is 0 again.

Now we come to some heavier code elements a condition:

if (c1 = 10) { c1 = 0; }

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

A bit harder to explain but its possible to understand ;o)
First we need 2 temp values for this example. First we need to save c1. So we put the value of c1 to t1 and t2 (this is done in the first bracket). Now we go to t2 and "shift" the value back to c1 (second bracket). After that we put a 1 to t2. We will need this value later. Back to t1 which is still the same than c1. We decrement 10 times cause we want to check if c1 was 10 (any number is possible). Now the trick: The next bracket will only be executed if we have a value unequal to 0. So if c1 was exact 10 the content of this bracket will never be used. If we have a different value now we go to t2 and set it to 0 (before that it is 1 like said above). Now we set the pointer to t2 and the last bracket will only be evaluated if there is still the value 1 in t2. That bracket is equal to the { } brackets of the C++ term. C1 will be set to 0 in this example but everything else is possible at this place.

The problem of variable positions in the BF array could be organized like this:

{
 char c1;
 char c2;
 {
  char c3;
 }
}

Now you can say with every "{" you make a ">" in BF and with every "}" a "<" than you have your array pointer relative to your last statementblock. To count how "deep" you are you it would be possible to add something like a counter at the beginning of the array. So it would be possible to have global and local variables as well.

At last a while loop:

while (c1 = 10) { c1 = 0; }

[-]+[ IF-BLOCK [ LOOPBODY +] -]

We set a temp value to 1 and begin a loop. We can use a IF construction for the condition. If this condition is true we can do the loop body. After that we increment the temp value to 2. Now we decrement it to 1 if condition was true else we get temp to 0. If the value in temp is 0 at the end of the main bracket the whole while loop ends.

With the knowledge of this few constructions it should be possible to implement many many others ;o)