diff --git a/app/MimaAsm/Main.hs b/app/MimaAsm/Main.hs index 77d6656..9550948 100644 --- a/app/MimaAsm/Main.hs +++ b/app/MimaAsm/Main.hs @@ -1,10 +1,9 @@ module Main where -import qualified Data.Text.IO as T import Options.Applicative -import Text.Megaparsec import Mima.Assembler.Parser +import Mima.IO import Mima.Load data Settings = Settings @@ -33,13 +32,10 @@ main = do settings <- execParser opts putStrLn $ "Loading assembly file at " ++ infile settings - asm <- T.readFile (infile settings) - - putStrLn "Parsing assembly" - let parsed = parse parseState (infile settings) asm - case parsed of - Left errorBundle -> putStrLn $ errorBundlePretty errorBundle - Right (state, _) -> do + asm <- parseFile parseState (infile settings) + case asm of + Nothing -> pure () + Just (state, _) -> do putStrLn "Parsing successful" putStrLn $ "Writing result to " ++ outfile settings saveStateToFile (outfile settings) state diff --git a/src/Mima/IO.hs b/src/Mima/IO.hs new file mode 100644 index 0000000..589eb8b --- /dev/null +++ b/src/Mima/IO.hs @@ -0,0 +1,17 @@ +module Mima.IO + ( parseFile + ) where + +import qualified Data.Text.IO as T +import Text.Megaparsec + +import Mima.Parser.Common + +parseFile :: Parser a -> FilePath -> IO (Maybe a) +parseFile parser filepath = do + content <- T.readFile filepath + case parse parser filepath content of + Right a -> pure $ Just a + Left errorBundle -> do + putStrLn $ errorBundlePretty errorBundle + pure Nothing