BFG is an optimised Brainfuck interpreter written in Go.
Uses signed ints for data (platform specific 32/64), memory wraps around at 65535, EOF returns -1.
Optionally uses 8 bit data cells.
Buffered output flushes on newline, 200 chars or input.
Operates with a instruction parse then execute pattern.
- loop start/end are calculated up front.
- repeat ++++ or --- are replaced with a single addition/subtraction
- [-] is converted to a blind set 0 command
- addition/subtraction after zero does a blind set.
- repeat >>> or <<< are replaced with a single pointer jump
- [>>>] is merged into a skip instruction.
- [>>+<<-] is merged into a move instruction.
- [<+++++>-] is converted to a multiply instruction.
- [<<+>+>-] is converted to a duplicate instruction.
- [<+++++>->+++++<] is converted to a vector multiply instruction.
- and dead code removal.
for performance comparison see no_optimisation branch.
Usage:
bfg [option] source.bf [input]
Options:
-e, --eight eight bit execution
-p, --print pretty print parsed program
-v, --version display version
May use - as source to read program from STDIN and output is STDOUT
#!/usr/bin/env bf
+++++++++[>++++++++>++++++++++++>++++>++++++++++++>+++++++++++>+<<<<<<-]>---.>++
.----.+++++.++++++++++.>----.<-----.>>----.+.<<-.>.<<---.>-.>>>--.<.+++++.<<<+++
+.>+++.>>>++.<---.<+.>>>+.