Brainfuck-ng


 * The title of this article may well be brainfuck-ng, due to typically being lowercased except, often, at the start of a sentence.

Brainfuck-ng is a powerful interpreter of a superset of brainfuck language. It is full compatible with brainfuck, and has in addition some enhancements.

Language overview
Brainfuck-ng operates on an array of memory cells, also referred to as the tape, each initially set to zero. There is a pointer, initially pointing to the first memory cell. The commands are:

All characters other than  should be considered comments and ignored.

If no input environment array is given, the environment array is initially an array of a fixed dimension with all cells septed to zero. The initial dimension depends on interpreter implementation.

Return value
Brainfuck-ng always return the value of the cell pointed by the last instruction executed.

repositories

 * brainfuck-ng on Github
 * brainfuck-ng on Bitbucket

usage
brainfuck-ng v0.1 USAGE: bfng [options] [file] valid options: -i             --shell         interactive (shell mode) -h             --help          show this help -v             --version       prints brainfuck-ng version number -s             --stdin         read bf prog from stdin -d 'datum'     --data          set environment-array -o             --out           print env-array to stdout after computation -p             --print         same as -o but with newline added. -r             --reversefuck   switch 'ReverseFuck' mode on. -m size        --size          set the size (in bytes) of environment array (default is 32K). -x 'prog'      --exec          gets & execute bf prog -c             --cin           gets environment-array from stdin -b file        --xfile         gets bf prog from file -a file        --dfile         gets environment-array from file

interpreter's interactive commands
in shell interactive mode you can give  brainfuck-ng  interpreter  the following command:

:h    print help summary :ks (yes|no) :ks print  the  keep  state variable. :ks yes or :ks no set the keep state variable. The keep state  variable  can  retain  the value of the environment-array between line input. :p    print the environment-array to stdout (added with \n endline). :r (yes|no) enable/disable/show reverse-fuck mode. :set datum set the environment-array to datum (and set :ks yes) :z    print the last return value to stdout

ReverseFuck mode
In the 'ReverseFuck' mode all commands are switched to their semantic opposite. Here is an overview:

Instructions: -     Increments the current cell (under the pointer). +     Decrements the current cell (under the pointer). >     Decrements pointer. <     Increments pointer. .     Inputs a character to current cell. ,     Outputs a character from current cell. ]     While the current cell value is not zero, do stuff inside ] and [ braces [     End while. !     input integer at current cell. ?     output integer from current cell. @     exit program returning current cel value( the only command not changed).

You can switch to ReverseFuck mode by adding the -r or --reversefuck option to the bfng interpreter.

Hello, World!
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.--..>>+.>++.

This is a slightly more complex variant that often triggers interpreter bugs

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

bfng - The brainfuck-ng interpreter
% echo "+++"|bfng -s % echo $? 3          #(the putput)
 * get the return value:

% echo "foobar"|bfng -c -x'+' -o goobar      #(the output) or % echo "+"|bfng -d'foobar' -p goobar       #(the output)
 * give an environment and get the result:

% bfng -b program.bf -a env.data
 * give an environment and a program from files:

% bfng HelloWorld.bf
 * simply execute a program

% bfng -x '?+++!' 35               #(user keyboard input) 38               #(program output) % echo $? 38               #(output)
 * get input and give output from bf program:

% bfng -i
 * enter interactive shell mode:

External resources

 * brainfuck-ng on Github
 * brainfuck-ng on Bitbucket