-module Typecheck where
+module Typecheck (typeCheckExp, typeCheckStm, typeCheckVar) where
import Abssyntax
import Control.Monad.State
type Types = Map Ident Type
+inList :: Eq a => a -> [a] -> Bool
+inList _ [] = False
+inList a (x:xs) = if a == x then True else inList a xs
+
typeCheckExp :: (MonadState Types m) => Exp -> m Type
-typeCheckExp (BExp e o e') = do
- TInt <- typeCheckExp e
- TInt <- typeCheckExp e'
- return TBool
-typeCheckExp (OpExp e o e') = do
- TInt <- typeCheckExp e
- TInt <- typeCheckExp e'
- return TInt
+typeCheckExp (BiOpExp e o e') = do
+ t1 <- typeCheckExp e
+ t2 <- typeCheckExp e'
+ if not(t1 == t2) then fail ""
+ else case inList o [Eq,NEq] of
+ True -> return TBool
+ False -> if not(t1 == TInt) then fail ""
+ else case inList o [Plus,Minus,Times,Div] of
+ True -> return TInt
+ False -> return TBool
typeCheckExp (EVar i) = typeCheckVar i
typeCheckExp (EAss i e) = do
a <- typeCheckVar i