blob: 1a9625dc4fcfc65efbf57611b680e3a2f461c98c (
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
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
|
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
|