From: Mart Lubbers Date: Wed, 13 Apr 2016 20:09:12 +0000 (+0200) Subject: Merge branch 'mastert push ' of github.com:dopefishh/cc1516 X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=b0a8d161ec4566b50febd63640fab70f8081c685;hp=72fb877d00efbba5cc4540ee2e76fbb01c6f572d;p=cc1516.git Merge branch 'mastert push ' of github.com:dopefishh/cc1516 --- diff --git a/examples/test.spl b/examples/test.spl index bdcb0ed..b33297c 100644 --- a/examples/test.spl +++ b/examples/test.spl @@ -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 --- 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)