From 79d4e3e42cd4a9e668790f8a2e7a243523a14c88 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 14 Aug 2022 09:06:05 +0100 Subject: Extract code to add errors to the state --- src/Compiler.hs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Compiler.hs b/src/Compiler.hs index c373988..e694d71 100644 --- a/src/Compiler.hs +++ b/src/Compiler.hs @@ -28,6 +28,14 @@ foldlEither fn init xs = init xs +-- | @addError error@ adds @error@ to the state and returns no type to allow +-- the compilation to continue. +addError :: Error -> State CompState CompResult +addError e = do + (ev, errs) <- get + put (ev, e : errs) + return $ Right Nothing + compile :: A.Expr -> State CompState CompResult compile x = do case x of @@ -56,15 +64,9 @@ compile x = do case r of p@(Right (Just (A.FuncType params _))) -> if length args /= length params - then do - (ev, errs) <- get - put (ev, Error ("invalid number of arguments in function call") pos : errs) - return $ Right Nothing + then addError $ Error ("invalid number of arguments in function call") pos else return $ p - Right _ -> do - (ev, errs) <- get - put (ev, Error ("non callable value in function call") pos : errs) - return $ Right Nothing + Right _ -> addError $ Error ("non callable value in function call") pos Left r -> return $ Right Nothing (A.Return value pos) -> case value of Just v -> compile v @@ -73,9 +75,7 @@ compile x = do (ev, errs) <- get case getSym ev ident of Just (_, t, _) -> return $ Right $ Just t - Nothing -> do - put (ev, Error ("undefined variable \"" ++ ident ++ "\"") pos : errs) - return $ Right Nothing + Nothing -> addError $ Error ("undefined variable \"" ++ ident ++ "\"") pos _ -> return $ Right Nothing compileAll :: [A.Expr] -> State CompState CompResult -- cgit v1.2.3