aboutsummaryrefslogtreecommitdiff
path: root/src/Error.hs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-10-09 07:05:56 +0100
committerAkshay <[email protected]>2020-10-09 07:05:56 +0100
commit06bf4c656377572859846767cb9af5db8fc27893 (patch)
tree489b727cf1e6d31f2b1de66a7ee919c78cca19a5 /src/Error.hs
parent9160b3648a69303c2ed288edec3d8e9bcec52f11 (diff)
use mtl to generate errors
Diffstat (limited to 'src/Error.hs')
-rw-r--r--src/Error.hs31
1 files changed, 31 insertions, 0 deletions
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 @@
1module Error (
2 LispError (..)
3 , LispResult (..)
4 , unwrap
5 ) where
6
7import Control.Monad.Except
8import Parser
9import Text.ParserCombinators.Parsec
10
11data LispError = Parse ParseError
12 | BadForm String Expr
13 | ArgCount Int [Expr]
14 | UnknownFunction String
15 | TypeMismatch String Expr
16
17unwordsList :: [Expr] -> String
18unwordsList = unwords . map show
19
20instance Show LispError where
21 show (Parse e) = "Parser Error: " ++ show e
22 show (BadForm s expr) = "Bad Form: " ++ s ++ ": " ++ show expr
23 show (ArgCount n es) = "Invalid arity, expected " ++ show n ++ ", got values: " ++ unwordsList es
24 show (UnknownFunction fn) = "Unknown reference to function: " ++ fn
25 show (TypeMismatch msg got) = "Type mismatch, expected " ++ msg ++ ", got: " ++ show got
26
27type LispResult = Either LispError
28
29unwrap :: LispResult t -> t
30unwrap (Right v) = v
31unwrap (Left _) = undefined -- should panic