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)