add input and output commands parsing and call compilation

This commit is contained in:
Aivean
2018-04-28 16:47:23 -07:00
parent 7422dc8551
commit 8d3a452902
2 changed files with 34 additions and 33 deletions
+19 -10
View File
@@ -11,6 +11,8 @@ import Options.Applicative
import Data.Semigroup ((<>)) import Data.Semigroup ((<>))
import Wasm.WasmParser import Wasm.WasmParser
import Data.Maybe (fromMaybe)
data OutputMode = WasmBinary | JSWrapper deriving (Show, Eq) data OutputMode = WasmBinary | JSWrapper deriving (Show, Eq)
data WasmConfig = WasmConfig { data WasmConfig = WasmConfig {
@@ -28,15 +30,22 @@ compile input output = do
Left reason -> Left reason ->
putStrLn $ "Cannot complie module: " ++ reason putStrLn $ "Cannot complie module: " ++ reason
main :: IO () main :: IO ()
main = parseArgs =<< execParser opts main = do
where config <- execParser opts
opts = info (sample <**> helper) process config
( fullDesc where
<> progDesc "Print a greeting for TARGET" opts = info (sample <**> helper)
<> header "hello - a test for optparse-applicative" ) ( fullDesc
<> progDesc "Haskell WebAssembly Toolkit"
<> header "Compile WebAssembly code into binary" )
parseArgs :: WasmParser -> IO () process :: WasmParser -> IO ()
parseArgs (WasmParser h False n) = putStrLn $ "Hello, " ++ h ++ replicate n '!'
parseArgs _ = return () process (WasmParser (Just output) input) =
compile input output
process (WasmParser Nothing input) =
compile input "out.wasm"
process _ = return ()
+15 -23
View File
@@ -3,31 +3,23 @@ module Wasm.WasmParser where
import Options.Applicative import Options.Applicative
import Data.Semigroup ((<>)) import Data.Semigroup ((<>))
data WasmFileFormat = WasmText | WasmBinary | WasmScript deriving (Read, Show) --data WasmFileFormat = WasmText | WasmBinary | WasmScript deriving (Read, Show)
--
instance Read WasmFileFormat where --instance Read WasmFileFormat where
readsPrec _ ("WAT":xs) = [ (WasmText, xs) ] -- readsPrec _ ("WAT":xs) = [ (WasmText, xs) ]
readsPrec _ ("WAST":xs) = [ (WasmScript, xs) ] -- readsPrec _ ("WAST":xs) = [ (WasmScript, xs) ]
readsPrec _ ("WASM":xs) = [ (WasmBinary, xs) ] -- readsPrec _ ("WASM":xs) = [ (WasmBinary, xs) ]
data WasmParser = WasmParser data WasmParser = WasmParser
{ hello :: String { output :: Maybe String
, quiet :: Bool , input :: String }
, enthusiasm :: Int }
sample :: Parser WasmParser sample :: Parser WasmParser
sample = WasmParser sample = WasmParser
<$> strOption <$> option (maybeReader (Just . Just))
( long "hello" ( long "output"
<> metavar "TARGET" <> short 'o'
<> help "Target for the greeting" ) <> metavar "OUTPUT_FILE"
<*> switch <> help "Output filename"
( long "quiet" <> value Nothing )
<> short 'q' <*> argument str (metavar "INPUT_FILE")
<> help "Whether to be quiet" )
<*> option auto
( long "enthusiasm"
<> help "How enthusiastically to greet"
<> showDefault
<> value 1
<> metavar "INT" )