From b19a4a35db4cd951c52e179f3340518c9e2dcc1e Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 16 Oct 2020 18:54:46 +0530 Subject: add initial support for variable definition --- src/Error/Base.hs | 24 ++++++++++++++---------- src/Error/Pretty.hs | 9 ++++----- 2 files changed, 18 insertions(+), 15 deletions(-) (limited to 'src/Error') diff --git a/src/Error/Base.hs b/src/Error/Base.hs index d7b685c..ef32d52 100644 --- a/src/Error/Base.hs +++ b/src/Error/Base.hs @@ -1,8 +1,7 @@ -module Error.Base ( - LispError (..) - , LispResult (..) - , unwrap - ) where +module Error.Base ( LispError (..) + , LispResult (..) + , unwrap + ) where import Control.Monad.Except import Parser @@ -15,19 +14,24 @@ data LispError = Parse ParseError | ArgCount String Int [Expr] | UnknownFunction String | TypeMismatch String Expr + | UnboundVariable String unwordsList :: [Expr] -> String unwordsList = unwords . map show +literal :: String -> String +literal v = "`" <> v <> "`" + instance Show LispError where show (Parse e) = "Parser Error: " ++ show e - show (BadForm s expr) = "Bad Form: " ++ s ++ ": " ++ show expr + show (BadForm s expr) = "Bad Form: " ++ literal s ++ ": " ++ show expr -- TODO: clean this up show (ArgCount fn n es) - | null es = "Invalid arity, `" ++ fn ++ "` expects " ++ show n ++ " or more expression(s)!" - | otherwise = "Invalid arity, `" ++ fn ++ "` expects " ++ show n ++ " or more expression(s), got value(s): " ++ unwordsList es - show (UnknownFunction fn) = "Cannot apply function: " ++ fn - show (TypeMismatch msg got) = "Type mismatch, expected " ++ msg ++ ", got: " ++ show got + | null es = "Invalid arity, " ++ literal fn ++ " expects " ++ show n ++ " or more expression(s)!" + | otherwise = "Invalid arity, " ++ literal fn ++ " expects " ++ show n ++ " or more expression(s), got value(s): " ++ unwordsList es + show (UnknownFunction fn) = "Cannot apply function: " ++ literal fn + show (TypeMismatch msg got) = "Type mismatch, expected " ++ literal msg ++ ", got: " ++ show got + show (UnboundVariable name) = "Possibly unbound variable: " ++ literal name type LispResult = Either LispError diff --git a/src/Error/Pretty.hs b/src/Error/Pretty.hs index 44601d6..a90131b 100644 --- a/src/Error/Pretty.hs +++ b/src/Error/Pretty.hs @@ -1,8 +1,7 @@ -module Error.Pretty ( - showError - , Options (..) - , defaults - ) where +module Error.Pretty ( showError + , Options (..) + , defaults + ) where import Data.List (intercalate, nub) import Error.Base (LispError (..)) -- cgit v1.2.3