aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-08-21 08:07:52 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-08-21 08:07:52 +0100
commitccb2e6e02f39f6cf5f7beb4aa64bec948a8c7edf (patch)
tree0c5f4964972cdfb8d03f0d2e4100e04c9fc3e300
parent0961ef6728abea4f3926fbf34539f4a98583d0c7 (diff)
downloadmicro-lang-hs-ccb2e6e02f39f6cf5f7beb4aa64bec948a8c7edf.tar.gz
micro-lang-hs-ccb2e6e02f39f6cf5f7beb4aa64bec948a8c7edf.zip
Qualified undefined variable error, added test
-rw-r--r--src/Compiler.hs2
-rw-r--r--src/Error.hs3
-rw-r--r--test/Language.hs11
3 files changed, 13 insertions, 3 deletions
diff --git a/src/Compiler.hs b/src/Compiler.hs
index 0228b90..bdbc5f3 100644
--- a/src/Compiler.hs
+++ b/src/Compiler.hs
@@ -131,7 +131,7 @@ compile x = do
(ev, errs) <- get
case getSym ev ident of
Just (_, t, _) -> return $ Right $ Just t
- Nothing -> addError $ Error GenericError ("undefined variable \"" ++ ident ++ "\"") pos
+ Nothing -> addError $ Error Undefined ("undefined variable \"" ++ ident ++ "\"") pos
compileAll :: [A.Expr] -> State CompState CompResult
compileAll (x : xs) = do
diff --git a/src/Error.hs b/src/Error.hs
index 724f57c..40bb046 100644
--- a/src/Error.hs
+++ b/src/Error.hs
@@ -4,7 +4,7 @@ import Data.List (sort)
import Text.Parsec (SourcePos, errorPos)
import Text.Parsec.Error (ParseError, errorMessages, showErrorMessages)
-data ErrorType = GenericError | TypeError | UnexpectedReturn | AlreadyDefined | NonCallable deriving (Show)
+data ErrorType = GenericError | TypeError | UnexpectedReturn | AlreadyDefined | NonCallable | Undefined deriving (Show)
instance Enum ErrorType where
fromEnum GenericError = 0
@@ -12,6 +12,7 @@ instance Enum ErrorType where
fromEnum UnexpectedReturn = 2
fromEnum AlreadyDefined = 3
fromEnum NonCallable = 4
+ fromEnum Undefined = 5
toEnum _ = error "toEnum is undefined for Error"
data Error = Error ErrorType String SourcePos
diff --git a/test/Language.hs b/test/Language.hs
index 7d814a7..e52bb3b 100644
--- a/test/Language.hs
+++ b/test/Language.hs
@@ -226,6 +226,14 @@ testCase17 =
\def fn(a: u8): u8 { return a(); }\n"
E.NonCallable
+testCase18 =
+ TestLabel "undefined variable" $
+ TestCase $
+ expectError
+ "module main\n\
+ \def fn(a: u8): u8 { return undef; }\n"
+ E.Undefined
+
language =
[ testCase1,
testCase2,
@@ -243,5 +251,6 @@ language =
testCase14,
testCase15,
testCase16,
- testCase17
+ testCase17,
+ testCase18
]