More Complicated Programs As previously mentioned, Brainfuck is
Turring-Complete and is capable of performing any task that a normal
computer could do. To see how some common functions can be done in
Brainfuck, consider the following program fragment from a typical
programming language:
a=10;
In the example, the value 10 is assigned to a
variable called a. This is probably one of the simplest operations that
can be performed using a typical programming language, however it is a
bit more complicated when using Brainfuck. The first thing to realize
is that the previous example can be implemented in Brainfuck in several
different ways, where some are more efficient than others. For
simplicity, we will use the following method to deal with variables:
1. Variables will not be
designated by a name, instead just by a memory address (this is typical
in most processors at the lower-level of programming) 2. All variables will be
stored at the beginning of the memory array. 3. To access a variable
requires the > command a certain number of times to get to that
spot. After the variable has been accessed we will return The Pointer
to zero so that its ready for the next time to access a different
variable. If this wasn't done, it would be difficult to predict where
the pointer would be during different parts of the program.
Since the program a=10; only contains a single
variable we will only have to use one memory location. And since a
Brainfuck computer should be initialized (on power-up) to zero, we can
take for granted that The Pointer will already be poiting to zero, thus
+ + + + + + + + + +
is our finished program. All this program does is increments the value
at The Pointer ten times. Since The Pointer starts off pointing to the
first memory cell in the memory array (location zero) and since the
value of all of the memory cells are also set to zero at power-up, we
know that by performing ten "+" commands we increases the value of the
first memory cell from zero to ten. So what about multiple variable
programs? Consider the next example:
a=10;
b=5;
c=3;
In this program, values 10, 5, and 3 are
assigned to variable names a, b, and c respectively. In Brainfuck this
operation would be performed by the following code...
+ + + + + + + + + + > + +
+ + + > + + + < <
The first ten commands are identical to the previous
example. They assign the value "10" to the first memory location. The
next command increments The Pointer so that it is now pointing to the
second memory address, which is then followed by five more "+"
commands. So now the first memory locations holds a value of 10 and the
second holds a value of 5. Then there is another > command
incrementing The Pointer again to point to the third memory address
which is then assigned a value of three. Now all "variables" are
initilialized. What about the last "<<" part of the program? This
is used to "point" The Pointer back to the first memory address,
location zero, so that later extensions in the program, if it were to
be added on to, would start from zero and could easily be redirected
since the programmer would always know its start location at any given
time. The draw back to this programming approach is that in some cases
the "reset pointer to zero" technique is not necessary and wastes time.
For example, the code fragment <<>> sends The Pointer back
two spaces and then forward two spaces, completely cancelling out. A
Brainfuck compiler should be set to detect such instances and delete
them*, since they are normally a waste of time.
* In some cases the programmer may have intended the <<>>
to cause a time delay before jumping to another part of the program. In
which case, the compiler automatically erasing that part of the
program could cause undesirable effects.
Now that a general strategy of variable
declaration has been established, we can now move on to more
complicated Brainfuck programming. The next example will explain how to
create a move function which will move the contents of one memory
location to another. This is the premise of another important function
- copy. Below is the pseudo-code.
a=10;
move a to b;
The value of 10 is assigned to variable a.
Then the contents of variable a are moved to variable b. By doing so
the previous contents of variable a are lost. If they were not lost the
function would be a copy. The Brainfuck implementation is the following:
+ + + + + + + + + + [ - > + < ]
The blue portion is the
initialization, which assigns the value of 10 to the first memory
location. The red part can be considered a loop similar to most
programming languages as:
while(a!=0) { a--; b++; } For a more detailed explanation of why
and how [->+<] works, read the Brainfuck
- Part 1 page, which contains the instruction set and description.
Remember, by moving the value of the first memory
address to the second, the first is lost. If this is undesirable, the
copy function is required. It works using a similar structure but
instead involves a temporary variable, as in the following example:
a=10;
move a to c;
move c to (a and b);
In
C++ and many other "normal" languages all the programmer would have to
do is
a=10;
b=a;
The green section initializes
the first memory location(a) to a value of 10. The red section is the
loop which moves the value at the first memory location(a) to the
third(c). Since after the "red" loop is completed The Pointer will be
pointing to zero, the yellow section makes The Pointer point to the
third memory location(c) so that the blue section will be ready. The
blue section then moves the value at the third memory location(c) to
both the first(a) and the second(b) memory location. So, after the
program is complete memory
location(c) is empty and location(a and b) are equal! The black section
cleans things up making future additions to the code easier by returing
The Pointer back to the first memory location, also known as location 0
or "variable" a in this case.