10 Commits
5 changed files with 99 additions and 1 deletions
+1
View File
@@ -1,2 +1,3 @@
.stack-work
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
- 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
View File
@@ -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