项目作者: cptx032

项目描述 :
A brainfuck interpreter
高级语言: Python
项目地址: git://github.com/cptx032/bfi.git
创建时间: 2016-04-26T16:08:41Z
项目社区:https://github.com/cptx032/bfi

开源协议:

下载


bfi

A brainfuck interpreter
BFI - Brain Fuck Interpreter is a simple, but powerful, Python brainfuck interpreter. With him you can go forward step by step in the source code making it a very embeddable library. See a very simple example:

  1. import bfi
  2. interpreter = bfi.BFI("++[-].")
  3. while not interpreter.EOF:
  4. interpreter.next()

With BFI you can redirect the input and the output to embed in your software:

  1. import bfi
  2. from StringIO import StringIO
  3. output_buffer = StringIO()
  4. input_buffer = StringIO()
  5. # filling the input
  6. input_buffer.write('\x00\x01\x00\x01')
  7. interpreter = bfi.BFI(",>,>,>,++[-].", output=output_buffer, input=input_buffer)
  8. while not interpreter.EOF:
  9. interpreter.next()
  10. print "output:", output_buffer.getvalue()

The BFI allows make cells negative:

  1. import bfi
  2. interpreter = bfi.BFI("[-]-----", allow_negative=True)
  3. while not interpreter.EOF:
  4. interpreter.next() # run ok
  5. interpreter = bfi.BFI("[-]-----", allow_negative=False)
  6. while not interpreter.EOF:
  7. interpreter.next() # raises a bfi.NegativeNumberError

BFI allows loop the stack:

  1. import bfi
  2. interpreter = bfi.BFI("<", allow_turn_stack=True)
  3. while not interpreter.EOF:
  4. interpreter.next() # run ok
  5. interpreter = bfi.BFI("<", allow_turn_stack=False)
  6. while not interpreter.EOF:
  7. interpreter.next() # raises a bfi.StackIndexError

BFI has manysome other options like stack expanding, pre filling stack etc.