# HEAR Compiler in Haskell A compiler for HEAR, a language for when you cannot C. Written in Haskell, and currently using the megaparsec and llvm-hs-\* libraries, but I plan to eventually rewrite the lexar/parser from scratch. Built for the Introduction to Compiler Design class at The University of Texas at Dallas. Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell ## Setup Instructions - Install `ghcup` (for managing Haskell tools) and `nix` (for managing external dependencies). - Clone the repo. - Use `ghcup` to install `stack 2.11.1`, `HLS 2.3.0.0`, and `cabal 3.8.1.0`. ## Run Instructions - Use `stack run ` to run the program (for example, `stack run example/1.hear`). - The LLVM will be generated as `a.out.ll`, and the executable will be generated as `a.out`. ## Currently Supported functionality - Arithmetic - Parenthesis - print() statement ## To edit I recommend using VSCodium, which is preconfigured to have syntax highlighting and (currently broken) debugging features and will automatically suggest the Haskell extensions to install. ## Known bugs ### Building - If llvm_16 is in the nix store, stack will use it for some reason and the build will fail. Currently, you may need to run `nix-store --delete /nix/store/` to build it. ## File structure - `src` - contains the compiler program - `example` - contains example programs that can be compiled ## Credits ### Learning Resources Used - Introduction to Compiler Design class at The University of Texas at Dallas, taught by Charles Averill - https://learnyouahaskell.com (for learning Haskell basics) - https://akashagrawal.me/2017/01/19/beginners-guide-to-megaparsec.html - https://markkarpov.com/tutorial/megaparsec.html (for help writing a Haskell equation parser using megaparsec) - https://www.forth.com/starting-forth/1-forth-stacks-dictionary/ (for learning Forth) - https://blog.josephmorag.com/posts/mcc0/ (Haskell compiler tutorial with megaparsec, llvm-hs, and nix) - https://gh.sudoer.ch/llvm-hs/llvm-hs-examples (for help writing an llvm code generator using llvm-hs) - https://danieljharvey.github.io/posts/2023-02-08-llvm-compiler-part-1.html (for help using llvm-hs-pure) - https://gh.sudoer.ch/danieljharvey/mimsa/blob/trunk/llvm-calc/src/Calc/Compile/ToLLVM.hs (source code for above resource) - https://9to5tutorial.com/homebrew-compiler-made-with-haskell-llvm-configuration (for help using llvm-hs-pure) - https://blog.ocharles.org.uk/blog/posts/2012-12-17-24-days-of-hackage-optparse-applicative.html (for help parsing command line arguments with optparse-applicative) - http://learnyouahaskell.com/making-our-own-types-and-typeclasses (for help defining types) - https://llvm.org/docs/LangRef.html (LLVM documentation) - https://hackage.haskell.org/package/llvm-hs-pure-9.0.0/docs/ (llvm-hs documentation) ### Tools - Language: Haskell - Haskell/management tools: GHCup, Stack, Cabal, GHC 9.2, Nix - Libraries: See `package.yaml` - Dependencies: llvm 15, clang 15 - IDE: VSCodium - Git platform: Forgejo - AI: Phind (GPT-4), ollama (codellama) - Search: Kagi, Stack Overflow