Add command line argument parsing
parent
b52fe34667
commit
bdbe823bc3
|
@ -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
|
||||
|
|
|
@ -14,6 +14,7 @@ dependencies:
|
|||
- bytestring
|
||||
- string-conversions
|
||||
- transformers
|
||||
- optparse-applicative >= 0.17 && < 1
|
||||
tested-with: GHC == 9.2.8
|
||||
category: Compilers/Interpreters
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
33
src/Main.hs
33
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."
|
||||
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
|
||||
|
|
Reference in New Issue