diff options
-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 "><+-.,[]" + |