Skip to content

Commit

Permalink
Merge pull request #77 from Hoxmot/feature/15-static-typing
Browse files Browse the repository at this point in the history
[#62] Static typing: Lambdas
  • Loading branch information
Hoxmot authored May 15, 2022
2 parents ad209cd + 3fb1f51 commit 7a9927a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ createFuncEnv env ((Arg t v):als) (a:as) = do
arg <- interpretExp a
if typeEq t arg
then createFuncEnv (M.insert v (Var arg) env) als as
else throwError "Types don't match!"
else throwError $ "Types don't match! Expected '" ++ pprintType t
++ "', but got '" ++ pprintResult arg ++ "'!"
createFuncEnv env [] [] = return env
createFuncEnv _ [] _ = throwError "Too many arguments!"
createFuncEnv _ _ _ = throwError "Partial function application is not supported yet!"
Expand All @@ -119,7 +120,8 @@ interpretFuncBody t exps = do
res <- interpretExps exps
if typeEq t res
then return res
else throwError "Types don't match!"
else throwError $ "Types don't match! Expected '" ++ pprintType t
++ "', but got '" ++ pprintResult res ++ "'!"


{- | Interprets an internal function usage in Baalbolge.
Expand Down
19 changes: 19 additions & 0 deletions src/TypeChecker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ checkTypesIFunc iFunc@(BG.IFuncDecl pos t (BG.Var v) (BG.AList _ argsList) exps)
where
argToType (BG.AArg _ at _) = checkTypesType at

{- | For the lambda declaration, we have to make sure that the type returned by the
function body is the same as the declared return type. However, first, we have to make
sure that all the expressions inside the body of the function have the correct type.
The type of the function declaration is the function itself.
-}
checkTypesIFunc iFunc@(BG.ILambda pos t (BG.AList _ argsList) exps) = do
tt <- checkTypesType t
args <- mapM argToType argsList
mem <- get
funcMem <- foldM createFuncMem mem argsList
case runReader (runExceptT $ checkTypesExps exps) funcMem of
Left l -> throwError l
Right ft -> if tt == ft
then return $ TFunc tt args
else throwError $ typesError iFunc "lambda declaration" pos tt ft
where
argToType (BG.AArg _ at _) = checkTypesType at

checkTypesIFunc e = throwError $ "Checking types for internal function: " ++ show e
++ " is not yet implemented"

Expand Down

0 comments on commit 7a9927a

Please sign in to comment.