-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParse.hs
More file actions
27 lines (21 loc) · 1.27 KB
/
Parse.hs
File metadata and controls
27 lines (21 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module Parse where
import Text.ParserCombinators.Parsec (ParseError, many1, many, letter, char, string, between, (<|>), digit)
import qualified Text.Parsec
type Identifier = String
data Expression = BooleanLiteral Bool
| IntegerLiteral Integer
| Abstraction Identifier TypeExpression Expression
| Application Expression Expression
| Variable Identifier
deriving (Eq, Show)
data TypeExpression = Boolean | Integer | TypeExpression :->: TypeExpression deriving (Eq, Show)
parse :: String -> Either ParseError Expression
parse = Text.Parsec.parse application "" where
application = foldl Application <$> primary <*> many (char ' ' *> primary)
primary = abstraction <|> literal <|> parameter <|> between (char '(') (char ')') application
abstraction = Abstraction <$> (char '\\' *> many1 letter) <*> (char ':' *> typ) <*> (char '.' *> application)
typ = (Boolean <$ string "Bool") <|> Integer <$ string "Int" <|> foldr (:->:) <$> typ <*> many (string "->" *> typ)
literal = boolLiteral <|> intLiteral
boolLiteral = BooleanLiteral . read <$> (string "True" <|> string "False")
intLiteral = IntegerLiteral . read <$> many1 digit
parameter = Variable <$> many1 letter