From d52939715e33a40b2c59ccfadf417b2752d3229e Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 26 Mar 2014 13:09:09 +1100 Subject: Fixed detection of unclosed brackets --- Brainfuck/Parser.hs | 43 ++++++++++++++++++++++++++++--------------- 1 file 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 "><+-.,[]" -- cgit