A brainfuck compiler written in Haskell
- Languages: Haskell
- Packages: Parsec
I wrote a blog post about this project
You need to have cabal, Haskell installed. Then run the following commands To run the program you need gcc for the C version and SPIM for the MIPS version
# clone the repo and move to it
git clone https://github.com/tttardigrado/hsfuck
cd hsfuck
# build the project using cabal
cabal build
# optionally move the binary into another location with
# cp ./path/to/binary .
# run the compiler
# (fst argument is compilation target mode. Either c or mips)
# (snd argument is the path of the src file)
# (trd argument is the path of the output file)
./hsfuck c test.bf test.c
# compile and run the C code
gcc test.c
./a.out
Suggestion: Add the following snippets to your .bashrc
# compile brainfuck to c and then to binary
bfC()
{
./hsfuck c $1 /tmp/ccode.c
gcc /tmp/ccode.c -o $2
}
# simulate as MIPS (using SPIM)
bfMIPS()
{
./hsfuck mips $1 /tmp/mipscode.mips
spim -file /tmp/mipscode.mips
}
+
increment the value of the current cell-
decrement the value of the current cell»
right shift the value of the current cell«
left shift the value of the current cell>
move the tape one cell to the right<
move the tape one cell to the left.
print the value of the current cell as ASCII,
read the value of an ASCII character from stdin to the current cell:
print the value of the current cell as an integer;
read an integer from stdin to the current cell[c]
executec
while the value of the cell is not zero#
print debug information
- Brainfuck
- Optimizations
-
0
set the cell to 0 -
»
and«
-> right and left shifts - Add more print and read options (integer)
- remove register
- compile to MIPS
- Add debug to MIPS target
- Test MIPS and C output
- Add compilation target flag
- Add commands documentation
- Add references