A Brainfuck interpreter written in C++. Hobby project — C++ isn't my main language, just messing around.
Brainfuck is an esoteric programming language created by Urban Müller in 1993. The entire language has only 8 instructions, yet it's Turing-complete — meaning anything computable can theoretically be written in it. In practice, even simple programs like Hello World require dozens of characters and a lot of mental arithmetic to write by hand.
The point isn't utility. It's a thought experiment: what's the absolute minimum a programming language needs to be "real"?
g++ -std=c++17 -O2 -Wall -o bfi main.cpp./bfi <program.bf>$ ./bfi programs/hello.bf
Hello World!
$ echo "hello" | ./bfi programs/cat.bf
hello
$ ./bfi programs/count.bf
0123456789| File | Description |
|---|---|
programs/hello.bf |
Classic Hello World — exercises nested loops |
programs/cat.bf |
Echo stdin to stdout — the one-liner ,[.,] |
programs/count.bf |
Print digits 0–9 — shows arithmetic via loops |
BF's memory model is a 30,000-cell array of bytes called the tape. Two pointers drive execution:
- data pointer — which cell on the tape we're reading/writing
- instruction pointer — which character in the source we're executing
The 8 instructions:
| Instruction | Effect |
|---|---|
> / < |
Move the data pointer right / left |
+ / - |
Increment / decrement the current cell (wraps 0–255) |
. |
Output the current cell as an ASCII character |
, |
Read one byte of input into the current cell |
[ |
Jump past the matching ] if the current cell is 0 |
] |
Jump back to the matching [ if the current cell is nonzero |
Any character that isn't one of the eight above is silently ignored — which means you can freely add comments inline with the code.
Bracket pairs are resolved into a jump table before execution starts, so [ / ] jumps are O(1) rather than scanning the source each time.
cat.bf is just three characters: ,[.,]
, read one byte of input into cell[0]
[ while cell[0] != 0:
. print cell[0]
, read next byte into cell[0]
] end loop (exits when input is exhausted / EOF sets cell to 0)
count.bf shows how BF does arithmetic — there are no number literals, so you build values up with loops:
++++++++ cell[0] = 8
[ repeat 8 times:
>++++++ cell[1] += 6
<- cell[0] -= 1
] result: cell[1] = 48 (ASCII '0'), cell[0] = 0
> move to cell[1]
.+.+.+.+.+.+.+.+.+. print 48..57 ('0'..'9')
>++++++++++. cell[2] = 10 (newline), print it