From d12b9590b7c95ea5eab1b3d3c9d78038de00bdc1 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Tue, 27 Oct 2020 16:09:56 +0100 Subject: [PATCH] Added execute instruction function --- src/Interpreter.hs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Interpreter.hs b/src/Interpreter.hs index 1b897f4..062e58d 100644 --- a/src/Interpreter.hs +++ b/src/Interpreter.hs @@ -52,8 +52,8 @@ errorDesc :: Error -> String errorDesc NegTape = "ERROR: '<' not working. Cannot access negative tape positions." errorDesc LoopNoMatch = "ERROR: '[' or ']' not working. Matching parenthesis not found." -incrInt :: ProgState -> ProgState -incrInt p = p { ip = ip p + 1 } +incrIP :: ProgState -> ProgState +incrIP p = p { ip = ip p + 1 } execAdd :: ProgState -> Either Error ProgState execAdd p = Right p { tape = newTape } @@ -91,12 +91,19 @@ findMatching prg start wd = case wd of | otherwise = fmHelper (upd s) acc b ob upd limit where c = prg !! start -execLoopStart :: ProgState -> Either Error ProgState -execLoopStart w = case newInt of +execLoop :: WalkingDirection -> ProgState -> Either Error ProgState +execLoop d w = case posOrF of Just n -> Right w { ip = n } Nothing -> Left LoopNoMatch where jump = (tape w !! dp w) == Byte 0 - posOrF = if jump then findMatching (program w) (ip w) Forward else Just $ ip w - newInt = fmap (+1) posOrF - + 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 + TapeLeft -> execTapeLeft + TapeRight -> execTapeRight + Add -> execAdd + Sub -> execSub + Out -> execOut + LoopStart -> execLoop Forward + LoopEnd -> execLoop Backward