bf-hs
a brainfuck interpreter written in haskell.
bf-hs
A Brainfuck interpreter written in Haskell.
overview
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.
features
- 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
brainfuck commands
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 |
installation
prerequisites
building from source
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
usage
running a brainfuck program from a file
bf-hs program.bf
running a brainfuck program from standard input
echo ">++++++++[<+++++++++>-]<." | bf-hs
or
bf-hs < program.bf
example
The repository includes a sample “Hello World” program in hello.bf
:
bf-hs hello.bf
This should output: Hello World!
example programs
Hello World (hello.bf
)
>++++++++[<+++++++++>-]<.
>++++[<+++++++>-]<+.
+++++++..
+++.
>>++++++[<+++++++>-]<++.
------------.
>++++++[<+++++++++>-]<+.
<.
+++.
------.
--------.
>>>++++[<++++++++>-]<+.
simple addition
,>,<[->+<]>.
This program reads two single-digit numbers and outputs their sum.
implementation details
- 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
development
project structure
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
dependencies
base
: Core Haskell libraryarray
: For efficient mutable arrays
contributing
Contributions are welcome! Please feel free to submit a Pull Request.
acknowledgments
- Inspired by the original Brainfuck language created by Urban Müller
- Built with the excellent Haskell ecosystem