Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81248 / brainfsck / src / Brainfsck
diff options
context:
space:
mode:
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
diff --git a/homework/81248/brainfsck/src/Brainfsck/Parse.hs b/homework/81248/brainfsck/src/Brainfsck/Parse.hs
new file mode 100644
index 0000000..1a9625d
--- /dev/null
+++ b/homework/81248/brainfsck/src/Brainfsck/Parse.hs
@@ -0,0 +1,60 @@
+module Brainfsck.Parse
+ ( parse
+ , ps
+ ) where
+
+import Prelude hiding (takeWhile)
+
+import Data.Functor (($>))
+import Data.Either (fromRight)
+import Data.Foldable (asum)
+
+import Data.ByteString (ByteString)
+
+import Data.Attoparsec.ByteString.Char8 hiding (parse)
+
+import Brainfsck.Types
+expr :: Parser Expr
+expr = asum
+ [ goRight
+ , goLeft
+ , incr
+ , decr
+ , out
+ , inp
+ , loop
+ ]
+
+eatComments :: Parser ()
+eatComments = takeWhile (\x -> x `notElem` ("><+-,.[]" :: String)) $> ()
+
+comments :: Parser a -> Parser a
+comments p = eatComments *> p <* eatComments
+
+goRight :: Parser Expr
+goRight = comments $ char '>' $> GoRight
+
+goLeft :: Parser Expr
+goLeft = comments $ char '<' $> GoLeft
+
+incr :: Parser Expr
+incr = comments $ char '+' $> Incr
+
+decr :: Parser Expr
+decr = comments $ char '-' $> Decr
+
+out :: Parser Expr
+out = comments $ char '.' $> Out
+
+inp :: Parser Expr
+inp = comments $ char ',' $> In
+
+loop :: Parser Expr
+loop = comments $ Loop <$>
+ (char '[' *> comments (many' expr) <* char ']')
+
+parse :: ByteString -> Either String [Expr]
+parse = parseOnly (many' expr <* endOfInput)
+
+ps :: ByteString -> [Expr]
+ps = fromRight [] . parse
diff --git a/homework/81248/brainfsck/src/Brainfsck/Types.hs b/homework/81248/brainfsck/src/Brainfsck/Types.hs
new file mode 100644
index 0000000..6195a24
--- /dev/null
+++ b/homework/81248/brainfsck/src/Brainfsck/Types.hs
@@ -0,0 +1,28 @@
+module Brainfsck.Types
+ ( Expr(..)
+ , Tape(..)
+ , initialTape
+ ) where
+
+data Expr
+ = GoRight -- increment the data pointer (to point to the next cell to the right).
+ | GoLeft -- decrement the data pointer (to point to the next cell to the left).
+ | Incr -- increment (increase by one) the byte at the data pointer.
+ | Decr -- decrement (decrease by one) the byte at the data pointer.
+ | Out -- output the byte at the data pointer.
+ | In -- accept one byte of input, storing its value in the byte at the data pointer.
+ | Loop [Expr]
+ deriving (Show, Eq)
+
+-- if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
+-- if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
+
+data Tape = Tape
+ { left :: [Int]
+ , head :: Int
+ , right :: [Int]
+ }
+ deriving (Show, Eq)
+
+initialTape :: Tape
+initialTape = Tape (repeat 0) 0 $ repeat 0