Skip to main content

High-performance Brainfuck interpreter with JIT acceleration, library imports, and interactive REPL

Project description

advanced-brainfuck

advanced-brainfuck

A high-performance Brainfuck language interpreter for Python with JIT acceleration, library imports, and an interactive REPL.


Features

  • JIT-accelerated execution — Compiles Brainfuck programs to an intermediate representation (IR) with run-length encoding, then executes via Numba's @jit(nopython=True) for 3-5x speedup
  • Segmented JIT with checkpoints — All programs use the JIT path; I/O operations (,, *, &) trigger checkpoints where Python handles input/output before resuming JIT execution
  • Interpreted fallback — If JIT compilation fails, falls back to interpreted IR execution with full compatibility
  • Library system — Import external .bf files with {libname} syntax; recursive imports supported
  • Interactive REPL — Run brainfuck with no arguments for an interactive shell
  • Python APIfrom brainfuck import BrainFuck for programmatic use

Quick Start

Installation

pip install advanced-brainfuck

Command Line

# Run a program and exit
brainfuck -c '++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'

# Run a program then enter interactive REPL
brainfuck '+++++++++++++++++++++++++++++++++++++++++++++++++++.'

# Enter interactive REPL
brainfuck

Python API

from brainfuck import BrainFuck

bf = BrainFuck()
bf.execute('+++++++++++++++++++++++++++++++++++++++++++++++++++.')  # prints: 3
bf.execute('[-]')                    # clears cell 0
bf.execute('{p10}*{tochar}')         # imports and prints: |10|
bf.execute('&')                       # prints command history
bf.interpreter()                      # starts interactive REPL

Brainfuck Commands

Standard Commands

Command Description
> Increment the data pointer
< Decrement the data pointer
+ Increment the value at the data pointer
- Decrement the value at the data pointer
. Output the value at the data pointer
, Accept one integer of input
[ Jump forward past matching ] if value is zero
] Jump back to matching [ if value is non-zero

Additional Commands

Command Description
{LIB} Import external Brainfuck code from bflib/LIB.bf
* Output all cells with pointer highlighted
& Output command history
help Show command reference (REPL only)

Library Modules

The bflib/ directory contains reusable Brainfuck modules:

Module Description
sum Sum current cell and next cell
sub Subtract next cell from current cell
mul Multiply current cell by next cell
mul2 Multiply current cell by two
copy Copy current cell into next cell
p10, p50, p100 Add 10, 50, 100 to current cell
m10, m50, m100 Subtract 10, 50, 100 from current cell
toint Convert ASCII digit character to integer
tochar Convert integer (0-9) to ASCII character
lower Convert uppercase ASCII to lowercase
upper Convert lowercase ASCII to uppercase

Architecture

Source Code → Import Resolution → IR Compilation → Execution
                                                        ↓
                                               ┌── Segmented JIT ──┐
                                               │ execute_jit()      │
                                               │ via Numba          │
                                               │                    │
                                               │ Checkpoints at:    │
                                               │  , * &             │
                                               │                    │
                                               │ Python handles I/O │
                                               │ then resumes JIT   │
                                               └────────────────────┘
                                               ┌── Fallback ────────┐
                                               │ (JIT compilation   │
                                               │  failure only)     │
                                               │ Python IR loop     │
                                               └────────────────────┘

IR Compilation

Source code is compiled to an intermediate representation with:

  • Run-length encoding+++++ becomes ('add', 5) in a single operation
  • Pre-resolved jumps[ and ] targets are computed at compile time
  • Numeric encoding — IR tuples converted to NumPy int32 arrays for JIT

Tape Memory

Cells use a pre-allocated NumPy tape of 65,536 cells (64K) for JIT execution, with a hybrid Python storage model for the REPL:

  • Pre-allocated list of 30,000 integers for indices 0-29,999 (O(1) access)
  • Sparse defaultdict for negative indices (rarely used but supported)

Requirements

  • Python >= 3.13
  • Numba >= 0.58.0
  • NumPy >= 1.24.0

Project Structure

advanced-brainfuck/
├── brainfuck.py           # Core interpreter, compiler, JIT, CLI
├── bflib/                 # Reusable Brainfuck library modules
│   ├── sum.bf
│   ├── copy.bf
│   ├── mul.bf
│   ├── toint.bf
│   └── ...
├── docs/
│   ├── spec/              # Product & technical specifications
│   ├── adr/               # Architecture decision records
│   └── features/           # Feature specifications
├── tests/
│   └── features/           # Test files
├── pyproject.toml          # Package configuration
└── README.md

License

GPLv3 — See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

advanced_brainfuck-2.0.0.tar.gz (42.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

advanced_brainfuck-2.0.0-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file advanced_brainfuck-2.0.0.tar.gz.

File metadata

  • Download URL: advanced_brainfuck-2.0.0.tar.gz
  • Upload date:
  • Size: 42.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for advanced_brainfuck-2.0.0.tar.gz
Algorithm Hash digest
SHA256 627a3e0da265ab8fdb67be09b9fe571335ac66a9c4c4b1390f9da7280e1c7665
MD5 a09f4b4821b1d2d277ecbb40d6a48929
BLAKE2b-256 5021000da39c95a6dc88205a6aa36648f6066763774af8dc2a7a2d906b90075d

See more details on using hashes here.

Provenance

The following attestation bundles were made for advanced_brainfuck-2.0.0.tar.gz:

Publisher: pypi-publish.yml on nullhack/advanced-brainfuck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file advanced_brainfuck-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for advanced_brainfuck-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 362f5316d63bf5cc0d20ccad483a32fcc5a9b497dbc9b53563a39b933714b781
MD5 b9c0ac7312f19227d73a01af1c44f8b0
BLAKE2b-256 e2ed1778124b4eec477f9a7ab4afeca43085ec2d4ee8c0e0013f1c2d75757b08

See more details on using hashes here.

Provenance

The following attestation bundles were made for advanced_brainfuck-2.0.0-py3-none-any.whl:

Publisher: pypi-publish.yml on nullhack/advanced-brainfuck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page