aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-08-14 09:06:05 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-08-14 09:06:05 +0100
commit79d4e3e42cd4a9e668790f8a2e7a243523a14c88 (patch)
treea5b9d53715e2e646c72a0ea975542d84f2ab5909 /src
parent079d5f51a63e409f83c5414fd848a90a0b8def62 (diff)
downloadmicro-lang-hs-79d4e3e42cd4a9e668790f8a2e7a243523a14c88.tar.gz
micro-lang-hs-79d4e3e42cd4a9e668790f8a2e7a243523a14c88.zip
Extract code to add errors to the state
Diffstat (limited to 'src')
-rw-r--r--src/Compiler.hs22
1 files changed, 11 insertions, 11 deletions
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