A Brainfuck interpreter written in Haskell.
bf-hs is a simple and efficient Brainfuck interpreter that can execute Brainfuck programs from files or standard input. Brainfuck is an esoteric programming language with only eight commands, making it a fun challenge to implement and an interesting way to explore language interpreters.
- Complete Brainfuck language support with all 8 commands
- 30,000-cell memory tape (standard Brainfuck specification)
- Wraparound pointer arithmetic for safe memory access
- Interactive input/output support
- Read programs from files or standard input
- Clean, readable Haskell implementation
| Command | Description |
|---|---|
> |
Move the data pointer to the right |
< |
Move the data pointer to the left |
+ |
Increment the byte at the data pointer |
- |
Decrement the byte at the data pointer |
. |
Output the byte at the data pointer as an ASCII character |
, |
Input a byte and store it at the data pointer |
[ |
Jump forward to the command after the matching ] if the byte at the data pointer is zero |
] |
Jump back to the command after the matching [ if the byte at the data pointer is nonzero |
-
Clone the repository:
git clone https://github.com/grapefizz/bf-hs.git cd bf-hs -
Build the project:
cabal build
-
Install the executable:
cabal install
bf-hs program.bfecho ">++++++++[<+++++++++>-]<." | bf-hsor
bf-hs < program.bfThe repository includes a sample "Hello World" program in hello.bf:
bf-hs hello.bfThis should output: Hello World!
>++++++++[<+++++++++>-]<.
>++++[<+++++++>-]<+.
+++++++..
+++.
>>++++++[<+++++++>-]<++.
------------.
>++++++[<+++++++++>-]<+.
<.
+++.
------.
--------.
>>>++++[<++++++++>-]<+.,>,<[->+<]>.This program reads two single-digit numbers and outputs their sum.
- Memory Model: Uses a 30,000-cell tape implemented with Haskell's
IOUArray - Pointer Wrapping: Implements wraparound arithmetic to prevent segmentation faults
- Loop Matching: Efficient bracket matching algorithm for
[and]commands - I/O Handling: Immediate character output with buffer flushing for responsive interaction
bf-hs/
├── app/
│ └── Main.hs # Main interpreter implementation
├── bf-hs.cabal # Cabal package configuration
├── hello.bf # Example Brainfuck program
├── LICENSE # MIT License
├── CHANGELOG.md # Version history
└── README.md # This file
base: Core Haskell libraryarray: For efficient mutable arrays
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Inspired by the original Brainfuck language created by Urban Müller
- Built with the excellent Haskell ecosystem