Add command line argument parsing

pull/25/head
Ethan Reece 2023-10-06 23:55:57 -05:00
parent b52fe34667
commit bdbe823bc3
Signed by: me
GPG Key ID: D3993665FF92E1C3
4 changed files with 34 additions and 6 deletions

View File

@ -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://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://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://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 ### Tools
- Language: Haskell - Language: Haskell
- Haskell tools: GHCup, Stack, Cabal, GHC 9.2 - Haskell/management tools: GHCup, Stack, Cabal, GHC 9.2, Nix
- Libraries: megaparsec, parser-combinators, text, process, llvm-hs 15, llvm-hs-pure 15, - Libraries: See `package.yaml`
- Dependencies: llvm 15, clang 15 - Dependencies: llvm 15, clang 15
- IDE: VSCodium - IDE: VSCodium
- Git platform: Forgejo - Git platform: Forgejo

View File

@ -14,6 +14,7 @@ dependencies:
- bytestring - bytestring
- string-conversions - string-conversions
- transformers - transformers
- optparse-applicative >= 0.17 && < 1
tested-with: GHC == 9.2.8 tested-with: GHC == 9.2.8
category: Compilers/Interpreters category: Compilers/Interpreters

View File

@ -33,6 +33,7 @@ executable really-bad-compiler-in-haskell
, llvm-hs-pure ==15.* , llvm-hs-pure ==15.*
, megaparsec >=9.0.1 && <10 , megaparsec >=9.0.1 && <10
, mtl , mtl
, optparse-applicative >=0.17 && <1
, parser-combinators , parser-combinators
, process , process
, string-conversions , string-conversions

View File

@ -7,16 +7,41 @@ import Data.ByteString.Char8 qualified as B
import Data.Text.IO qualified as T import Data.Text.IO qualified as T
import Main.LLVMGen import Main.LLVMGen
import Main.Parser.Megaparsec import Main.Parser.Megaparsec
import Options.Applicative
import System.Environment import System.Environment
import System.Process (callCommand) import System.Process (callCommand)
main :: IO () data Opt = Opt
main = do { filePath :: String,
fileName <- head <$> getArgs showLLVM :: Bool,
showDebug :: Bool
}
run :: Opt -> IO ()
run opts = do
let fileName = filePath opts
contents <- T.readFile fileName contents <- T.readFile fileName
T.putStrLn "- Generating LLVM to './a.out.ll'..." T.putStrLn "- Generating LLVM to './a.out.ll'..."
result <- (llvmGen . parse) contents result <- (llvmGen . parse) contents
B.writeFile "a.out.ll" result B.writeFile "a.out.ll" result
T.putStrLn "- Compiling to executable './a.out'..." T.putStrLn "- Compiling to executable './a.out'..."
callCommand "clang a.out.ll" callCommand "clang a.out.ll"
T.putStrLn "- Done." T.putStrLn "- Done."
main :: IO ()
main = execParser opts >>= run
where
parser =
Opt
<$> argument str (metavar "FILE_PATH")
<*> switch
( short 'l'
<> long "showLLVM"
<> help "Create <file>.ll with LLVM used to compile the binary"
)
<*> switch
( short 'd'
<> long "showDebug"
<> help "Show debug output"
)
opts = info parser mempty