Skip to content

Fastest(?) Optimised BF interpreter in Go

License

Notifications You must be signed in to change notification settings

tristanmorgan/bfg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

B.F.G.

  • license MIT
  • GoReportCard
  • Go

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.

Optimisations

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

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
+++++++++[>++++++++>++++++++++++>++++>++++++++++++>+++++++++++>+<<<<<<-]>---.>++
.----.+++++.++++++++++.>----.<-----.>>----.+.<<-.>.<<---.>-.>>>--.<.+++++.<<<+++
+.>+++.>>>++.<---.<+.>>>+.