-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
58 lines (49 loc) · 1.05 KB
/
Parser.hs
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- Copyright © 2014 Garrison Jensen
-- License
-- This code and text are dedicated to the public domain.
-- You can copy, modify, distribute and perform the work,
-- even for commercial purposes, all without asking permission.
-- For more information, please refer to <http://unlicense.org/>
-- or the accompanying LICENSE file.
--
module Parser
( lambdaParser
) where
import Text.Parsec
import Text.Parsec.String
import Data.Either
import LambdaCalc
lam :: Parser Expr
lam = do
char '\\' <|> char 'λ'
n <- letter
string "."
e <- expr
return $ Lam (VC 0 [n]) e
app :: Parser Expr
app = do
apps <- many1 term
return $ foldl1 App apps
var :: Parser Expr
var = do
n <- letter
return $ Var $ VC 0 [n]
parens :: Parser Expr -> Parser Expr
parens p = do
char '('
e <- p
char ')'
return e
term :: Parser Expr
term = var <|> parens expr
expr :: Parser Expr
expr = lam <|> app
decl :: Parser Expr
decl = do
e <- expr
eof
return e
lambdaParser :: IO Expr
lambdaParser = do
n <- getLine
return $ head $ rights [parse decl "" n]