Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8d3a452902 | |||
| 7422dc8551 | |||
| 3addd3dbb1 | |||
| 778cba3d8d | |||
| 23ad2539e4 | |||
| c5daf5b187 | |||
| 1cf5b34d07 | |||
| 845b57f428 | |||
| 388a505c8d | |||
| 9c5c4a77cc |
+2
-1
@@ -1,2 +1,3 @@
|
||||
.stack-work
|
||||
tests/runnable/*
|
||||
tests/runnable/*
|
||||
.idea
|
||||
@@ -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 ()
|
||||
@@ -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")
|
||||
@@ -31,6 +31,15 @@ library:
|
||||
- containers >= 0.5 && <0.6
|
||||
- utf8-string >= 1.0
|
||||
|
||||
executables:
|
||||
wasm:
|
||||
source-dirs: exec
|
||||
main: Main.hs
|
||||
dependencies:
|
||||
- wasm == 0.1.0
|
||||
- base
|
||||
- optparse-applicative
|
||||
|
||||
tests:
|
||||
test:
|
||||
main: Test.hs
|
||||
|
||||
+12
@@ -50,6 +50,18 @@ library
|
||||
Paths_wasm
|
||||
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
|
||||
type: exitcode-stdio-1.0
|
||||
main-is: Test.hs
|
||||
|
||||
Reference in New Issue
Block a user