BrainFuck is a minimalistic but Turing complete
programming language devised by Urban Mueller. It is
oriented around a tape, a read/write head, an input
device and an output device.
There are 8 instructions available:
| Character | Name | Action |
|---|---|---|
| + - | val | increase/decrease the value of the tape cell pointed to by the head |
| > < | ptr | move the head 1 position to the right respectively left |
| [ | do | loop while the contents of the tape cell is nonzero |
| ] | od | closes a loop |
| , | get | read a value from the input device and write the ASCII code of that value to the current tape cell |
| . | put | output the value from the current tape cell as an ASCII character to the output device |
Any other character represents a comment.
The de facto default values for the environment are:
The head position and cell values are mostly wrapped. Initially, the head points to cell 0 (the leftmost one) and all cells contain the value 0. So, both "[-]" and "[+]" set the current cell value to 0 and "[-]." outputs a NUL character.
The program uses a as name for the tape and
p as the value of the head's position.
For each token, a separate class has been defined:
Each token class has methods for generating the output
code, interpreting itself, setting and retrieving it's
various values and states and testing if it's usable in
the parse tree (i.e. if it isn't redundant).
The parse tree is cleaned in the following way :
The parse tree is optimized as follows:
Comments are never added to the parse tree.
Tokens are scanned in a cumulative way and a count is
kept for the tokens, except for do and od.
This results in a much smaller initial parse tree and
reduces the work in the cleaning phases.