A basic project setup in Haskell. Namely used for Comp 520 - Compiler Design
To use this project, install stack
and execute stack run
.
Use IntelliJ along with the Haskell plugin. Note the readme if you want to install the newer beta versions.
An alternative is Visual Studio Code along with the Haskell Language Server. It requires installing the IDE Engine, which as of now involves building it yourself. This didn't work too well for me.
This sample uses megaparsec.
And showcases the example from the simple imperative language tutorial.
To summarize, defining the rules and types allows you to produce outputs like so:
-- Sample compiler
--------------------------------------------------
if 5 then x := 5 else x := 5
1:6:
|
1 | if 5 then x := 5 else x := 5
| ^
unexpected 't'
expecting '*', '+', '-', '/', '<', or '>'
--------------------------------------------------
--------------------------------------------------
if true then x := 5
--------------------------------------------------
1:20:
|
1 | if true then x := 5
| ^
unexpected end of input
expecting "else", '*', '+', '-', '/', ';', or digit
--------------------------------------------------
--------------------------------------------------
if true then x := 5 else x := 5
--------------------------------------------------
If (BoolConst True) (Assign "x" (IntConst 5)) (Assign "x" (IntConst 5))