Skip to content

tttardigrado/hsfuck

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hsfuck

Tests CI Build CI License

Logo

A brainfuck compiler written in Haskell

Tech stack

  • Languages: Haskell
  • Packages: Parsec

Blog Post

I wrote a blog post about this project

How to install and use

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
}

Commands

  • + 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] execute c while the value of the cell is not zero
  • # print debug information

References

TO DO:

  • 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