diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-08-16 23:37:40 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-08-16 23:37:40 +0100 |
commit | 007b1e966d4234f2fbdc8f21d53b27dec0060b5e (patch) | |
tree | 99e02aa703470bff74f4711d1c3bd91379b897e5 | |
parent | 7ac5c089d447c11bb9c9152bb30e1cff5b96267e (diff) | |
download | micro-lang-hs-007b1e966d4234f2fbdc8f21d53b27dec0060b5e.tar.gz micro-lang-hs-007b1e966d4234f2fbdc8f21d53b27dec0060b5e.zip |
Unexpected return error
-rw-r--r-- | src/Compiler.hs | 2 | ||||
-rw-r--r-- | src/Error.hs | 3 | ||||
-rw-r--r-- | test/Language.hs | 27 |
3 files changed, 30 insertions, 2 deletions
diff --git a/src/Compiler.hs b/src/Compiler.hs index da25c5d..413a041 100644 --- a/src/Compiler.hs +++ b/src/Compiler.hs @@ -121,7 +121,7 @@ compile x = do (A.Return value pos) -> do (ev, errs) <- get case getSyml ev "$fn$" of - Nothing -> addError $ Error "return without function call" pos + Nothing -> addError $ UnexpectedReturn "return without function call" pos Just (_, A.FuncType _ rtyp, _) -> do r <- typecheckReturn value rtyp case r of diff --git a/src/Error.hs b/src/Error.hs index b528f9e..1363120 100644 --- a/src/Error.hs +++ b/src/Error.hs @@ -6,6 +6,7 @@ import Text.Parsec.Error (ParseError, errorMessages, showErrorMessages) data Error = TypeError String SourcePos + | UnexpectedReturn String SourcePos | Error String SourcePos deriving (Eq) @@ -15,6 +16,8 @@ instance Show Error where -- XXX: can we do this differently? show (TypeError message pos) = show pos ++ " error: " ++ message + show (UnexpectedReturn message pos) = + show pos ++ " error: " ++ message showParserError :: ParseError -> String showParserError error = diff --git a/test/Language.hs b/test/Language.hs index 7892c16..9da3124 100644 --- a/test/Language.hs +++ b/test/Language.hs @@ -183,4 +183,29 @@ testCase12 = Nothing -> assertFailure "expected error, didn't happen" Just (E.TypeError _ _) -> return $ () -language = [testCase1, testCase2, testCase3, testCase4, testCase5, testCase6, testCase7, testCase8, testCase9, testCase10, testCase11, testCase12] +testCase13 = + TestLabel "return without function" $ + TestCase $ do + e <- + expectError + "module main\n\ + \return;" + case e of + Nothing -> assertFailure "expected error, didn't happen" + Just (E.UnexpectedReturn _ _) -> return $ () + +language = + [ testCase1, + testCase2, + testCase3, + testCase4, + testCase5, + testCase6, + testCase7, + testCase8, + testCase9, + testCase10, + testCase11, + testCase12, + testCase13 + ] |