/
Fuck.hs
73 lines (53 loc) · 2.09 KB
/
Fuck.hs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{-# LANGUAGE FlexibleContexts, NoMonomorphismRestriction #-}
import Text.Parsec hiding (State)
import Control.Monad.State
import Control.Monad.Loops
import Control.Applicative hiding (many)
import Data.Char (chr, ord)
import System.Environment (getArgs)
import Input
data Tape a = Tape [a] a [a]
deriving Show
type Program a = InputT a (State (Tape a)) [a]
runProgram :: Program a -> [a] -> Tape a -> (([a], [a]), Tape a)
runProgram p input tape = runState (runInputT p input) tape
loopProgram :: (Num a, Eq a) => Program a -> Program a
loopProgram = fmap concat . whileM (liftM ((/= 0) . readTape) get)
left :: Tape a -> Tape a
left (Tape (l:ls) a rs) = Tape ls l (a:rs)
right :: Tape a -> Tape a
right (Tape ls a (r:rs)) = Tape (a:ls) r rs
incr :: Num a => Tape a -> Tape a
incr (Tape ls a rs) = Tape ls (a + 1) rs
decr :: Num a => Tape a -> Tape a
decr (Tape ls a rs) = Tape ls (a - 1) rs
modify' :: (Tape a -> Tape a) -> Program a
modify' f = modify f >> return []
output :: Program a
output = (\x -> [readTape x]) <$> get
readTape :: Tape a -> a
readTape (Tape _ a _ ) = a
writeTape :: a -> Tape a -> Tape a
writeTape a (Tape ls _ rs) = Tape ls a rs
writeInput :: Program a
writeInput = input >>= modify' . writeTape
program = fmap concat . sequence
<$> many (choice [loop, rightP, leftP, incrP, decrP, printP, inputP])
where rightP = instruction '>' right
leftP = instruction '<' left
incrP = instruction '+' incr
decrP = instruction '-' decr
printP = output <$ char '.'
inputP = writeInput <$ char ','
loop = fmap loopProgram
$ between (char '[') (char ']') program
instruction c f = modify' f <$ char c
initialTape = Tape (repeat 0) 0 (repeat 0)
fromRight (Right x) = x
parseProgram = fromRight . parse program ""
toFilter :: Program Int -> (String -> String)
toFilter prog = \s -> map chr . fst . fst $ runProgram prog (map ord s) initialTape
main = runProgramAtPath . head =<< getArgs
runProgramAtPath p = do
prog <- parseProgram . filter (`elem` "><+-[].,") <$> readFile p
interact (toFilter prog)