blob: 10ffb753e927c2ed0bc34344304be26b52f1b538 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
{-# OPTIONS -fno-warn-incomplete-patterns #-}
module Brainfsck.Eval
( eval
) where
import Control.Monad (void, foldM)
import Data.Char (chr, ord)
import Brainfsck.Types
(?) :: (a -> b -> c) -> b -> a -> c
(?) = flip
-- We know we never reach [], because we only call this with infinite tapes.
operTape :: Expr -> Tape -> IO Tape
operTape GoRight (Tape ls x (r:rs)) = pure $ Tape (x:ls) r rs
operTape GoLeft (Tape (l:ls) x rs) = pure $ Tape ls l (x:rs)
operTape Incr (Tape ls x rs) = pure $ Tape ls (succ x) rs
operTape Decr (Tape ls x rs) = pure $ Tape ls (pred x) rs
operTape Out t@(Tape _ x _) = t <$ do putChar $ chr x
operTape In (Tape ls _ rs) = (Tape ls ? rs) . ord <$> getChar
operTape l@(Loop xs) t@(Tape _ x _) =
if x == 0 then pure t
else do
tn@(Tape _ x' _) <- evalTape xs t
if x' /= 0 then operTape l tn
else pure tn
evalTape :: [Expr] -> Tape -> IO Tape
evalTape xs t = foldM (flip operTape) t xs
eval :: [Expr] -> IO ()
eval xs = void $ evalTape xs initialTape
|