diff options
author | Jed Barber <jjbarber@y7mail.com> | 2014-03-25 22:22:55 +1100 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2014-03-25 22:22:55 +1100 |
commit | 2784bfc9228b80c6e4d3e8d1dd049459ef13ef69 (patch) | |
tree | ebd3d3adc337a8027c771c46d865eecccd5265f5 | |
parent | 7e8cd0e859e41bf7ce8942997367e1cf82bddf80 (diff) |
Brainfuck parser, it really is simple to write
-rw-r--r-- | Brainfuck/Parser.hs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Brainfuck/Parser.hs b/Brainfuck/Parser.hs new file mode 100644 index 0000000..1f2a414 --- /dev/null +++ b/Brainfuck/Parser.hs @@ -0,0 +1,46 @@ +module Brainfuck.Parser ( + parseBrainfuck + ) where + +import Control.Applicative( some ) +import Text.ParserCombinators.Parsec + + + +type BrainfuckProgram = [BrainCom] + + +data BrainCom = R | L | Inc | Dec | Out | In | OpenLoop | CloseLoop + deriving (Show, Eq) + + + + +parseBrainfuck :: String -> Either ParseError BrainfuckProgram +parseBrainfuck = parse brainfuck "error" + + + + +brainfuck = many commentChar >> many fuck + + +fuck = do + f <- brainCommand + 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" + + +commentChar = noneOf "><+-.,[]" + |