Compare commits

..

1 Commits

Author SHA1 Message Date
Claudio Maggioni (maggicl) 4b4e4a5fe6 Program is now an array 2020-11-08 14:12:17 +01:00
1 changed files with 13 additions and 12 deletions

View File

@ -78,18 +78,18 @@ execOut = StateT $ \p -> putChar (chr (tape p !! dp p)) >> (return (Nothing, p))
findMatching :: Program -> Instruction -> Int -> Maybe Int
findMatching prg par = fmHelper $ -1
where (oppos, limit, next) = case par of
LoopStart -> (LoopEnd, length prg, (+1))
LoopEnd -> (LoopStart, 0, (-1+))
where (oppos, limit, next) = case par of
LoopStart -> (LoopEnd, length prg, (+1))
LoopEnd -> (LoopStart, 0, (-1+))
fmHelper :: Int -> Int -> Maybe Int
fmHelper acc s
| s == limit = Nothing
| acc == 0 && c == oppos = Just s
| c == oppos = fmHelper (acc - 1) $ next s
| c == par = fmHelper (acc + 1) $ next s
| otherwise = fmHelper acc $ next s
where c = prg ! s
fmHelper :: Int -> Int -> Maybe Int
fmHelper acc s
| s == limit = Nothing
| acc == 0 && c == oppos = Just s
| c == oppos = fmHelper (acc - 1) $ next s
| c == par = fmHelper (acc + 1) $ next s
| otherwise = fmHelper acc $ next s
where c = prg ! s
execLoop :: Program -> Instruction -> Int -> State Memory Term
execLoop p i ip = state $ \w -> case posOrF w of
@ -106,7 +106,8 @@ execute p = runStateT (runInstruction 1)
runInstruction ip = fmap checkDone (instrFor ip) >>= dbg >>= loopBack
where loopBack (Next i) = runInstruction i
loopBack x = return x
dbg a = StateT $ \s -> return $ traceShowId (a, s)
dbg a = StateT $ \s -> do _ <- putStrLn . show $ (a, s)
return (a, s)
instrFor :: Int -> StateT Memory IO Term
instrFor ip = case (p ! ip) of