X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Typecheck.hs;h=a8c10e55f5dc570e99e79fd9b30b9ad085664d11;hp=38ee8fe070973d51b4602dc6a39e90c928c9fcce;hb=HEAD;hpb=de34a0eeb75a54ce9ae058797327cd5ddc853ecc diff --git a/Typecheck.hs b/Typecheck.hs index 38ee8fe..a8c10e5 100644 --- a/Typecheck.hs +++ b/Typecheck.hs @@ -1,77 +1,136 @@ -module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar) where +module Typecheck (typeCheck, typeCheckExp, typeCheckStm, typeCheckVar, typeCheckFunction, addFunction, emptyState, State(..)) where + import Abssyntax -import Control.Monad.State -import Data.Map as Map +import Control.Monad.State hiding (State) +import Data.Map as Map hiding (map) import Prelude hiding (lookup) -type Types = Map Ident Type +type Types = [Map Ident Type] +type Function = (Type, [Type]) + +data State = State {variables::Types,functions::(Map Ident Function),function::Ident} + +emptyState = State{variables=[empty], functions=(empty), function=(Ident "")} + +assert :: Monad m => Bool -> String -> m () +assert True _ = return () +assert False s = fail s -inList :: Eq a => a -> [a] -> Bool -inList _ [] = False -inList a (x:xs) = if a == x then True else inList a xs +typeCheck :: [Func] -> [Stm] -> IO () +typeCheck fun st = do + runStateT (do mapM addFunction fun; mapM typeCheckFunction fun; mapM typeCheckStm st) emptyState + return () -typeCheckExp :: (MonadState Types m) => Exp -> m Type +typeCheckExp :: (MonadState State m) => Exp -> m Type typeCheckExp (BiOpExp e o e') = do t1 <- typeCheckExp e t2 <- typeCheckExp e' - if not(t1 == t2) then fail "The parameters for the binary operator aren't equal" - else if inList o [Eq,NEq] then return TBool - else if not(t1 == TInt) then fail "The parameters need to be of type int" - else if inList o [Plus,Minus,Times,Div] - then return TInt - else return TBool + assert (t1 == t2) "The parameters for the binary operator aren't equal" + if elem o [Eq,NEq] then return TBool + else do + assert (t1 == TInt) "The parameters need to be of type int" + if elem o [Plus,Minus,Times,Div] + then return TInt + else return TBool typeCheckExp (EVar i) = typeCheckVar i typeCheckExp (EAss i e) = do - a <- typeCheckVar i - b <- typeCheckExp e - if a == b then return a else fail "FEL!" + t <- typeCheckVar i + t2 <- typeCheckExp e + assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t + return t typeCheckExp (EInt i) = return TInt typeCheckExp (EBool b) = return TBool typeCheckExp EReadI = return TInt typeCheckExp EReadB = return TBool -typeCheckExp (ExpT t e) = do - t2 <- typeCheckExp e - if t == t2 then return t else fail "FEL!" -typeCheckExp EDefault = return NoType typeCheckExp (EPost i op) = do TInt <- typeCheckVar i return TInt typeCheckExp (ENeg e) = do TInt <- typeCheckExp e return TInt +typeCheckExp (ENot e) = do + TBool <- typeCheckExp e + return TBool +typeCheckExp (EFunc i as) = do + state <- get + (t,ts) <- lookup i $ functions state + checkParams as ts + return t -typeCheckVar :: (MonadState Types m) => Ident -> m Type +checkParams :: (MonadState State m) => [Exp] -> [Type] -> m () +checkParams [] [] = return () +checkParams [] _ = fail "Too for arguments when calling function" +checkParams _ [] = fail "Too many arguments when calling function" +checkParams (e:es) (t:ts) = do + t2 <- typeCheckExp e + assert (t == t2) "Arugments does not match" + checkParams es ts + +typeCheckVar :: (MonadState State m) => Ident -> m Type typeCheckVar i = do - e <- get - lookup i e + s <- get + findVariable i $ variables s + +findVariable :: (MonadState State m) => Ident -> Types -> m Type +findVariable i [] = fail $ "Variable "++show i++" not found in any scope." +findVariable i (m:ms) = if member i m then lookup i m else findVariable i ms -typeCheckStm :: (MonadState Types m) => Stm -> m Type -typeCheckStm SNoop = return NoType +pushAndPop :: (MonadState State m) => m a -> m () +pushAndPop s = do + modify (\s -> s { variables = empty:variables s}) + s + modify (\s -> s { variables = tail $ variables s}) + +typeCheckStm :: (MonadState State m) => Stm -> m () +typeCheckStm SNoop = return () typeCheckStm (SExp e) = do typeCheckExp e - return NoType -typeCheckStm (SBlock ss) = do - mapM typeCheckStm ss - return NoType + return () +typeCheckStm (SBlock ss) = pushAndPop $ mapM typeCheckStm ss typeCheckStm (SIf e s s') = do TBool <- typeCheckExp e - NoType <- typeCheckStm s - NoType <- typeCheckStm s - return NoType + pushAndPop $ typeCheckStm s + pushAndPop $ typeCheckStm s' typeCheckStm (SWhile e s) = do TBool <- typeCheckExp e - NoType <- typeCheckStm s - return NoType + pushAndPop $ typeCheckStm s +typeCheckStm (SDeclD t i) = addVariable i t typeCheckStm (SDecl t i e) = do t2 <- typeCheckExp e - if t == t2 || t2 == NoType then do - m <- get - case insertLookupWithKey (\k a1 a2 -> a1) i t m of - (Nothing,m') -> put m' - _ -> fail $ "Duplicate variable declaration: "++show i - return NoType - else fail $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t + assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t + addVariable i t typeCheckStm (SPrint e) = do typeCheckExp e - return NoType + return () +typeCheckStm (SReturn e) = do + t <- typeCheckExp e + state <- get + (t2,_) <- lookup (function state) $ functions state + assert (t == t2) $ "Illegal to return "++show t++" in function "++show (function state)++" which returns "++show t2 + + +addVariable :: (MonadState State m) => Ident -> Type -> m () +addVariable i t = do + s <- get + let (m:ms) = variables s in case insertLookupWithKey (\k a1 a2 -> a1) i t m of + (Nothing,m') -> modify (\s -> s{ variables = m':ms}) + _ -> fail $ "Duplicate variable declaration: "++show i + +typeCheckFunction :: (MonadState State m) => Func -> m () +typeCheckFunction (Func t i d s) = do + state <- get + modify (\s -> s{variables=[empty], function=i}) + mapM (\(Decl t i) -> addVariable i t) d + case last s of + (SReturn _) -> return () + _ -> fail $ "Function "++show i++" doesn't end with return statement" + mapM typeCheckStm s + put state + +addFunction :: (MonadState State m) => Func -> m () +addFunction (Func t i d _) = do + s <- get + let m = functions s in case insertLookupWithKey (\k a1 a2 -> a1) i (t, map (\(Decl t i) -> t) d) m of + (Nothing,m') -> modify (\s -> s{ functions = m'}) + _ -> fail $ "Duplicate variable declaration: "++show i