From 06bf4c656377572859846767cb9af5db8fc27893 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 9 Oct 2020 11:35:56 +0530 Subject: use mtl to generate errors --- src/Error.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/Error.hs (limited to 'src/Error.hs') diff --git a/src/Error.hs b/src/Error.hs new file mode 100644 index 0000000..276d3e2 --- /dev/null +++ b/src/Error.hs @@ -0,0 +1,31 @@ +module Error ( + LispError (..) + , LispResult (..) + , unwrap + ) where + +import Control.Monad.Except +import Parser +import Text.ParserCombinators.Parsec + +data LispError = Parse ParseError + | BadForm String Expr + | ArgCount Int [Expr] + | UnknownFunction String + | TypeMismatch String Expr + +unwordsList :: [Expr] -> String +unwordsList = unwords . map show + +instance Show LispError where + show (Parse e) = "Parser Error: " ++ show e + show (BadForm s expr) = "Bad Form: " ++ s ++ ": " ++ show expr + show (ArgCount n es) = "Invalid arity, expected " ++ show n ++ ", got values: " ++ unwordsList es + show (UnknownFunction fn) = "Unknown reference to function: " ++ fn + show (TypeMismatch msg got) = "Type mismatch, expected " ++ msg ++ ", got: " ++ show got + +type LispResult = Either LispError + +unwrap :: LispResult t -> t +unwrap (Right v) = v +unwrap (Left _) = undefined -- should panic -- cgit v1.2.3