Created
February 14, 2017 07:17
-
-
Save wyager/741b8f73e6eda424c6f436ebe9cd8bbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Text.Parsec | |
import Text.Parsec.String | |
data Expr = Mul Expr Expr | Add Expr Expr | Lit Int deriving Show | |
expr :: Parser Expr | |
expr = try lit <|> try mul <|> try add <|> parenthesized | |
where | |
mul = char '*' >> Mul <$> rec <*> rec | |
add = char '+' >> Add <$> rec <*> rec | |
lit = (Lit . read) <$> many1 digit | |
rec = many space >> expr | |
parenthesized = between (char '(') (char ')') rec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment