1 module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar, typeCheckFunction, addFunction, emptyState, State(..)) where
5 import Control.Monad.State hiding (State)
6 import Data.Map as Map hiding (map)
7 import Prelude hiding (lookup)
9 type Types = [Map Ident Type]
10 type Function = (Type, [Type])
12 data State = State {variables::Types,functions::(Map Ident Function),function::Ident}
14 emptyState = State{variables=[empty], functions=(empty), function=(Ident "")}
16 assert :: Monad m => Bool -> String -> m ()
17 assert True _ = return ()
18 assert False s = fail s
20 typeCheckExp :: (MonadState State m) => Exp -> m Type
21 typeCheckExp (BiOpExp e o e') = do
24 assert (t1 == t2) "The parameters for the binary operator aren't equal"
25 if elem o [Eq,NEq] then return TBool
27 assert (t1 == TInt) "The parameters need to be of type int"
28 if elem o [Plus,Minus,Times,Div]
31 typeCheckExp (EVar i) = typeCheckVar i
32 typeCheckExp (EAss i e) = do
35 assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t
37 typeCheckExp (EInt i) = return TInt
38 typeCheckExp (EBool b) = return TBool
39 typeCheckExp EReadI = return TInt
40 typeCheckExp EReadB = return TBool
41 typeCheckExp (EPost i op) = do
42 TInt <- typeCheckVar i
44 typeCheckExp (ENeg e) = do
45 TInt <- typeCheckExp e
47 typeCheckExp (ENot e) = do
48 TBool <- typeCheckExp e
50 typeCheckExp (EFunc i as) = do
52 (t,ts) <- lookup i $ functions state
56 checkParams :: (MonadState State m) => [Exp] -> [Type] -> m ()
57 checkParams [] [] = return ()
58 checkParams [] _ = fail "Too for arguments when calling function"
59 checkParams _ [] = fail "Too many arguments when calling function"
60 checkParams (e:es) (t:ts) = do
62 assert (t == t2) "Arugments does not match"
65 typeCheckVar :: (MonadState State m) => Ident -> m Type
68 findVariable i $ variables s
70 findVariable :: (MonadState State m) => Ident -> Types -> m Type
71 findVariable i [] = fail $ "Variable "++show i++" not found in any scope."
72 findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms
74 pushAndPop :: (MonadState State m) => m a -> m ()
76 modify (\s -> s { variables = empty:variables s})
78 modify (\s -> s { variables = tail $ variables s})
80 typeCheckStm :: (MonadState State m) => Stm -> m ()
81 typeCheckStm SNoop = return ()
82 typeCheckStm (SExp e) = do
85 typeCheckStm (SBlock ss) = pushAndPop $ mapM typeCheckStm ss
86 typeCheckStm (SIf e s s') = do
87 TBool <- typeCheckExp e
88 pushAndPop $ typeCheckStm s
89 pushAndPop $ typeCheckStm s'
90 typeCheckStm (SWhile e s) = do
91 TBool <- typeCheckExp e
92 pushAndPop $ typeCheckStm s
93 typeCheckStm (SDeclD t i) = addVariable i t
94 typeCheckStm (SDecl t i e) = do
96 assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t
98 typeCheckStm (SPrint e) = do
101 typeCheckStm (SReturn e) = do
104 (t2,_) <- lookup (function state) $ functions state
105 assert (t == t2) $ "Illegal to return "++show t++" in function "++show (function state)++" which returns "++show t2
108 addVariable :: (MonadState State m) => Ident -> Type -> m ()
111 let (m:ms) = variables s in case insertLookupWithKey (\k a1 a2 -> a1) i t m of
112 (Nothing,m') -> modify (\s -> s{ variables = m':ms})
113 _ -> fail $ "Duplicate variable declaration: "++show i
115 typeCheckFunction :: (MonadState State m) => Func -> m ()
116 typeCheckFunction (Func t i d s) = do
118 modify (\s -> s{variables=[empty], function=i})
119 mapM (\(Decl t i) -> addVariable i t) d
121 (SReturn _) -> return ()
122 _ -> fail $ "Function "++show i++" doesn't end with return statement"
126 addFunction :: (MonadState State m) => Func -> m ()
127 addFunction (Func t i d _) = do
129 let m = functions s in case insertLookupWithKey (\k a1 a2 -> a1) i (t, map (\(Decl t i) -> t) d) m of
130 (Nothing,m') -> modify (\s -> s{ functions = m'})
131 _ -> fail $ "Duplicate variable declaration: "++show i