10 Commits

Author SHA1 Message Date
Aivean 8d3a452902 add input and output commands parsing and call compilation 2018-04-28 16:47:23 -07:00
Sergey Romanovsky 7422dc8551 Merge branch 'cli' of github.com:SPY/haskell-wasm into cli 2018-04-28 16:36:55 -07:00
Sergey Romanovsky 3addd3dbb1 +WasmFileFormat 2018-04-28 16:35:41 -07:00
Aivean 778cba3d8d fix optparse-applicative in cabal 2018-04-28 15:56:11 -07:00
Ivan Zaytsev 23ad2539e4 basic parsing stub using optparse-applicative 2018-04-28 14:39:00 -07:00
Ilya Rezvov c5daf5b187 add complie function 2018-04-28 14:31:40 -07:00
Ilya Rezvov 1cf5b34d07 add output mode 2018-04-28 14:26:07 -07:00
Ilya Rezvov 845b57f428 add config datatype 2018-04-28 14:24:26 -07:00
Ilya Rezvov 388a505c8d fix executable entry point 2018-04-28 14:07:58 -07:00
Ilya Rezvov 9c5c4a77cc add exectuable sources 2018-04-28 14:04:41 -07:00
5 changed files with 99 additions and 1 deletions
+2 -1
View File
@@ -1,2 +1,3 @@
.stack-work .stack-work
tests/runnable/* tests/runnable/*
.idea
+51
View File
@@ -0,0 +1,51 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ByteString.Lazy as LBS
import qualified Language.Wasm.Lexer as Lexer
import qualified Language.Wasm.Parser as Parser
import qualified Language.Wasm.Binary as Binary
import Options.Applicative
import Data.Semigroup ((<>))
import Wasm.WasmParser
import Data.Maybe (fromMaybe)
data OutputMode = WasmBinary | JSWrapper deriving (Show, Eq)
data WasmConfig = WasmConfig {
inputFile :: String,
outputFile :: String,
outputMode :: OutputMode
} deriving (Show, Eq)
compile :: String -> String -> IO ()
compile input output = do
content <- LBS.readFile input
case Lexer.scanner content >>= Parser.parseModule of
Right mod ->
LBS.writeFile output $ Binary.dumpModuleLazy mod
Left reason ->
putStrLn $ "Cannot complie module: " ++ reason
main :: IO ()
main = do
config <- execParser opts
process config
where
opts = info (sample <**> helper)
( fullDesc
<> progDesc "Haskell WebAssembly Toolkit"
<> header "Compile WebAssembly code into binary" )
process :: WasmParser -> IO ()
process (WasmParser (Just output) input) =
compile input output
process (WasmParser Nothing input) =
compile input "out.wasm"
process _ = return ()
+25
View File
@@ -0,0 +1,25 @@
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 WasmParser = WasmParser
{ output :: Maybe String
, input :: String }
sample :: Parser WasmParser
sample = WasmParser
<$> option (maybeReader (Just . Just))
( long "output"
<> short 'o'
<> metavar "OUTPUT_FILE"
<> help "Output filename"
<> value Nothing )
<*> argument str (metavar "INPUT_FILE")
+9
View File
@@ -31,6 +31,15 @@ library:
- containers >= 0.5 && <0.6 - containers >= 0.5 && <0.6
- utf8-string >= 1.0 - utf8-string >= 1.0
executables:
wasm:
source-dirs: exec
main: Main.hs
dependencies:
- wasm == 0.1.0
- base
- optparse-applicative
tests: tests:
test: test:
main: Test.hs main: Test.hs
+12
View File
@@ -50,6 +50,18 @@ library
Paths_wasm Paths_wasm
default-language: Haskell2010 default-language: Haskell2010
executable wasm
main-is: Main.hs
hs-source-dirs: exec
build-depends:
base >=4.6 && <5.0
, wasm ==0.1.0
, optparse-applicative >= 0.14
, bytestring >=0.10 && <0.11
other-modules:
Wasm.WasmParser
test-suite test test-suite test
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Test.hs main-is: Test.hs