This repository contains Brainfuck engines written in:
- C++
- Zig
The implementations explore different execution strategies for running Brainfuck programs.
- The C++ engine supports multiple execution modes consisting of a character interpreter and an IR-based virtual machine.
- The Zig engine is a minimal direct interpreter.
The project is designed for learning and experimentation with interpreter design, intermediate representations, and virtual machine runtimes.
The C++ implementation provides a multi-engine Brainfuck runtime.
Features:
- Fixed-size memory tape (
32768cells) - Supports all 8 Brainfuck instructions
- Detects unmatched brackets
- Prevents pointer underflow
- Runtime engine selection
- Optional REPL mode
- IR instruction collapsing optimization
Execution engines:
- Character Interpreter — executes Brainfuck instructions directly
- IR + Virtual Machine — compiles Brainfuck into an intermediate representation before execution
The Zig implementation provides a multi-engine Brainfuck runtime.
Features:
- Fixed-size memory model
- Supports all standard Brainfuck instructions
- Direct interpreter execution
- IR-based virtual machine execution
- Builds with
zig build
Execution engines:
- Direct Interpreter — executes Brainfuck instructions directly
- IR + Virtual Machine — compiles Brainfuck into an intermediate representation before execution
cd engines/cpp
make
./brainfuck hello.bfcd engines/zig
zig build
./zig-out/bin/brainfuck hello.bfAll engines run by passing a .bf file as a command-line argument. Example:
./brainfuck program.bf
./zig-out/bin/brainfuck program.bfThe C++ engine also supports selecting the execution mode:
./brainfuck program.bf --mode=char
./brainfuck program.bf --mode=ir++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.Expected output:
Hello World!
,.Type a character, and it will be printed back.
- CPU: Intel(R) Core(TM) i5 M 460 @ 2.53GHz
- Cores / Threads: 2 cores / 4 threads
- RAM: 4 GB
- System: Dell Inspiron N5010
- OS: Windows 10 Home (Build 19045, x64)
- Zig: ReleaseFast
- C++: -O3
For Mandel Brot code
| Run | C++ Interpreter (ms) | Zig Interpreter (ms) | C++ VM (ms) | Zig VM (ms) |
|---|---|---|---|---|
| 1 | 994222 | 90883 | 50028 | 43827 |
| 2 | 101755 | 92457 | 42753 | 37110 |
| 3 | 100700 | 91428 | 36374 | 38758 |
| 4 | 112873 | 92705 | 33157 | 39720 |
| 5 | 106427 | 92208 | 31967 | 29985 |
| Average | 104235 | 91936.2 | 38855.8 | 37880 |
- Runs Brainfuck
.bor.bfprograms - Supports
>, <, +, -, ., ,, [, ] - Multiple execution engines (C++)
- IR-based virtual machine (C++)
- Instruction collapsing optimizations (C++)
- Fixed-size memory model
- Bracket validation
- Pointer safety checks
- Optional REPL mode in both Zig and C++
- No memory expansion (C++ & Zig)
- Only basic IR optimizations implemented
- No debugging mode or step execution
- Input is character-based only
- Console encoding may vary by platform
brainfuck
├── engines/
│ ├── cpp/
│ │ ├── src/ # C++ interpreter sources
│ │ └── Makefile
│ └── zig/
│ ├── src/ # Zig interpreter sources
│ ├── build.zig
│ └── build.zig.zon
│
├── examples/
│
├── LICENSE
└── README.md
Contributing guidelines will be added soon.
Made with ❤️ by JourneyCodesAyush