From 8641ce3e1a730c8195e5a74fabef8814f43b05f8 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 13 Oct 2020 18:07:08 +0530 Subject: refactor Error into submodules --- src/Error/Base.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/Error/Base.hs (limited to 'src/Error/Base.hs') diff --git a/src/Error/Base.hs b/src/Error/Base.hs new file mode 100644 index 0000000..b6ae9a3 --- /dev/null +++ b/src/Error/Base.hs @@ -0,0 +1,37 @@ +module Error.Base ( + LispError (..) + , LispResult (..) + , unwrap + ) where + +import Control.Monad.Except +import Data.List (intercalate, nub) +import Parser +import Text.Parsec +import Text.Parsec.Error +import Text.Parsec.Pos +import Text.Parsec.String (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 value(s): " ++ unwordsList es + show (UnknownFunction fn) = "Cannot apply 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