bfi package

Module contents

exception bfi.BrainfuckSyntaxError

Bases: exceptions.Exception

Raised when brainfuck source contains invalid syntax

class bfi.Opcode(code, move=0, value=None)

Bases: object

Brainfuck intermediate representation opcode

bfi.execute(opcodes, input_data=None, time_limit=None, tape_size=30000, buffer_output=False, write_byte=None, read_byte=None)

Execute a list of intermediate opcodes

Parameters:
  • opcodes ([Opcode]) – opcodes to execute
  • input_data (str) – input data
  • time_limit (float) – execution time limit
  • tape_size (int) – Brainfuck program tape size
  • buffer_output (bool) – if True, any output generated by the Brainfuck program will be buffered and returned as a string
  • write_byte (callable) – callback to implement custom output behaviour; whenever the ‘.’ brainfuck opcode is used to output the contents of the current cell, the contents of the current cell will be passed to this function. Should accept one argument which is the byte to write as an integer, and return nothing. Overrides the ‘buffer_output’ argument.
  • read_byte (callable) – callback to implement custom input behaviour; whenever the ‘,’ brainfuck opcode is used to read input and put it into the current cell, this function will be called to obtain 1 byte of input. Should accept no arguments, and return the read byte as an integer. Overrides the ‘input_data’ argument.
bfi.interpret(program, input_data=None, time_limit=None, tape_size=30000, buffer_output=False, write_byte=None, read_byte=None)

Interpret & execute a brainfuck program

Parameters:
  • program (str) – Brainfuck source code
  • input_data (str) – input data
  • time_limit (float) – execution time limit
  • tape_size (int) – Brainfuck program tape size
  • buffer_output (bool) – if True, any output generated by the Brainfuck program will be buffered and returned as a string
  • write_byte (callable) – callback to implement custom output behaviour; whenever the ‘.’ brainfuck opcode is used to output the contents of the current cell, the contents of the current cell will be passed to this function. Should accept one argument which is the byte to write as an integer, and return nothing. Overrides the ‘buffer_output’ argument.
  • read_byte (callable) – callback to implement custom input behaviour; whenever the ‘,’ brainfuck opcode is used to read input and put it into the current cell, this function will be called to obtain 1 byte of input. Should accept no arguments, and return the read byte as an integer. Overrides the ‘input_data’ argument.
bfi.parse(program)

Convert brainfuck source into some intermediate opcodes that take advantage of common brainfuck paradigms to execute more efficiently.

Specifically:

  • Strip out whitespace and any other non-BF characters
  • Replace copy loops, multiply loops, clear loops and scan loops with a single opcode that acheives the same effect
  • Collapse sequences of repeated “+”, “-”, “>” and “<” characters into a single opcode
Parameters:program (str) – Brainfuck source code
Returns:list of intermediate opcodes
Return type:[bfi.Opcode]