Last active
September 12, 2015 07:24
-
-
Save yoshitsugu/1f06d20afac24b36b9ad 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 | |
data Exp = And Exp Exp | Or Exp Exp | Not Exp | T | F deriving Show | |
expr :: Parsec String st Exp | |
expr = do | |
t <- term | |
Or t <$> (string "or" *> expr) <|> pure t | |
term :: Parsec String st Exp | |
term = do | |
b <- inverse | |
And b <$> (string "and" *> term) <|> pure b | |
inverse :: Parsec String st Exp | |
inverse = spaces *> (Not <$> (string "not" *> spaces *> bool) <|> bool) | |
bool :: Parsec String st Exp | |
bool = toBool <$> (oneOf ['t', 'f'] <* spaces) | |
where | |
toBool 't' = T | |
toBool 'f' = F | |
parseE :: String -> Either ParseError Exp | |
parseE = parse expr "* ParseError *" | |
calc :: String -> Either ParseError Bool | |
calc s = _calc <$> parseE s | |
where | |
_calc :: Exp -> Bool | |
_calc (And a b) = (_calc a) && (_calc b) | |
_calc (Or a b) = (_calc a) || (_calc b) | |
_calc (Not a) = not (_calc a) | |
_calc T = True | |
_calc F = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment