This commit is contained in:
Claudio Maggioni 2020-10-27 18:09:19 +01:00
parent d12b9590b7
commit 851568fa97
1 changed files with 18 additions and 2 deletions

View File

@ -99,11 +99,27 @@ execLoop d w = case posOrF of
posOrF = if jump then findMatching (program w) (ip w) d else Just $ ip w
executeInstruction :: Instruction -> ProgState -> Either Error ProgState
executeInstruction i = (fmap incrIP) . case i of
executeInstruction w = (fmap incrIP) . case (program w !! ip w) of
TapeLeft -> execTapeLeft
TapeRight -> execTapeRight
Add -> execAdd
Sub -> execSub
Out -> execOut
LoopStart -> execLoop Forward
LoopEnd -> execLoop Backward
LoopEnd -> execLoop Backward $ w
newtype Executor a = Ee Either String a
instance Executor Monad where
`<<=` :: Executor a -> (a -> Executor a) -> Executor A
(Ee a) <<= k = case a of
Left c -> Ee (Left c)
Right b -> k
return :: a -> Executor a
return a = Ee (Left a)
fail :: String -> Executor a
fail c = Ee (Right c)
executeInstruction