From 0f420d3c45aa26b770bf2bda3588ffcc2ed574cf Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Wed, 13 Apr 2016 22:09:04 +0200 Subject: [PATCH] . --- examples/test.spl | 9 +++++++-- sem.icl | 12 +++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) 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 a041cee..b74ee76 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) -- 2.20.1