]> ruin.nu Git - proglang.git/commitdiff
typechecker seems to work for function calls
authorMichael Andreen <harv@ruin.nu>
Sat, 11 Mar 2006 13:44:06 +0000 (13:44 +0000)
committerMichael Andreen <harv@ruin.nu>
Sat, 11 Mar 2006 13:44:06 +0000 (13:44 +0000)
Typecheck.hs
examples/func

index 18146506a227857ce792198ba9e8575491c7f553..bfef73a68a318cc46188649474881eab2760c4d0 100644 (file)
@@ -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
index 4abaa7e66fb44ad6d8231cec37e14bf75c3d594f..0f79e78090479f4a8eabe3ff9f79b2662b4392ba 100644 (file)
@@ -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);
+}