Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I used a similar technique about five years ago (!) when I wrote a toy compiler for a university class in Haskell. Here's the relevant bits:

* Expression evaluation: https://github.com/michaelmelanson/cminus-compiler/blob/mast...

* Statement compilation: https://github.com/michaelmelanson/cminus-compiler/blob/mast...

* Definition of the monad: https://github.com/michaelmelanson/cminus-compiler/blob/mast...

The 'compilation state' was wrapped up in a State monad which held a list of emitted instruction blocks, free registers, and so on. I didn't go as far as to wrap each of the instructions in their own functions to get an assembly-like DSL, but you can imagine defining functions like `add = emit $ ADD` for each instruction to get the same effect.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: