Create Runner

This commit is contained in:
2021-11-10 11:48:09 +01:00
parent 8880fcae8a
commit 00219b6316
3 changed files with 18 additions and 9 deletions

View File

@@ -34,6 +34,7 @@ executable MVM
Assembler.Parser Assembler.Parser
Assembler.Emitter Assembler.Emitter
Assembler.Compiler Assembler.Compiler
Runner
Util Util
-- LANGUAGE extensions used by modules in this package. -- LANGUAGE extensions used by modules in this package.
@@ -74,6 +75,7 @@ test-suite spec
Assembler.Tokenizer Assembler.Tokenizer
Assembler.Parser Assembler.Parser
Assembler.Emitter Assembler.Emitter
Runner
Util Util
default-language: Haskell2010 default-language: Haskell2010

View File

@@ -1,21 +1,14 @@
module Main where module Main where
import System.Environment import System.Environment
import Control.Monad.Trans.Except (runExceptT, except)
import VirtualMachine.VM (VM(..), empty) import Runner (run, runDebug)
import VirtualMachine.Interpreter (run)
import Assembler.Compiler (compile)
import qualified Data.ByteString as B
interpret :: String -> IO (Either String VM)
interpret input = runExceptT $ (except $ return $ input) >>= (except . compile) >>= (except . return . B.pack) >>= run empty { _debug = False }
main :: IO () main :: IO ()
main = do main = do
(filename:_) <- getArgs (filename:_) <- getArgs
input <- readFile filename input <- readFile filename
result <- interpret input result <- run input
case result of case result of
(Right vm) -> do (Right vm) -> do
putStrLn $ "\n\nDone:\n" ++ (show vm) putStrLn $ "\n\nDone:\n" ++ (show vm)

14
app/Runner.hs Normal file
View File

@@ -0,0 +1,14 @@
module Runner where
import Control.Monad.Trans.Except (runExceptT, except)
import qualified Data.ByteString as B
import Assembler.Compiler (compile)
import VirtualMachine.VM (VM(..), empty)
import qualified VirtualMachine.Interpreter as VM (run)
run :: String -> IO (Either String VM)
run input = runExceptT $ (except $ return $ input) >>= (except . compile) >>= (except . return . B.pack) >>= VM.run empty
runDebug :: String -> IO (Either String VM)
runDebug input = runExceptT $ (except $ return $ input) >>= (except . compile) >>= (except . return . B.pack) >>= VM.run empty { _debug = True }