diff options
Diffstat (limited to 'Brainfuck')
-rw-r--r-- | Brainfuck/Parser.hs | 43 |
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 "><+-.,[]" |