-
Notifications
You must be signed in to change notification settings - Fork 0
/
TigerParser.hs
335 lines (287 loc) · 7.35 KB
/
TigerParser.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
module TigerParser where
import Text.Parsec
import Text.Parsec.String (Parser)
import qualified Text.Parsec.Token as Tok
import qualified Text.Parsec.Expr as Ex
import TigerAbs
import TigerLexer
import TigerSymbol
binary s f assoc ln = Ex.Infix (do
reservedOp s
return (\e1 e2 -> OpExp e1 f e2 ln)) assoc
amperCmp ln = Ex.Infix (do
reservedOp "&"
return (\e1 e2 -> IfExp e1 e2 (Just (IntExp 0 ln)) ln)) Ex.AssocLeft
pipeCmp ln = Ex.Infix (do
reservedOp "|"
return (\e1 e2 -> IfExp e1 (IntExp 1 ln) (Just e2) ln)) Ex.AssocLeft
table ln = [
[binary "*" TimesOp Ex.AssocLeft ln
,binary "/" DivideOp Ex.AssocLeft ln]
,
[binary "+" PlusOp Ex.AssocLeft ln
,binary "-" MinusOp Ex.AssocLeft ln]
,
[binary "=" EqOp Ex.AssocNone ln
,binary "<>" NeqOp Ex.AssocNone ln
,binary "<" LtOp Ex.AssocNone ln
,binary ">" GtOp Ex.AssocNone ln
,binary "<=" LeOp Ex.AssocNone ln
,binary ">=" GeOp Ex.AssocNone ln
]
,[amperCmp ln]
,[pipeCmp ln]
]
int :: Parser Exp
int = do
pos <- gline
t <- number
return (IntExp t pos)
v' :: Var -> Parser Var
v' var = (do
dot
s <- identifier
v' (FieldVar var (pack s)))
<|>
(do
e <- brackets expression
v' (SubscriptVar var e))
<|> return var
variable :: Parser Var
variable = do
s <- identifier
let st = SimpleVar $ pack s
(v' st)
field :: Parser [(Symbol, Escapa , Ty)]
field =
commaSep (do
n <- identifier
colon
ty <- identifier
return (pack n, NoEscapa , NameTy (pack ty)))
field' :: Parser [(Symbol, Ty)]
field' =
commaSep (do
n <- identifier
colon
ty <- identifier
return (pack n, NameTy (pack ty)))
ftype :: Parser (Maybe Symbol)
ftype = ( do
ty <- identifier
return (Just $ pack ty)
) <|> return Nothing
mTypo :: Parser (Maybe Symbol)
mTypo = (do
colon
ftype) <|> return Nothing
fundec :: Parser (Symbol,[(Symbol, Escapa , Ty)], Maybe Symbol, Exp, Pos)
fundec = do
p <- gline
reserved "function"
name <- identifier
params <- parens field
m <- mTypo
symbol "="
e <- expression
return (pack $ name, params, m, e, p)
functiondec :: Parser Dec
functiondec = FunctionDec <$> many1 fundec
vardec :: Parser Dec
vardec = do
p <- gline
reserved "var"
name <- identifier
t <- mTypo
e <- maybe (symbol "=") (const $ symbol ":=") t >> expression
return (VarDec (pack name)
NoEscapa -- Todas las variables __no__ escapan por default.
t e p)
namety :: Parser Ty
namety = NameTy . pack <$> identifier
rety :: Parser Ty
rety = RecordTy <$> braces field'
arrty :: Parser Ty
arrty = reserved "array" >> reserved "of" >>
ArrayTy . pack <$> identifier
ty :: Parser Ty
ty = namety <|> rety <|> arrty
tydec :: Parser (Symbol, Ty, Pos)
tydec = do
p <- gline
reserved "type"
name <- identifier
symbol "="
t <- ty
return (pack name, t, p)
tydecs :: Parser Dec
tydecs = TypeDec <$> many1 tydec
declarations :: Parser Dec
declarations = functiondec
<|> tydecs
<|> vardec
gline :: Parser Pos
gline = do
posSrc <- getPosition -- Magia! Gracias Parsec
let (l,c) = (sourceLine posSrc, sourceColumn posSrc)
return (Simple l c)
varexp :: Parser Exp
varexp = do
p <- gline
v <- variable
return (VarExp v p)
letexp :: Parser Exp
letexp = do
b <- gline
reserved "let"
dcs <- many1 declarations
reserved "in"
b' <- gline
body <- semiSep1 expression
e <- gline
reserved "end"
if Prelude.length body == 1 then
return (LetExp dcs (Prelude.head body) (Range b e))
else
return (LetExp dcs (SeqExp body b') (Range b e))
arrayexp :: Parser Exp
arrayexp = do
p <- gline
i <- identifier
size <- brackets expression
reserved "of"
init <- expression
return (ArrayExp (pack i) size init p)
breakexp :: Parser Exp
breakexp = do
p <- gline
reserved "break"
return (BreakExp p)
forexp :: Parser Exp
forexp = do
p <- gline
reserved "for"
i <- identifier
symbol ":="
lo <- expression
reserved "to"
hi <- expression
reserved "do"
body <- expression
return (ForExp (pack i) NoEscapa lo hi body p)
whileexp :: Parser Exp
whileexp = do
p <- gline
reserved "while"
cond <- expression
reserved "do"
body <- expression
return (WhileExp cond body p)
ifexp :: Parser Exp -- Chequear el tema de la assoc del if
ifexp = do
p <- gline
reserved "if"
c <- expression
reserved "then"
e <- expression
(do
reserved "else"
ee <- expression
return (IfExp c e (Just ee) p)
<|>
return (IfExp c e Nothing p))
assignexp :: Parser Exp
assignexp = do
p <- gline
v <- variable
symbol ":="
e <- expression
return (AssignExp v e p)
seqexp :: Parser Exp
seqexp = do
p <- gline
es <- parens $ semiSep1 expression
return (SeqExp es p)
seqexpWOut :: Parser Exp
seqexpWOut = do
p <- gline
es <- semiSep1 expression
return (SeqExp es p)
recfld :: Parser (Symbol, Exp)
recfld = do
i <- identifier
reservedOp "="
e <- expression
return (pack i, e)
recordexp :: Parser Exp
recordexp = do
p <- gline
tname <- identifier
fls <- braces $ commaSep recfld
return (RecordExp fls (pack tname) p)
callexp :: Parser Exp
callexp = do
p <- gline
f <- identifier
args <- parens $ commaSep expression
return (CallExp (pack f) args p)
stringexp :: Parser Exp
stringexp = do
p <- gline
s <- stringLiteral
return (StringExp s p)
nilexp :: Parser Exp
nilexp = do
p <- gline
reserved "nil"
return (NilExp p)
expression :: Parser Exp
expression = whiteSpace >> parseexp
unitexp :: Parser Exp
unitexp = do
p <- gline
reserved "()"
return $ UnitExp p
expression' :: Parser Exp
expression' =
try letexp
<|> try callexp
<|> try unitexp
<|> try arrayexp
<|> try recordexp
<|> try forexp
<|> try whileexp
<|> try breakexp
<|> try ifexp
<|> try assignexp
<|> try stringexp
<|> try nilexp
<|> varexp
<|> int
<|> seqexp
parseexp :: Parser Exp
parseexp = do
p <- gline
try $ Ex.buildExpressionParser (table p) expression'
parseFromStr :: Monad m
=> (String, Int, Int)
-> String -> m Exp
parseFromStr (file, line, col) s =
case runParser parser () "" s of
Left err -> fail $ show err
Right e -> return e
where
parser = do pos <- getPosition
setPosition $
(flip setSourceName) file $
(flip setSourceLine) line $
(flip setSourceColumn) col $
pos
spaces
e <- expression
eof
return e
parse :: String -> Either ParseError Exp
parse p = runParser expression () p p
parseFromFile :: FilePath -> IO (Either ParseError Exp)
parseFromFile p = runParser expression () p <$> readFile p