Implement basic mima-run functionality

Bugs fixed:
- Print correct file name when load/save error occurs
- Correctly identify invalid file formats
This commit is contained in:
Joscha 2020-03-27 22:07:24 +00:00
parent 362025c8df
commit 6b81fd67b4
5 changed files with 52 additions and 16 deletions

View file

@ -6,8 +6,8 @@ module Mima.Vm.State
, basicState
, AbortReason(..)
, step
, run
, runN
, execute
, executeN
) where
import Control.Applicative
@ -157,16 +157,16 @@ step ms = do
(SmallInstruction so lv) -> doSmallOpcode so lv ms'
(LargeInstruction lo sv) -> doLargeOpcode lo sv ms'
run :: MimaState -> (MimaState, AbortReason, Integer)
run = helper 0
execute :: MimaState -> (MimaState, AbortReason, Integer)
execute = helper 0
where
helper completed s =
case step s of
Left e -> (s, e, completed)
Right s' -> helper (completed + 1) s'
runN :: Integer -> MimaState -> (MimaState, Maybe AbortReason, Integer)
runN n = helper 0
executeN :: Integer -> MimaState -> (MimaState, Maybe AbortReason, Integer)
executeN n = helper 0
where
helper completed s =
if completed >= n

View file

@ -32,7 +32,7 @@ loadMimaState :: FilePath -> Run MimaState
loadMimaState path = do
bs <- readFileBS path
case B.decodeOrFail bs of
Right ("", 0, a) -> pure a
Right ("", _, a) -> pure a
Right _ -> throw "invalid file format"
Left (_, _, e) -> throw $ T.pack e