-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserBrainFuck.hs
38 lines (27 loc) · 1.03 KB
/
ParserBrainFuck.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
module ParserBrainFuck where
import Parser
import Types
----------- PARSER BrainFuck --------------------------------------------------
parserAdd,parserSub,parserInc,parserDec,parserOut,parserInp,parserLoop,parserInst,parserCmt :: Parser Inst
parserAdd = const Add <$$> symbol' '+'
parserSub = const Sub <$$> symbol' '-'
parserInc = const Inc <$$> symbol' '>'
parserDec = const Dec <$$> symbol' '<'
parserOut = const Out <$$> symbol' '.'
parserInp = const Inp <$$> symbol' ','
isComment = not . flip elem "+-><-,[]"
parserCmt = Cmt <$$> oneOrMore ( satisfy' isComment )
parserLoop = f <$$> symbol' '[' <**> parserMultInst <**> symbol' ']'
where f _ b _ = Loop b
parserInst = parserAdd
<|> parserSub
<|> parserInc
<|> parserDec
<|> parserOut
<|> parserInp
<|> parserLoop
<|> parserCmt
parserMultInst :: Parser BrainFuck
parserMultInst = zeroOrMore parserInst
parserBrainFuck :: String -> BrainFuck
parserBrainFuck = fst . head . filter (null . snd) . parserMultInst