Merge branch 'mastert push ' of github.com:dopefishh/cc1516
authorMart Lubbers <mart@martlubbers.net>
Wed, 13 Apr 2016 20:09:12 +0000 (22:09 +0200)
committerMart Lubbers <mart@martlubbers.net>
Wed, 13 Apr 2016 20:09:12 +0000 (22:09 +0200)
examples/test.spl
sem.icl

index bdcb0ed..b33297c 100644 (file)
@@ -1,10 +1,15 @@
 Bool n = True;
 
-test(x) {
-       return f(x);
+test(x) :: Int -> Int {
+       var y = test(x+1);
+       return y;
 }
+
 f(n) :: Int -> Int{
        var m = n+1;
        return n;
 }
 
+test2(x) {
+       return f(x);
+}
diff --git a/sem.icl b/sem.icl
index 43df3a3..21fd235 100644 (file)
--- a/sem.icl
+++ b/sem.icl
@@ -54,15 +54,21 @@ semFunDecl fd=:(FunDecl p f args mt vds stmts) =
        matchFunctions args ft >>= \tres->
     mapM semVarDecl vds >>= \newvds->
     mapM (checkStmt tres) stmts >>= \newstmts->
-       pure IntType >>= \returntype->
        case mt of
-               Nothing = reconstructType args returntype 
-               >>= \ftype->restoreGamma gamma 
+               Nothing = inferReturnType stmts
+                       >>= \returntype->reconstructType args returntype 
+                       >>= \ftype->restoreGamma gamma 
                        >>| putIdent f ftype >>| pure (
                        FunDecl p f args (Just ftype) newvds newstmts) 
                Just t = restoreGamma gamma 
                        >>| pure (FunDecl p f args mt newvds newstmts) 
 
+inferReturnType :: [Stmt] -> Env Type
+inferReturnType [] = pure VoidType
+inferReturnType [ReturnStmt (Just t):rest] = typeExpr t
+inferReturnType [ReturnStmt _:rest] = pure VoidType
+inferReturnType [_:rest] = inferReturnType rest
+
 reconstructType :: [String] Type -> Env Type
 reconstructType [] t = pure t
 reconstructType [x:xs] t = gets (\(st, r)->'Map'.get x st)