Added execute instruction function
This commit is contained in:
parent
4076899334
commit
d12b9590b7
1 changed files with 14 additions and 7 deletions
|
@ -52,8 +52,8 @@ errorDesc :: Error -> String
|
||||||
errorDesc NegTape = "ERROR: '<' not working. Cannot access negative tape positions."
|
errorDesc NegTape = "ERROR: '<' not working. Cannot access negative tape positions."
|
||||||
errorDesc LoopNoMatch = "ERROR: '[' or ']' not working. Matching parenthesis not found."
|
errorDesc LoopNoMatch = "ERROR: '[' or ']' not working. Matching parenthesis not found."
|
||||||
|
|
||||||
incrInt :: ProgState -> ProgState
|
incrIP :: ProgState -> ProgState
|
||||||
incrInt p = p { ip = ip p + 1 }
|
incrIP p = p { ip = ip p + 1 }
|
||||||
|
|
||||||
execAdd :: ProgState -> Either Error ProgState
|
execAdd :: ProgState -> Either Error ProgState
|
||||||
execAdd p = Right p { tape = newTape }
|
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
|
| otherwise = fmHelper (upd s) acc b ob upd limit
|
||||||
where c = prg !! start
|
where c = prg !! start
|
||||||
|
|
||||||
execLoopStart :: ProgState -> Either Error ProgState
|
execLoop :: WalkingDirection -> ProgState -> Either Error ProgState
|
||||||
execLoopStart w = case newInt of
|
execLoop d w = case posOrF of
|
||||||
Just n -> Right w { ip = n }
|
Just n -> Right w { ip = n }
|
||||||
Nothing -> Left LoopNoMatch
|
Nothing -> Left LoopNoMatch
|
||||||
where jump = (tape w !! dp w) == Byte 0
|
where jump = (tape w !! dp w) == Byte 0
|
||||||
posOrF = if jump then findMatching (program w) (ip w) Forward else Just $ ip w
|
posOrF = if jump then findMatching (program w) (ip w) d else Just $ ip w
|
||||||
newInt = fmap (+1) posOrF
|
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
Loading…
Reference in a new issue