module Typecheck (inferExpr, checkExpr) where import Syntax import Control.Monad type Environment = [(Idnt,Typ)] assertInt TInt = Just () assertInt _ = Nothing assertRec (Record _) = Just () assertRec _ = Nothing assertVar (Variant _ _) = Just () assertVar _ = Nothing assertPtr (TPtr _) = Just () assertPtr _ = Nothing inferExpr :: Expr -> Environment -> Maybe Typ inferExpr (Var name) env = lookup name env inferExpr (IntLit _) _ = Just TInt inferExpr (Record _) _ = undefined inferExpr (Variant _ _) _ = undefined inferExpr (Add e1 e2) env = do checkExpr e1 env TInt checkExpr e1 env TInt return TInt inferExpr (Mult e1 e2) env = do checkExpr e1 env TInt checkExpr e1 env TInt return TInt inferExpr (Neg e) env = do checkExpr e env TInt return TInt inferExpr (Deref e1) env = do t <- inferExpr e1 env assertPtr t return t checkExpr :: Expr -> Environment -> Typ -> Maybe () checkExpr e env t = do t' <- inferExpr e env guard (t==t') return ()