aboutsummaryrefslogtreecommitdiff
path: root/src/Error/Base.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Error/Base.hs')
-rw-r--r--src/Error/Base.hs37
1 files changed, 37 insertions, 0 deletions
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 @@
1module Error.Base (
2 LispError (..)
3 , LispResult (..)
4 , unwrap
5 ) where
6
7import Control.Monad.Except
8import Data.List (intercalate, nub)
9import Parser
10import Text.Parsec
11import Text.Parsec.Error
12import Text.Parsec.Pos
13import Text.Parsec.String (Parser)
14import Text.ParserCombinators.Parsec
15
16data LispError = Parse ParseError
17 | BadForm String Expr
18 | ArgCount Int [Expr]
19 | UnknownFunction String
20 | TypeMismatch String Expr
21
22unwordsList :: [Expr] -> String
23unwordsList = unwords . map show
24
25instance Show LispError where
26 show (Parse e) = "Parser Error: " ++ show e
27 show (BadForm s expr) = "Bad Form: " ++ s ++ ": " ++ show expr
28 show (ArgCount n es) = "Invalid arity, expected " ++ show n ++ ", got value(s): " ++ unwordsList es
29 show (UnknownFunction fn) = "Cannot apply function: " ++ fn
30 show (TypeMismatch msg got) = "Type mismatch, expected " ++ msg ++ ", got: " ++ show got
31
32type LispResult = Either LispError
33
34unwrap :: LispResult t -> t
35unwrap (Right v) = v
36unwrap (Left _) = undefined -- should panic
37