From af1523e4b51f432d3df4d2e2ae57de95e3440d12 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sun, 29 Jan 2017 09:17:54 +0100 Subject: Do function argument typechecking correctly --- check.hs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/check.hs b/check.hs index 1a0a24c..88b20a9 100644 --- a/check.hs +++ b/check.hs @@ -137,20 +137,20 @@ typeCheck (Program decls) = Program <$> mapM (goD topLevelNames) decls goE _ (ExLit l@(LitString _) _) = return $ ExLit l $ Just (TypePtr (TypeInt 8)) goE names (ExLit l@(LitVar n) _) = maybe (Left $ "Undefined variable '" ++ n ++ "'") (return . ExLit l . Just) (Map.lookup n names) - goE names (ExLit l@(LitCall n args) _) = do + goE names (ExLit (LitCall n args) _) = do ft <- maybe (Left $ "Unknown function '" ++ n ++ "'") return $ Map.lookup n functionTypes rargs <- mapM (goE names) args when (length rargs /= length (snd ft)) $ Left ("Expected " ++ show (length (snd ft)) ++ "arguments to " ++ "function '" ++ n ++ "', but got " ++ show (length rargs)) >> return () - flip mapM_ rargs $ - \a -> let argtype = fromJust (exTypeOf a) - in if canConvert argtype (fst ft) - then return a - else Left $ "Cannot convert type '" ++ pshow argtype ++ "' to '" ++ pshow (fst ft) - ++ "' in call of function '" ++ pshow n ++ "'" - return $ ExLit l (Just (fst ft)) + flip mapM_ (zip rargs [0..]) $ + \(a,i) -> let argtype = fromJust (exTypeOf a) + in if canConvert argtype (snd ft !! i) + then return a + else Left $ "Cannot convert type '" ++ pshow argtype ++ "' to '" ++ pshow (fst ft) + ++ "' in call of function '" ++ pshow n ++ "'" + return $ ExLit (LitCall n rargs) (Just (fst ft)) goE names (ExBinOp bo e1 e2 _) = do re1 <- goE names e1 re2 <- goE names e2 -- cgit v1.2.3-70-g09d2