summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-03-26 13:09:09 +1100
committerJed Barber <jjbarber@y7mail.com>2014-03-26 13:09:09 +1100
commitd52939715e33a40b2c59ccfadf417b2752d3229e (patch)
treedfb40a2914169dfdfcd8b827139348e36199db69
parent2784bfc9228b80c6e4d3e8d1dd049459ef13ef69 (diff)
Fixed detection of unclosed brackets
-rw-r--r--Brainfuck/Parser.hs43
1 files changed, 28 insertions, 15 deletions
diff --git a/Brainfuck/Parser.hs b/Brainfuck/Parser.hs
index 1f2a414..2c2a530 100644
--- a/Brainfuck/Parser.hs
+++ b/Brainfuck/Parser.hs
@@ -22,24 +22,37 @@ parseBrainfuck = parse brainfuck "error"
-brainfuck = many commentChar >> many fuck
+brainfuck = do
+ many commentChar
+ bs <- many fuck
+ eof
+ return . concat $ bs
+
+fuck = (brainCommand >>= return . (:[]))
+ <|> loop
+ <?> "brainfuck command"
-fuck = do
- f <- brainCommand
+
+loop = do
+ char '['
+ many commentChar
+ bs <- many brainCommand
+ char ']'
many commentChar
- return f
-
-
-brainCommand = try (char '>' >> return R)
- <|> try (char '<' >> return L)
- <|> try (char '+' >> return Inc)
- <|> try (char '-' >> return Dec)
- <|> try (char '.' >> return Out)
- <|> try (char ',' >> return In)
- <|> try (char '[' >> return OpenLoop)
- <|> try (char ']' >> return CloseLoop)
- <?> "brainfuck command"
+ return . concat $ [[OpenLoop],bs,[CloseLoop]]
+
+
+brainCommand = do { b <- brainCom; many commentChar; return b }
+
+
+brainCom = (char '>' >> return R)
+ <|> (char '<' >> return L)
+ <|> (char '+' >> return Inc)
+ <|> (char '-' >> return Dec)
+ <|> (char '.' >> return Out)
+ <|> (char ',' >> return In)
+ <?> "brainfuck command"
commentChar = noneOf "><+-.,[]"