This project is a haskell parser (one-shot lexer) and interpreter for Brainfuck code.
Brainfuck is an esoteric programming language created in 1993 by Swiss student Urban Müller. Designed to be extremely minimalistic, the language consists of only eight simple commands, a data pointer, and an instruction pointer. — Wikipedia: Brainfuck
- Brainfuck is turing complete muppetlabs
- Brainfuck has a very small compiler ( 296 bytes ).
The eight commands in Brainfuck are:
>
- Move the pointer to the right<
- Move the pointer to the left+
- Increment the current cell value-
- Decrement the current cell value.
- Output the value of the current cell as an ASCII character,
- Input a character and store its ASCII value in the current cell[
- Jump to the matching]
if the current cell value is zero]
- Jump to the matching[
if the current cell value is not zero
All other characters are considered comments, which means you can embed a help message in between some math calculations that look pseudo-random in a notepad if you need to, and this interpreter will output the message for you ;)
- Lexes and Parses Brainfuck code in 1 step per command
- Ensures bracket matching, the only Parse-worthy operation
- Interprets Brainfuck programs
- Handles input and output operations
- Supports both file-based and interactive modes
- Includes example Brainfuck programs
To build the project, you need the GHC (Glasgow Haskell Compiler) and Cabal. Then run:
cd brainfuck-interpreter
cabal build
cabal run brainfuck-interpreter examples/hello_world.bf
This will prompt you for input (if needed by the program) and then display the output.
For example:
cabal run brainfuck-interpreter examples/subtract.bf
Should be ran with two, one-digit numbers, like:
32
To have an output of:
1
cabal run brainfuck-interpreter
Argumentless starts the interactive mode, where you can enter Brainfuck programs in one line, and see their output (no memory state visualization). Type 'quit' to exit.
The examples
directory contains several Brainfuck programs:
hello_world.bf
- Prints "Hello World!"hello_world_fixed.bf
- Another version of Hello World (there are also other versions of hello_world)subtract.bf
- Subtracts the second input number from the firstalphabet.bf
- Outputs the representable domain of charactersname.bf
- Outputs a short name
src/Parser.hs
- Parser and one-shot Lexersrc/Interpreter.hs
- Interpretersrc/Main.hs
- The logic and CLI interfaceexamples/
- Example Brainfuck programs
The interpreter has the pointer to the current cell, the program counter and memory state, executing the program.
The memory is implemented as a map from cell indices to cell values, with a pointer to the current cell. This allows for an "infinite" tape in both directions. Cell values wrap around from 255 to 0 and from 0 to 255.
This project is licensed under the MIT License.