Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

PyBrainfuck

Introduction

PyBrainfuck is a speed-optimized Brainfuck interpreter written in Python.

Brainfuck is an esoteric (joke) programming language which is Turing-complete (given enough memory) with only 8 op-codes (instructions). It was designed to allow for the smallest possible compiler.

Some other Python interpreters already exists for Brainfuck, but they are either obfuscated or awfully slow. PyBrainfuck has been optimized for speed by doing various preprocessing on the code such as pre-caching loop instructions, removing non-instructions, etc. PyBrainfuck also has configurable memory size, infinite loop protection and a somewhat spartan debugger.

PyBrainfuck can be used both as a stand-alone Brainfuck interpreter or as a python library. It can read from standard input or from a string (in library mode) and write to standard out or to a string buffer (in library mode).

Features

  • Speed optimized
  • User-definable memory limit
  • Infinite loop protection
  • Debugger
  • Stand-alone use or use as library
  • Brainfuck programs can read from STDIN or string buffer
  • Brainfuck programs can write to STDOUT or string buffer

Installation

Either use it directly from extracted archive or copy the brainfuck directory to a directory in your Python interpreter's path (e.g. /usr/lib/python2.4/site-packages/).

Usage

Standalone mode:

Usage: ./pybrainfuck [option] PROGRAM.bf

Options:
  -h, --help            show this help message and exit
  -m BYTES, --memory=BYTES
                        Number of bytes of memory.
  -i NUMBER, --instructions=NUMBER
                        Number of max instructions to run.
  -d, --debug           Show debugging information.

Example:

    $ ./pybrainfuck tests/helloworld.bf 
    Hello World!

    $ ./pybrainfuck tests/divide.bf 
    62<ENTER>
    3

    $ echo "ROT13 me please." | ./pybrainfuck tests/rot13.bf
    EBG13 zr cyrnfr.

Library mode:

Hello World:
    #!/usr/bin/python

    import brainfuck

    code = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
    bf = brainfuck.Brainfuck(code)
    out = bf.run() 
    print out # Hello, World!


Divide:
    #!/usr/bin/python

    import brainfuck

    code = ",>,>++++++[-<--------<-------->>]<<[>[->+>+<<]>[-<<-[>]>>>[<[>>>-<<<[-]]>>]<<]>>>+<<[-<<+>>]<<<]>[-]>>>>[-<<<<<+>>>>>]<<<<++++++[-<++++++++>]<."
    bf = brainfuck.Brainfuck(code, '62')
    out = bf.run()
    print out # 3

pyBrainfuck is Copyright by Ferry Boender, 2008-2013 - Released under the MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Recent activity

Ferry Boender

Ferry Boender pushed 4 commits to fboender/pybrainfuck

71a20c5 - Initial repository structure.
f6efab5 - Initial import.
fb94b84 - Improved the error handling: BrainfuckError now has an error code (errnr). A bug was fixed in which the jump pre-processor would scan beyond the end-of-string ...
a8059c4 - Minor changes.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.