-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
355 lines (310 loc) · 9.01 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
module Parser where
import Text.Parsec
import Text.Parsec.Token
import Text.Parsec.Language
import Data.Char
import Head
import Type
import Control.Monad.Identity (Identity)
{-# LANGUAGE NoMonomorphismRestriction #-}
parseExpr e = parse expr "Error:" e
parseFile a = do f <- readFile a
let ls = lines f
let fs = (filter (notComment) ls)
r <- parseFile' fs
return (r)
parseFile' f = do let ds = map (parse dd "Error:") (init f)
let e = parse expr' "Error:" (last f)
return (ds,e)
reservados = "$.&|=->{},;()\n "
operators = map varof ["+","-","*","/","==",">","<",">=","<=","===","&&","||"]
opsymbols = "><=-+*/|&"
notComment (' ':s) = notComment s
notComment ('-':('-':_)) = False
notComment [] = False
notComment _ = True
varof c = Var c
expr' :: Parsec String () (Expr)
expr' = do v <- pvar
spaces
char '='
spaces
e <- expr
return e
expr :: Parsec String () (Expr)
expr = do l <- lam
return l
<|>
do apps <- many1 term
return $ foldl1 App apps
<|>
do {t <- tuple; return t}
<|> do char '('
spaces
e <- expr
char ')'
spaces
return e
term :: Parsec String () (Expr)
term = do {i <- ifex; return i}
<|>
do {c <- caseex; return c}
<|>
do {l <- letex; return l}
<|>
do {a <- apps; return a}
alt :: Parsec String () (Pat, Expr)
alt = do spaces
p <- pat
string "->"
spaces
e <- expr
return (p,e)
pat :: Parsec String () (Pat)
pat = do {p <- pcon; return p}
<|>
do {p <- pvar; return p}
<|>
do {t <- tuplePat; return t}
lam :: Parsec String () (Expr)
lam = do char '\\'
spaces
var <- varName
spaces
string "->"
spaces
e <- expr
return (Lam var e)
ifex :: Parsec String () (Expr)
ifex = do string "if "
e1 <- singleExpr
string "then "
e2 <- singleExpr
string "else "
e3 <- singleExpr
return (If e1 e2 e3)
caseex :: Parsec String () (Expr)
caseex = do string "case "
e <- singleExpr
string "of {"
ps <- alt `sepBy` (char ';')
char '}'
return (Case e ps)
letex :: Parsec String () (Expr)
letex = do try $ do string "let "
spaces
v <- many1 (noneOf reservados)
spaces
char '='
spaces
e <- singleExpr
spaces
string "in "
spaces
e' <- expr
return (Let (v,e) e')
<|>
do string "let "
spaces
v <- many1 (noneOf reservados)
spaces
string "::"
spaces
a <- typeScheme
spaces
char '='
spaces
e <- expr
spaces
string "in "
spaces
e' <- expr
return (LetA (v,(quantifyAll a),e) e')
apps :: Parsec String () (Expr)
apps = do as <- many1 singleExpr
return (foldApp as)
singleExpr :: Parsec String () (Expr)
singleExpr = do try $ do char '('
e <- expr
char ')'
spaces
return e
<|>
do var <- varReservada
return (var)
<|>
do var <- varName
spaces
return (Var var)
<|>
do {t <- tuple; return t}
con :: Parsec String () (Expr)
con = do c <- conName
return (Con c)
pvar :: Parsec String () (Pat)
pvar = do var <- varName
spaces
return (PVar var)
pcon :: Parsec String () (Pat)
pcon = do c <- conName
ps <- many pat
return (PCon c ps)
varReservada :: Parsec String () (Expr)
varReservada = do {con <- conName; return (Con con)}
<|>
do op <- many1 (oneOf opsymbols)
spaces
return (varof op)
conName :: Parsec String () ([Char])
conName = do a <- oneOf ['A'..'Z']
as <- many (noneOf reservados)
spaces
return ([a] ++ as)
<|>
do try $ do as <- many1 digit
spaces
return (['0'] ++ as)
varName :: Parsec String () ([Char])
varName = do a <- noneOf (['A'..'Z'] ++ ['1'..'9'] ++ reservados)
as <- many (noneOf reservados)
spaces
let s = [a] ++ as
return ([a] ++ as)
foldApp :: [Expr] -> Expr
foldApp [x] = x
foldApp [f,g] = if g `elem` operators then (App g f) else (App f g)
foldApp (f:(g:as)) = if g `elem` operators then App (App g f) (foldApp as) else App (App f g) (foldApp as)
dd :: Parsec String () [Assump]
dd = do try $ do {a <- adt; return a}
<|>
do try $ do {a <- gadt; return a}
adt :: Parsec String () [Assump]
adt = do string "data"
spaces
i <- conName
spaces
ps <- many tvar
char '='
rs <- tcon `sepBy` (char '|')
return (map (buildADT i ps) rs)
gadt :: Parsec String () [Assump]
gadt = do string "data"
spaces
i <- conName
spaces
ps <- manyTill tvar (try (string "where"))
spaces
char '{'
spaces
rs <- gtcon `sepBy` (char ';')
char '}'
return (toGADT rs)
tvar :: Parsec String () (SimpleType)
tvar = do var <- varName
spaces
return (TVar var)
tcon :: Parsec String () (([Char],[SimpleType]))
tcon = do spaces
c <- conName
spaces
vs <- many tParam
spaces
return (c,vs)
tParam :: Parsec String () SimpleType
tParam = do c <- conName
spaces
return (TCon c)
<|>
do t <- tvar
return t
<|>
do char '('
spaces
t <-tParam
spaces
char ')'
spaces
return t
gtcon :: Parsec String () (Assump)
gtcon = do spaces
c <- conName
string "::"
spaces
cs <- do cs <- constraint
spaces
string "=>"
spaces
return cs
<|> do return E
s <- typeScheme
spaces
return (c :>: quantifyAllC s cs)
typeScheme :: Parsec String () SimpleType
typeScheme = do try $ do t <- typeScheme'
spaces
string "->"
spaces
t' <- typeScheme
return (TArr t t')
<|> typeScheme'
typeScheme' :: Parsec String () SimpleType
typeScheme' = do try $ do ts <- many1 singleType
return (foldl1 TApp ts)
<|>
do singleType
singleType :: Parsec String () SimpleType
singleType = do try $ do char '('
spaces
t <- typeScheme
char ')'
spaces
return t
<|>
do try $ do {c <- tParam; return c}
<|>
do {t <- tupleType; return t}
buildADT i ps (c,vs) = c :>: quantifyAll (foldl1 TArr (vs ++ [foldl1 TApp ([TCon i]++ps)]))
constraint :: Parsec String () SConstraint
constraint = do cs <- singleConstraint `sepBy` (char '^')
return (SConj cs)
singleConstraint :: Parsec String () SConstraint
singleConstraint = do char 'e'
spaces
return (E)
<|>
do char '('
spaces
t <- singleType
spaces
char '~'
spaces
t' <- singleType
spaces
char ')'
spaces
return (TEq t t')
tuple :: Parsec String () Expr
tuple = do char '('
spaces
es <- (do {spaces; e <- expr; return e}) `sepBy` (char ',')
spaces
char ')'
spaces
return (foldl1 App ([Con (idTup (length es))] ++ es))
tuplePat :: Parsec String () Pat
tuplePat = do char '('
spaces
es <- (do {spaces; p <- pat; return p}) `sepBy` (char ',')
spaces
char ')'
spaces
return (PCon (idTup (length es)) es)
tupleType :: Parsec String () SimpleType
tupleType = do char '('
spaces
es <- (do {spaces; t <- typeScheme; return t}) `sepBy` (char ',')
spaces
char ')'
spaces
return (foldl1 TApp ([TCon (idTup (length es))] ++ es))
idTup n = "(" ++ take (n-1) [',',','..] ++ ")"