diff --git a/exec/Main.hs b/exec/Main.hs index cc80912..b0349c6 100644 --- a/exec/Main.hs +++ b/exec/Main.hs @@ -11,6 +11,8 @@ import Options.Applicative import Data.Semigroup ((<>)) import Wasm.WasmParser +import Data.Maybe (fromMaybe) + data OutputMode = WasmBinary | JSWrapper deriving (Show, Eq) data WasmConfig = WasmConfig { @@ -28,15 +30,22 @@ compile input output = do Left reason -> putStrLn $ "Cannot complie module: " ++ reason - main :: IO () -main = parseArgs =<< execParser opts - where - opts = info (sample <**> helper) - ( fullDesc - <> progDesc "Print a greeting for TARGET" - <> header "hello - a test for optparse-applicative" ) +main = do + config <- execParser opts + process config + where + opts = info (sample <**> helper) + ( fullDesc + <> progDesc "Haskell WebAssembly Toolkit" + <> header "Compile WebAssembly code into binary" ) -parseArgs :: WasmParser -> IO () -parseArgs (WasmParser h False n) = putStrLn $ "Hello, " ++ h ++ replicate n '!' -parseArgs _ = return () \ No newline at end of file +process :: WasmParser -> IO () + +process (WasmParser (Just output) input) = + compile input output + +process (WasmParser Nothing input) = + compile input "out.wasm" + +process _ = return () \ No newline at end of file diff --git a/exec/Wasm/WasmParser.hs b/exec/Wasm/WasmParser.hs index a4c20a8..65aee7e 100644 --- a/exec/Wasm/WasmParser.hs +++ b/exec/Wasm/WasmParser.hs @@ -3,31 +3,23 @@ module Wasm.WasmParser where import Options.Applicative import Data.Semigroup ((<>)) -data WasmFileFormat = WasmText | WasmBinary | WasmScript deriving (Read, Show) - -instance Read WasmFileFormat where - readsPrec _ ("WAT":xs) = [ (WasmText, xs) ] - readsPrec _ ("WAST":xs) = [ (WasmScript, xs) ] - readsPrec _ ("WASM":xs) = [ (WasmBinary, xs) ] +--data WasmFileFormat = WasmText | WasmBinary | WasmScript deriving (Read, Show) +-- +--instance Read WasmFileFormat where +-- readsPrec _ ("WAT":xs) = [ (WasmText, xs) ] +-- readsPrec _ ("WAST":xs) = [ (WasmScript, xs) ] +-- readsPrec _ ("WASM":xs) = [ (WasmBinary, xs) ] data WasmParser = WasmParser - { hello :: String - , quiet :: Bool - , enthusiasm :: Int } + { output :: Maybe String + , input :: String } sample :: Parser WasmParser sample = WasmParser - <$> strOption - ( long "hello" - <> metavar "TARGET" - <> help "Target for the greeting" ) - <*> switch - ( long "quiet" - <> short 'q' - <> help "Whether to be quiet" ) - <*> option auto - ( long "enthusiasm" - <> help "How enthusiastically to greet" - <> showDefault - <> value 1 - <> metavar "INT" ) \ No newline at end of file + <$> option (maybeReader (Just . Just)) + ( long "output" + <> short 'o' + <> metavar "OUTPUT_FILE" + <> help "Output filename" + <> value Nothing ) + <*> argument str (metavar "INPUT_FILE") \ No newline at end of file