Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81333 / brainfsck / src
diff options
context:
space:
mode:
diff --git a/homework/81333/brainfsck/src/Lib.hs b/homework/81333/brainfsck/src/Lib.hs
new file mode 100644
index 0000000..e4cc95c
--- /dev/null
+++ b/homework/81333/brainfsck/src/Lib.hs
@@ -0,0 +1,30 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Lib
+ ( blocks
+ ) where
+
+import Data.Attoparsec.ByteString.Char8
+import qualified Data.ByteString.Char8 as B
+
+import Data.Char
+import Text.Brainfsck.Types
+import Text.Brainfsck.Util
+
+blocks :: Blocks -> [BFExpr] -> IO Blocks
+blocks b [] = pure b
+blocks (Blocks h b r) (BFRight:es) = blocks (Blocks (h + 1) b r) es
+blocks (Blocks h b r) (BFLeft:es) = blocks (Blocks (max (h - 1) 0) b r) es
+blocks (Blocks h b r) (BFPlus:es) = blocks (Blocks h (inc b h) r) es
+blocks (Blocks h b r) (BFMinus:es) = blocks (Blocks h (dec b h) r) es
+blocks (Blocks h b r) (BFPut:es) =
+ blocks (Blocks h b (r <> B.singleton (chr $ b !! h))) es
+blocks (Blocks h b r) (BFGet:es) = do
+ v <- getChar
+ blocks (Blocks h (load b h $ ord v) r) es
+blocks current@(Blocks h b r) (BFLoop expr:es) = do
+ if b !! h == 0
+ then blocks current es
+ else do
+ newBlocks <- blocks current expr
+ blocks newBlocks ((BFLoop expr) : es)
diff --git a/homework/81333/brainfsck/src/Text/Brainfsck/Parser.hs b/homework/81333/brainfsck/src/Text/Brainfsck/Parser.hs
new file mode 100644
index 0000000..28c019a
--- /dev/null
+++ b/homework/81333/brainfsck/src/Text/Brainfsck/Parser.hs
@@ -0,0 +1,56 @@
+module Text.Brainfsck.Parser
+ ( parseText
+ ) where
+
+import Control.Applicative
+
+import Data.Attoparsec.ByteString.Char8
+import qualified Data.ByteString.Char8 as B
+import Data.Either (fromRight)
+
+import Text.Brainfsck.Types
+
+goRight :: Parser BFExpr
+goRight = do
+ char '>'
+ pure BFRight
+
+goLeft :: Parser BFExpr
+goLeft = do
+ char '<'
+ pure BFLeft
+
+plus :: Parser BFExpr
+plus = do
+ char '+'
+ pure BFPlus
+
+minus :: Parser BFExpr
+minus = do
+ char '-'
+ pure BFMinus
+
+get :: Parser BFExpr
+get = do
+ char ','
+ pure BFGet
+
+put :: Parser BFExpr
+put = do
+ char '.'
+ pure BFPut
+
+bfsymbol :: Parser BFExpr
+bfsymbol = goRight <|> goLeft <|> plus <|> minus <|> get <|> put <|> loop
+
+loop :: Parser BFExpr
+loop = do
+ char '['
+ m <- many' bfsymbol
+ char ']'
+ pure $ BFLoop m
+
+parseText :: B.ByteString -> [BFExpr]
+parseText s =
+ fromRight [] $
+ parseOnly (many' bfsymbol) $ B.filter ((flip elem) "+-,.<>[]") s
diff --git a/homework/81333/brainfsck/src/Text/Brainfsck/Types.hs b/homework/81333/brainfsck/src/Text/Brainfsck/Types.hs
new file mode 100644
index 0000000..5dd26f6
--- /dev/null
+++ b/homework/81333/brainfsck/src/Text/Brainfsck/Types.hs
@@ -0,0 +1,24 @@
+{-# LANGUAGE OverloadedStrings #-}
+
+module Text.Brainfsck.Types where
+
+import qualified Data.ByteString.Char8 as B
+
+data BFExpr
+ = BFRight
+ | BFLeft
+ | BFPlus
+ | BFMinus
+ | BFGet
+ | BFPut
+ | BFLoop [BFExpr]
+ deriving (Show)
+
+data Blocks = Blocks
+ { h :: Int
+ , b :: [Int]
+ , r :: B.ByteString
+ } deriving (Show)
+
+newBlocks :: Blocks
+newBlocks = Blocks {h = 0, b = [0,0 ..], r = ""}
diff --git a/homework/81333/brainfsck/src/Text/Brainfsck/Util.hs b/homework/81333/brainfsck/src/Text/Brainfsck/Util.hs
new file mode 100644
index 0000000..fb2d5ad
--- /dev/null
+++ b/homework/81333/brainfsck/src/Text/Brainfsck/Util.hs
@@ -0,0 +1,10 @@
+module Text.Brainfsck.Util where
+
+inc :: [Int] -> Int -> [Int]
+inc b h = take h b <> [b !! h + 1] <> drop (h + 1) b
+
+dec :: [Int] -> Int -> [Int]
+dec b h = take h b <> [b !! h - 1] <> drop (h + 1) b
+
+load :: [Int] -> Int -> Int -> [Int]
+load b h v = take h b <> [v] <> drop (h + 1) b