Create utility function for loading and parsing files
This commit is contained in:
parent
f1d097094d
commit
1b8680004e
2 changed files with 22 additions and 9 deletions
|
|
@ -1,10 +1,9 @@
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import qualified Data.Text.IO as T
|
|
||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
import Text.Megaparsec
|
|
||||||
|
|
||||||
import Mima.Assembler.Parser
|
import Mima.Assembler.Parser
|
||||||
|
import Mima.IO
|
||||||
import Mima.Load
|
import Mima.Load
|
||||||
|
|
||||||
data Settings = Settings
|
data Settings = Settings
|
||||||
|
|
@ -33,13 +32,10 @@ main = do
|
||||||
settings <- execParser opts
|
settings <- execParser opts
|
||||||
|
|
||||||
putStrLn $ "Loading assembly file at " ++ infile settings
|
putStrLn $ "Loading assembly file at " ++ infile settings
|
||||||
asm <- T.readFile (infile settings)
|
asm <- parseFile parseState (infile settings)
|
||||||
|
case asm of
|
||||||
putStrLn "Parsing assembly"
|
Nothing -> pure ()
|
||||||
let parsed = parse parseState (infile settings) asm
|
Just (state, _) -> do
|
||||||
case parsed of
|
|
||||||
Left errorBundle -> putStrLn $ errorBundlePretty errorBundle
|
|
||||||
Right (state, _) -> do
|
|
||||||
putStrLn "Parsing successful"
|
putStrLn "Parsing successful"
|
||||||
putStrLn $ "Writing result to " ++ outfile settings
|
putStrLn $ "Writing result to " ++ outfile settings
|
||||||
saveStateToFile (outfile settings) state
|
saveStateToFile (outfile settings) state
|
||||||
|
|
|
||||||
17
src/Mima/IO.hs
Normal file
17
src/Mima/IO.hs
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue