-----------------------------------BRAINFUCK------------------------------------
--------------------------------------------------------------------------------
BASICS
Brainfuck is a minimal Turing-complete programming language invented by
Urban Müller that consists of only eight commands. These commands operate on
an array of bytes (usually 30,000) by moving a pointer along the array &
incrementing or decrementing the byte under the pointer as the programmer sees
fit. At the start of program execution, all of the bytes in the array are
initialized to zero, and the pointer is positioned at the first byte in the
array.
COMMANDS
> increments the pointer.
< decrements the pointer.
+ increments the byte at the pointer.
- decrements the byte at the pointer.
outputs the character represented by the byte at the pointer.
, reads in a character & stores it in the byte at the pointer.
[ executes the code from itself to the matching ] if the byte at the pointer is
nonzero; otherwise, the code is skipped.
] goes back to the matching [ if the byte at the pointer is nonzero.
All other characters are ignored.
IMPLEMENTATION-DEFINED ELEMENTS
As Müller's description of Brainfuck is not very thorough, a number of
aspects of the language are left to its implementors to determine. For
example, it is unclear what should happen when incrementing or decrementing a
byte beyond its storage capacity or when moving the pointer beyond the end of
the array in either direction. Most Brainfuck implementations have the byte or
pointer "wrap around," though this behavior should not be relied upon. The
length of the array & the size of its elements can also vary between
implementations, though most use Urban Müller's original array size of 30,000
cells, and nearly all use 8-bit bytes as the array elements.
A less uniformly implemented detail is how the language handles the
end-of-file condition when reading from standard input with ','. Some
implementations set the current byte to zero or -1, while others leave the byte
unchanged. One should consult the documentation for a specific Brainfuck
implementation before trying to make use of EOF or any other variable aspect of
the language in a program.
ALGORITHMS
This section lists the Brainfuck code necessary to perform certain
calculations with the language, though the exact codes given are not
necessarily the only ways that the calculations can be accomplished. Each
snippet is accompanied by a summary of the operations that it performs on the
segment of the data array that it is applied to, using a notation of my own
design. The two sets of values shown for each algorithm represent the values
of the consecutive cells that the code operates on both before & after
execution. The pointer starts & ends on the first element of the set, and any
elements outside of the given set are not affected. In the notation, 'x', 'y',
etc. represent arbitrary coder-defined values, and '0' indicates that the
appropriate cell either must be or will be equal to zero before or after
executing the code, respectively. The final operations performed on the
variables are represented as they are in C. Note that all of these code
snippets assume that the numeric values involved are feasible under one's
Brainfuck implementation.
If one wishes to manipulate elements that are not organized in
conformance to the given synopses, one must modify the quantities of '<'s &
'>'s appropriately.
Clearing a Value: {x} --> {0}
[-]
Moving a Value: {x, 0} --> {0, x}
[->+<]
Copying a Value: {x, 0, 0} --> {x, x, 0}
[->+>+<<]>>[-<<+>>]<<
Addition: {x, y} --> {0, x+y}
[->+<]
Subtraction: {x, y} --> {x-y, 0}
>[-<->]<
Multiplication: {x, y, 0, 0} --> {0, y, x*y, 0}
[->[->+>+<<]>>[-<<+>>]<<<]
Exponentiation: {x, y, 0, 0, 0} --> {x, 0, pow(x, y), 0, 0}
>>+<[->[-<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<]>[-<+>]<<]<
Division: {x, y, 0, 0, 0, 0} --> {x/y, x % y, 0, 0, 0, 0}
[>[->+>+<<]>[-<<-[>]>>>[<[-<->]<[>]>>[[-]>>+<]>-<]<<]>>>+<<[-<<+>>]<<<]
>>>>>[-<<<<<+>>>>>]<<<<<
Warning: When y=0, an infinite loop will result.
OTHER RESOURCES
http://www.muppetlabs.com/~breadbox/bf/
http://en.wikipedia.org/wiki/Brainfuck
http://esoteric.sange.fi/brainfuck/