diff --git a/README.md b/README.md index a62097b..3f59fd2 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,13 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a - 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) ### Tools - Language: Haskell -- Haskell tools: GHCup, Stack, Cabal, GHC 9.2 -- Libraries: megaparsec, parser-combinators, text, process, llvm-hs 15, llvm-hs-pure 15, +- Haskell/management tools: GHCup, Stack, Cabal, GHC 9.2, Nix +- Libraries: See `package.yaml` - Dependencies: llvm 15, clang 15 - IDE: VSCodium - Git platform: Forgejo diff --git a/package.yaml b/package.yaml index 72cd6c8..5e889c5 100644 --- a/package.yaml +++ b/package.yaml @@ -14,6 +14,7 @@ dependencies: - bytestring - string-conversions - transformers + - optparse-applicative >= 0.17 && < 1 tested-with: GHC == 9.2.8 category: Compilers/Interpreters diff --git a/really-bad-compiler-in-haskell.cabal b/really-bad-compiler-in-haskell.cabal index 7e52a66..0b1bace 100644 --- a/really-bad-compiler-in-haskell.cabal +++ b/really-bad-compiler-in-haskell.cabal @@ -33,6 +33,7 @@ executable really-bad-compiler-in-haskell , llvm-hs-pure ==15.* , megaparsec >=9.0.1 && <10 , mtl + , optparse-applicative >=0.17 && <1 , parser-combinators , process , string-conversions diff --git a/src/Main.hs b/src/Main.hs index c8b387f..dee11e3 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -7,16 +7,41 @@ import Data.ByteString.Char8 qualified as B import Data.Text.IO qualified as T import Main.LLVMGen import Main.Parser.Megaparsec +import Options.Applicative import System.Environment import System.Process (callCommand) -main :: IO () -main = do - fileName <- head <$> getArgs +data Opt = Opt + { filePath :: String, + showLLVM :: Bool, + showDebug :: Bool + } + +run :: Opt -> IO () +run opts = do + let fileName = filePath opts contents <- T.readFile fileName T.putStrLn "- Generating LLVM to './a.out.ll'..." result <- (llvmGen . parse) contents B.writeFile "a.out.ll" result T.putStrLn "- Compiling to executable './a.out'..." callCommand "clang a.out.ll" - T.putStrLn "- Done." \ No newline at end of file + T.putStrLn "- Done." + +main :: IO () +main = execParser opts >>= run + where + parser = + Opt + <$> argument str (metavar "FILE_PATH") + <*> switch + ( short 'l' + <> long "showLLVM" + <> help "Create .ll with LLVM used to compile the binary" + ) + <*> switch + ( short 'd' + <> long "showDebug" + <> help "Show debug output" + ) + opts = info parser mempty