From 74c984c4c70336e7b7618cd934db819263ceb565 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Sat, 11 Mar 2006 13:44:06 +0000 Subject: [PATCH] typechecker seems to work for function calls --- Typecheck.hs | 14 ++++++++++++++ examples/func | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/Typecheck.hs b/Typecheck.hs index 1814650..bfef73a 100644 --- a/Typecheck.hs +++ b/Typecheck.hs @@ -50,6 +50,20 @@ typeCheckExp (ENeg e) = do 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 + +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 diff --git a/examples/func b/examples/func index 4abaa7e..0f79e78 100644 --- a/examples/func +++ b/examples/func @@ -10,3 +10,23 @@ int fac(int n){ while (n-- > 1) sum = sum * n; return sum; } + +int fib(int n){ + int n1 = 0; + int n2 = 1; + + while (n-- > 0){ + int temp = n1+n2; + n1 = n2; + n2 = temp; + } + return n2; +} + +int func(int a, int b){ + return (a+b); +} + +bool boolfunc(int a, int b){ + return (a == b); +} -- 2.39.2