42 lines
1.2 KiB
Haskell
42 lines
1.2 KiB
Haskell
|
module Main where
|
||
|
|
||
|
import System.Environment(getArgs)
|
||
|
import System.Exit(exitFailure)
|
||
|
|
||
|
import MiniWasm.Syntax
|
||
|
import MiniWasm.Validation
|
||
|
import MiniWasm.Execution
|
||
|
import MiniWasm.TestCases.Programs
|
||
|
|
||
|
printError :: String -> IO a
|
||
|
printError err = do
|
||
|
putStrLn err
|
||
|
exitFailure
|
||
|
|
||
|
validateAndExecute :: Module -> IO ()
|
||
|
validateAndExecute m =
|
||
|
case validateModule m of
|
||
|
Left e -> printError $ "Validation error: " ++ e
|
||
|
Right () -> do
|
||
|
let cfg = executeModule m
|
||
|
case executeModule m of
|
||
|
Config{instrs = []} -> print cfg.stack
|
||
|
Config{instrs = [Trapping reason]} -> printError $ "Program trapped with " ++ show reason
|
||
|
_ -> printError "Program didn't reach final state" -- This shouldn't happen unless you modify 'stepUntilFinal' or 'executeModule'
|
||
|
|
||
|
main :: IO ()
|
||
|
main = do
|
||
|
args <- getArgs
|
||
|
case args of
|
||
|
[m] ->
|
||
|
case lookup m testPrograms of
|
||
|
Nothing -> printError "Unknown module name"
|
||
|
Just (m, _) -> validateAndExecute m
|
||
|
_ -> do
|
||
|
printError $
|
||
|
"Usage:\n\
|
||
|
\ miniwasm <module-name> # Validate and execute the specified MiniWasm module\n\
|
||
|
\\n\
|
||
|
\Available modules:\n"
|
||
|
++ unlines (fmap (" " ++) $ fmap fst testPrograms)
|