This compiler is based on the book Implementing Functional Languages: A Tutorial by Simon Peyton Jones and David Lester.
To download:
stack install core-compiler
To run the example:
stack build
stack exec core-compiler-exe [file]
OR
stack exec core-compiler-exe run-steps [file]
The option run-steps
will print out each step the G-Machine makes in evaluating the program, to help the user learn how it works. There are example Core programs in the SamplePrograms
folder
As the project currently stands, anyone who wants to create a very simple functional language can do so by parsing it into the Core Expression AST found in this project.
The Core language is a simple functional language that other functional languages (such as Haskell) can be compiled to. In the book, as well as in this implementation, Core is compiled to G-Code (which can be further compiled to C or machine code) or later interpreted by the G-Machine. The grammar of Core in this implementation is ever so slightly different than what is in the book (for readability), but this change had no affects on the compiler itself.
This implementation leaves a lot to be desired, but was successful as an introduction to the world of compilers as well as state transition and stack machines. I believe that it was a great next step following the Write Yourself a Scheme tutorial.
The project itself is broken into the following series of steps:
- Lexing (using Alex)
- Parsing (using Happy)
- Compilation to initial G-Code
- Evaluation by the G-Machine
- implement lambda expressions and lambda lifting
- possibly implement GmState as a State monad
- consider Reader for the environment during evalutation in the G machine
- fix odd syntax such as double semi-colons
- better error handling
- replace
String
withText
I encourage contribution in the form of pull requests. The todo list is a good place to start; the lambda lifting for example is a well documented addition in the book mentioned above, and the error handling would be relatively simple.