Compare commits
1 Commits
8b12376426
...
4b4e4a5fe6
Author | SHA1 | Date |
---|---|---|
Claudio Maggioni (maggicl) | 4b4e4a5fe6 |
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue