-
Notifications
You must be signed in to change notification settings - Fork 0
/
miLatte.hs
609 lines (525 loc) · 20.1 KB
/
miLatte.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
module Main where
import System.IO (stdin, hGetContents)
import System.Environment (getArgs, getProgName)
import System.Exit (exitFailure, exitSuccess)
import Control.Monad (when)
import Data.Map (Map, insert, member, lookup, empty, filter, update)
import Control.Monad.State
import LexMiLatte
import ParMiLatte
import SkelMiLatte
import PrintMiLatte
import AbsMiLatte
import TypesMiLatte
import ErrorsMiLatte
import TypesValidator
import ErrM
type ParseFun a = [Token] -> Err a
myLLexer = myLexer
runFile :: ParseFun Program -> FilePath -> IO ()
runFile p f = putStrLn f >> readFile f >>= run p
run :: ParseFun Program -> String -> IO ()
run p s = let ts = myLLexer s in case p ts of
Bad s -> do putStrLn "\nParse Failed...\n"
putStrLn "Tokens:"
putStrLn $ show ts
putStrLn s
exitFailure
Ok tree -> do runValidator tree
runProgram tree
exitSuccess
showTree :: (Show a, Print a) => a -> IO ()
showTree tree
= do
putStrLn $ "\n[Abstract Syntax]\n\n" ++ show tree
putStrLn $ "\n[Linearized tree]\n\n" ++ printTree tree
main :: IO ()
main = do
args <- getArgs
case args of
[] -> getContents >>= run pProgram
fs -> mapM_ (runFile pProgram) fs
runProgram :: Program -> IO ()
runProgram prog = do
liftIO (evalStateT (executeProgram prog) emptyProgState)
emptyProgState = ([],Data.Map.empty,Data.Map.empty,0)
executeProgram :: Program -> ProgStateT ()
executeProgram (Program prog) = do
readProgram prog
executeMain
return ()
readProgram :: [TopDef] -> ProgStateT ()
readProgram [] = return ()
readProgram (x:xs) = do
readTopDef x
readProgram xs
readTopDef :: TopDef -> ProgStateT ()
readTopDef (FnDef ftype ident args block) = do
declareFunOrArr ident (FunEE (ftype,args,block))
executeMain :: ProgStateT ()
executeMain = do
callFun (Ident "main") []
return ()
callFun :: Ident -> [Expr] -> ProgStateT (Maybe Expr)
callFun i exprs = do
case i of
(Ident "print") -> do
executePrint exprs
return Nothing
_ -> do
fun <- getEnvElem i
case fun of
FunEE (ftype,args,block) -> do
(stackOld,envOld,_,_) <- get
prepareProgState args exprs envOld
ret <- executeFun fun
(_,_,storeNew,siNew) <- get
put (stackOld,envOld,storeNew,siNew)
return ret
LambdaEE (ltype,types,args,lblock) ->
case lblock of
NonInit -> throwError "Lambda not initialized" (fun)
LambdaBlock block -> do
(stackOld,envOld,_,_) <- get
prepareProgState args exprs envOld
ret <- executeFun $ FunEE (ltype,args,block)
(_,_,storeNew,siNew) <- get
put (stackOld,envOld,storeNew,siNew)
return ret
_ -> throwError "Not a function" (fun)
prepareProgState :: [Arg] -> [Expr] -> Env -> ProgStateT ()
prepareProgState args exprs oldEnv = do
getFunctionsIntoEnv
initArgs args exprs oldEnv
getFunctionsIntoEnv :: ProgStateT ()
getFunctionsIntoEnv = do
(stack,env,store,si) <- get
let newEnv = (Data.Map.filter (\n -> isFun n) env) in put (stack,newEnv,store,si)
isFun :: EnvElem -> Bool
isFun (FunEE _) = True
isFun _ = False
initArgs :: [Arg] -> [Expr] -> Env -> ProgStateT ()
initArgs [] [] _ = return ()
initArgs as@(a:args) es@(e:exprs) env = do
if (isNumberOfArgsOk as es) then do
declareArg a e env
initArgs args exprs env
else throwErrorNoObj "Wrong number of arguments"
declareArg :: Arg -> Expr -> Env -> ProgStateT ()
declareArg (Arg atype ident) expr env = do
case expr of
EVar i -> do
case atype of
Func _ _ -> do --Lambda
l <- getLambdaEE i env
declareFunOrArr ident l
_ -> do --Variable
loc <- getLoc i env
(stack,env,store,si) <- get
put (stack,Data.Map.insert ident (VarEE loc) env,store,si)
_ -> executeDeclaration atype ((Init ident expr):[])
getLambdaEE :: Ident -> Env -> ProgStateT EnvElem
getLambdaEE i env = do
case Data.Map.lookup i env of
Just l -> return l
_ -> throwError "Fun variable not initialized" i
getLoc :: Ident -> Env -> ProgStateT Loc
getLoc i env = do
case Data.Map.lookup i env of
Just (VarEE l) -> return l
_ -> throwError "Not a variable" i
isNumberOfArgsOk :: [Arg] -> [Expr] -> Bool
isNumberOfArgsOk args exprs = (length args) == (length exprs)
executePrint :: [Expr] -> ProgStateT ()
executePrint (x:[]) = do
s <- convertToString x
printStr s
executePrint xs = do
throwError "Wrong number of arguments for print function" (length xs)
convertToString :: Expr -> ProgStateT String
convertToString (EString s) = return s
convertToString (EVar i) = do
val <- getVarExpr i
convertToString val
convertToString (ELitInt i) = return $ show i
convertToString ELitTrue = return "true"
convertToString ELitFalse = return "false"
convertToString (EArr i arrbrs) = do
envElem <- getEnvElem i
case envElem of
ArrEE a -> do
indeces <- getArrSizes arrbrs
expr <- getArrayElem a indeces
convertToString expr
_ -> throwError "Not an array" envElem
convertToString o = do
res <- evaluateExpression o
convertToString res
printStr :: String -> ProgStateT ()
printStr s = do
liftIO (putStrLn s)
return ()
executeFun :: EnvElem -> ProgStateT (Maybe Expr)
executeFun (FunEE (Void,args,(Block block))) = do
executeVoidFun block
return Nothing
executeFun (FunEE (t,args,(Block block))) = do
ret <- executeRetFun block
return (Just ret)
executeRetFun :: [StmtOrFn] -> ProgStateT Expr
executeRetFun [] = throwErrorNoObj "Missing return statement"
executeRetFun ((StmtF x):xs) = do
readTopDef x
executeRetFun xs
executeRetFun ((StmtS x):xs) = do
case x of
Ret e -> evaluateExpression e
VRet -> throwErrorNoObj "Missing expression in return statement"
_ -> do
executeStatement x
executeRetFun xs
executeVoidFun :: [StmtOrFn] -> ProgStateT ()
executeVoidFun [] = return ()
executeVoidFun ((StmtF x):xs) = do
readTopDef x
executeVoidFun xs
executeVoidFun ((StmtS x):xs) = do
case x of
VRet -> return ()
Ret e -> throwErrorNoObj "Void function cannot return value"
_ -> do
executeStatement x
executeVoidFun xs
executeBlock :: Block -> ProgStateT ()
executeBlock (Block []) = return ()
executeBlock (Block (x:xs)) = do
executeStatementOrFun x
executeBlock (Block xs)
executeStatementOrFun :: StmtOrFn -> ProgStateT ()
executeStatementOrFun (StmtS s) = executeStatement s
executeStatementOrFun (StmtF f) = executeTopDef f
changeVarValue :: Ident -> Expr -> ProgStateT ()
changeVarValue i e = do
evalExpr <- evaluateExpression e
isDeclared <- isVarOrFuncDeclared i
if isDeclared then do
(stack,env,store,si) <- get
varLoc <- getVarLoc i
storeElem <- getStoreElem varLoc
case storeElem of
(VarSE (t,val)) -> do
put (stack,env,Data.Map.insert varLoc (VarSE (t,(Expr evalExpr))) store,si)
return ()
else throwError "Variable not defined" i
executeStatement :: Stmt -> ProgStateT ()
executeStatement Empty = return ()
executeStatement (BStmt b) = executeBlock b
executeStatement (Decl dtype xs) = executeDeclaration dtype xs
executeStatement (ArrDecl atype ident arrbrs) = executeArrayDeclaration atype ident arrbrs
executeStatement (Ass ident expr) = executeVariableAssignment ident expr
executeStatement (AssArr ident arrbrs expr) = executeArrayAssignment ident arrbrs expr
executeStatement (Incr ident) = incrementVariable ident
executeStatement (Decr ident) = decrementVariable ident
executeStatement (Cond expr stmt) = executeConditional expr stmt
executeStatement (CondElse expr stmtif stmtelse) = executeConditionalElse expr stmtif stmtelse
executeStatement (While expr stmt) = executeWhile expr stmt
executeStatement Break = return () --does nothing if it's not in a while loop
executeStatement Continue = return () --does nothing if it's not in a while loop
executeStatement (SExp expr) = do
evaluateExpression expr
return ()
executeStatement _ = return ()
getArrayElem :: ArrEE -> [Integer] -> ProgStateT Expr
getArrayElem (t,xs,sizes) inds = do
isArrOk <- checkArraySize inds sizes
if isArrOk then do
findElemInArray xs inds
else throwErrorNoObj "Wrong indices for array"
checkArraySize :: [Integer] -> [Integer] -> ProgStateT Bool
checkArraySize xs sizes = do
if (length xs /= length sizes) then return False
else do
checkMaxIndices xs sizes
checkMaxIndices :: [Integer] -> [Integer] -> ProgStateT Bool
checkMaxIndices [] [] = return True
checkMaxIndices (x:xs) (s:sizes) = do
if x >= s then return False
else checkMaxIndices xs sizes
findElemInArray :: [([Integer],Expr)] -> [Integer] -> ProgStateT Expr
findElemInArray [] ys = throwErrorNoObj "Element in array not initialized"
findElemInArray ((xs,val):elems) ys = do
if xs == ys then
return val
else (findElemInArray elems ys)
deleteElemInArray :: [([Integer],Expr)] -> [Integer] -> ProgStateT [([Integer],Expr)]
deleteElemInArray arr inds = do
let result = [l | l@(xs,exp) <- arr, xs /= inds] in return result
executeArrayAssignment :: Ident -> [ArrBr] -> Expr -> ProgStateT ()
executeArrayAssignment ident arrbrs expr = do
envElem <- getEnvElem ident
case envElem of
ArrEE (t,xs,sizes) -> do
indeces <- getArrSizes arrbrs
isArrOk <- checkArraySize indeces sizes
if isArrOk then do
value <- evaluateExpression expr
arrWithoutVal <- deleteElemInArray xs indeces --deleting value assigned under those indices
(stack,env,store,si) <- get
put (stack,Data.Map.insert ident (ArrEE (t,((indeces,value):arrWithoutVal),sizes)) env,store,si) --putting an array woth a new value
else throwError "Wrong indeces for array" ident
_ -> throwError "Array not declared" ident
executeArrayDeclaration :: Type -> Ident -> [ArrBr] -> ProgStateT ()
executeArrayDeclaration atype ident exs = do
arrSizes <- getArrSizes exs
declareFunOrArr ident (ArrEE (atype,[],arrSizes))
getArrSizes :: [ArrBr] -> ProgStateT [Integer]
getArrSizes [] = return []
getArrSizes ((ArrBrac x):xs) = do
index <- evaluateNumericExpression x
arr <- getArrSizes xs
return ((index:[]) ++ arr)
executeVariableAssignment :: Ident -> Expr -> ProgStateT ()
executeVariableAssignment i (Lambda ltype args block) = do
isDeclared <- isVarOrFuncDeclared i
case isDeclared of
True -> do
LambdaEE (ltype,types,args,lblock) <- getEnvElem i
(stack,env,store,si) <- get
let l _ = Just (LambdaEE (ltype,types,args,(LambdaBlock block))) in
put (stack,Data.Map.update l i env,store,si)
False -> throwError ("Lambda variable not declared") i
executeVariableAssignment i expr = changeVarValue i expr
executeDeclaration :: Type -> [Item] -> ProgStateT ()
executeDeclaration _ [] = return ()
executeDeclaration dtype@(Func ftype argtypes) (x:xs) = do --lambdas
case x of
NoInit ident -> do
declareFunOrArr ident (LambdaEE (ftype,argtypes,[],NonInit))
Init ident (Lambda ltype args block) -> do
declareFunOrArr ident (LambdaEE (ftype,argtypes,args,(LambdaBlock block)))
executeDeclaration dtype xs
executeDeclaration dtype (x:xs) = do
case x of
NoInit ident -> do
declareVariable ident (VarSE (dtype,NotInst))
Init ident expr -> do
evalExpr <- evaluateExpression expr
declareVariable ident (VarSE (dtype,(Expr evalExpr)))
executeDeclaration dtype xs
executeWhile :: Expr -> Stmt -> ProgStateT ()
executeWhile expr stmt = do
bool <- evaluateBoolExpression expr
if bool then do
case stmt of
BStmt (Block xs) -> do
shouldContinue <- executeWhileBlockStatement xs
if shouldContinue then executeWhile expr stmt else return ()
_ -> do
executeStatement stmt
executeWhile expr stmt
else return ()
executeWhileBlockStatement :: [StmtOrFn] -> ProgStateT Bool --return False if break appears
executeWhileBlockStatement [] = return True
executeWhileBlockStatement ((StmtS x):xs) = do
case x of
Break -> return False
Continue -> return True
BStmt (Block xs) -> executeWhileBlockStatement xs
_ -> do
executeStatement x
executeWhileBlockStatement xs
executeWhileBlockStatement ((StmtF x):xs) = return False
executeConditionalElse :: Expr -> Stmt -> Stmt -> ProgStateT ()
executeConditionalElse expr stmtif stmtelse = do
exprVal <- evaluateBoolExpression expr
if exprVal then executeStatement stmtif
else executeStatement stmtelse
executeConditional :: Expr -> Stmt -> ProgStateT ()
executeConditional expr stmt = do
exprVal <- evaluateBoolExpression expr
if exprVal then executeStatement stmt
else return ()
decrementVariable :: Ident -> ProgStateT ()
decrementVariable i = do
val <- getVarExpr i
case val of
ELitInt integ -> changeVarValue i (ELitInt (integ - 1))
_ -> throwError "Variable is not and integer and cannot be decremented" i
incrementVariable :: Ident -> ProgStateT ()
incrementVariable i = do
val <- getVarExpr i
case val of
ELitInt integ -> changeVarValue i (ELitInt (integ + 1))
_ -> throwError "Variable is not and integer and cannot be incremented" i
evaluateBoolExpression :: Expr -> ProgStateT Bool
evaluateBoolExpression ELitTrue = return True
evaluateBoolExpression ELitFalse = return False
evaluateBoolExpression (EVar ident) = do
varExpr <- getVarExpr ident
case varExpr of
ELitTrue -> return True
ELitFalse -> return False
_ -> throwError "Variable is not a boolean" ident
evaluateBoolExpression (Not expr) = do
result <- evaluateBoolExpression expr
return (not result)
evaluateBoolExpression (EAnd expr1 expr2) = do
result1 <- evaluateBoolExpression expr1
result2 <- evaluateBoolExpression expr2
return (result1 && result2)
evaluateBoolExpression (EOr expr1 expr2) = do
result1 <- evaluateBoolExpression expr1
result2 <- evaluateBoolExpression expr2
return (result1 || result2)
evaluateBoolExpression e@(ERel expr1 relop expr2) = evaluateRelExpression e
evaluateBoolExpression e@(EArr ident arrbrs) = do
expr <- evaluateArrayExpression ident arrbrs
evaluateBoolExpression expr
evaluateBoolExpression e = throwError "Expression is not a boolean" e
evaluateRelExpression :: Expr -> ProgStateT Bool
evaluateRelExpression (ERel expr1 relop expr2) = do
result1 <- evaluateNumericExpression expr1
result2 <- evaluateNumericExpression expr2
case relop of
LTH -> return (result1 < result2)
LE -> return (result1 <= result2)
GTH -> return (result1 > result2)
GE -> return (result1 >= result2)
EQU -> return (result1 == result2)
NE -> return (result1 /= result2)
evaluateRelExpression o = throwError "Expression is not a rel expression" o
evaluateNumericExpression :: Expr -> ProgStateT Integer
evaluateNumericExpression (ELitInt e) = return e
evaluateNumericExpression (EVar ident) = do
varExpr <- getVarExpr ident
case varExpr of
(ELitInt i) -> return i
_ -> throwError "Variable is not an integer" ident
evaluateNumericExpression (Neg e) = do
expr <- evaluateNumericExpression e
return $ negate expr
evaluateNumericExpression e@(EMul expr1 mulOp expr2) = evaluateEMulExpression e
evaluateNumericExpression e@(EAdd expr1 mulOp expr2) = evaluateEAddExpression e
evaluateNumericExpression e@(EArr ident arrbrs) = do
expr <- evaluateArrayExpression ident arrbrs
case expr of
ELitInt i -> return i
_ -> throwError "Array is not of integer type" ident
evaluateNumericExpression e = throwError "Expression is not of integer type" e
evaluateEMulExpression :: Expr -> ProgStateT Integer
evaluateEMulExpression (EMul expr1 mulOp expr2) = do
result1 <- evaluateNumericExpression expr1
result2 <- evaluateNumericExpression expr2
case mulOp of
Times -> return (result1 * result2)
Div -> return (result1 `div` result2)
Mod -> return (result1 `mod` result2)
evaluateEMulExpression o = throwError "Expression is not a mul expression" o
evaluateEAddExpression :: Expr -> ProgStateT Integer
evaluateEAddExpression (EAdd expr1 addOp expr2) = do
result1 <- evaluateNumericExpression expr1
result2 <- evaluateNumericExpression expr2
case addOp of
Plus -> return (result1 + result2)
Minus -> return (result1 - result2)
evaluateEAddExpression o = throwError "Expression is not an add expression" o
evaluateExpression :: Expr -> ProgStateT Expr
evaluateExpression e@(Lambda ltype [arg] block) = return e
evaluateExpression e@(EAdd expr1 addOp expr2) = do
result <- evaluateNumericExpression e
return (ELitInt result)
evaluateExpression e@(EMul expr1 mulOp expr2) = do
result <- evaluateNumericExpression e
return (ELitInt result)
evaluateExpression e@(ERel expr1 relOp expr2) = do
result <- evaluateBoolExpression e
case result of
True -> return ELitTrue
False -> return ELitFalse
evaluateExpression e@(EAnd expr1 expr2) = do
result <- evaluateBoolExpression e
case result of
True -> return ELitTrue
False -> return ELitFalse
evaluateExpression e@(EOr expr1 expr2) = do
result <- evaluateBoolExpression e
case result of
True -> return ELitTrue
False -> return ELitFalse
evaluateExpression e@ELitTrue = return e
evaluateExpression e@ELitFalse = return e
evaluateExpression e@(Neg expr) = do
result <- evaluateNumericExpression e
return (ELitInt result)
evaluateExpression e@(ELitInt i) = return e
evaluateExpression e@(EString s) = return e
evaluateExpression e@(Not expr) = do
result <- evaluateBoolExpression e
case result of
True -> return ELitTrue
False -> return ELitFalse
evaluateExpression (EArr ident arrbrs) = evaluateArrayExpression ident arrbrs
evaluateExpression (EApp ident exprs) = do
ret <- callFun ident exprs
case ret of
Just r -> return r
Nothing -> return ELitTrue
evaluateExpression (EVar ident) = getVarExpr ident
evaluateArrayExpression :: Ident -> [ArrBr] -> ProgStateT Expr
evaluateArrayExpression ident arrbrs = do
envElem <- getEnvElem ident
case envElem of
ArrEE e@(t,xs,sizes) -> do
indeces <- getArrSizes arrbrs
getArrayElem e indeces
_ -> throwError "Array is not declared" ident
executeTopDef :: TopDef -> ProgStateT ()
executeTopDef f = return ()
declareFunOrArr :: Ident -> EnvElem -> ProgStateT ()
declareFunOrArr i ee = do
(stack,env,store,si) <- get
put (stack,Data.Map.insert i ee env,store,si)
declareVariable :: Ident -> StoreElem -> ProgStateT ()
declareVariable i se = do
loc <- allocateMemory i se
(stack,env,store,si) <- get
put (stack,Data.Map.insert i (VarEE loc) env,store,si)
allocateMemory :: Ident -> StoreElem -> ProgStateT Loc
allocateMemory i se = do
(stack,env,store,si) <- get
put (stack,env,Data.Map.insert si se store,si+1)
return si
isVarOrFuncDeclared :: Ident -> ProgStateT Bool
isVarOrFuncDeclared i = do
(_,env,_,_) <- get
if Data.Map.member i env then return True
else return False
getEnvElem :: Ident -> ProgStateT EnvElem
getEnvElem i = do
(_,env,_,_) <- get
case Data.Map.lookup i env of
Just el -> return el
Nothing -> throwError "Env element not declared" i
getStoreElem :: Loc -> ProgStateT StoreElem
getStoreElem l = do
(_,_,store,_) <- get
case Data.Map.lookup l store of
Just el -> return el
Nothing -> throwError "Store element not in scope" l
getVarExpr :: Ident -> ProgStateT Expr
getVarExpr i = do
envElem <- getEnvElem i
case envElem of
VarEE l -> do
VarSE (vtype,sexpr) <- getStoreElem l
case sexpr of
Expr e -> do
return e
NotInst -> throwError "Variable not initialized" i
FunEE f -> return ELitFalse
getVarLoc :: Ident -> ProgStateT Loc
getVarLoc i = do
ee <- getEnvElem i
case ee of
VarEE l -> return l
FunEE f -> throwError "Object is a function, not a variable" i