A simple and efficient Brainfuck interpreter implemented in Rust. Brainfuck is a minimalist programming language with a small set of commands, designed to challenge and amuse programmers.
- Supports all Brainfuck commands:
>
,<
,+
,-
,.
,,
,[
, and]
. - Can execute Brainfuck programs from a file.
- Optionally outputs debug information.
Command | Description |
---|---|
> |
Move the pointer to the next cell |
< |
Move the pointer to the previous cell |
+ |
Increase the value at the current cell by 1 |
- |
Decrease the value at the current cell by 1 |
. |
Output the ASCII representation of the current cell |
, |
Get a single byte of input from the user |
[ |
Start a loop; execute if the current cell is non-zero |
] |
End a loop; jump back if the current cell is non-zero |
To build the interpreter, ensure you have Rust installed. Clone this repository and run:
cargo build --release
Provide the filename of the Brainfuck program as an argument:
./brainfuck-interpreter <filename.bf>
To see the raw output as a vector of bytes, use the --debug flag:
./brainfuck-interpreter <filename.bf> --debug
Given a file hello.bf containing:
print 0 to 99
++++++++++
>++++++++++++++++++++++++++++++++++++++++++++++++
>++++++++++
[
>++++++++++++++++++++++++++++++++++++++++++++++++
>++++++++++
[
<<<. >>.+ <<<.
>>>>-
]
<----------------------------------------------------------
<<+
>
-
]
Run the program:
./brainfuck-interpreter hello.bf
# Expected output:
# 00
# 01
# ..
# 99
The interpreter validates input files and checks for invalid commands. If the program encounters unmatched loops ([ and ]), it may produce unintended behavior.
This project is open-source and available under the MIT License.
Feel free to open issues or submit pull requests for improvements and new features.
Happy Brainfuck coding hehehe 🤣!