2 ms·
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 evalu
by a-priori 12y ago
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/master/Compiler/CodeGeneration/ExpressionEvaluation.hs https://github.com/michaelmelanson/cminus-compiler/blob/mast...
* Statement compilation: https://github.com/michaelmelanson/cminus-compiler/blob/master/Compiler/CodeGeneration/StatementCompilation.hs https://github.com/michaelmelanson/cminus-compiler/blob/mast...
* Definition of the monad: https://github.com/michaelmelanson/cminus-compiler/blob/master/Compiler/CodeGeneration/CompilationState.hs 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.