Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81248 / brainfsck / src / Brainfsck / Eval.hs
diff options
context:
space:
mode:
1 files changed, 35 insertions, 0 deletions
-rw-r--r--homework/81248/brainfsck/src/Brainfsck/Eval.hs35
diff --git a/homework/81248/brainfsck/src/Brainfsck/Eval.hs b/homework/81248/brainfsck/src/Brainfsck/Eval.hs
new file mode 100644
index 0000000..10ffb75
--- /dev/null
+++ b/homework/81248/brainfsck/src/Brainfsck/Eval.hs
@@ -0,0 +1,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