diff options
author | Akshay <[email protected]> | 2020-10-13 13:37:08 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-10-13 13:37:08 +0100 |
commit | 8641ce3e1a730c8195e5a74fabef8814f43b05f8 (patch) | |
tree | cdacd671ba7445c4823ffb8bbccb20de6e56f655 /src/Error/Base.hs | |
parent | 4842ab6362077a62893ac79b752c7da5578292b9 (diff) |
refactor Error into submodules
Diffstat (limited to 'src/Error/Base.hs')
-rw-r--r-- | src/Error/Base.hs | 37 |
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 @@ | |||
1 | module Error.Base ( | ||
2 | LispError (..) | ||
3 | , LispResult (..) | ||
4 | , unwrap | ||
5 | ) where | ||
6 | |||
7 | import Control.Monad.Except | ||
8 | import Data.List (intercalate, nub) | ||
9 | import Parser | ||
10 | import Text.Parsec | ||
11 | import Text.Parsec.Error | ||
12 | import Text.Parsec.Pos | ||
13 | import Text.Parsec.String (Parser) | ||
14 | import Text.ParserCombinators.Parsec | ||
15 | |||
16 | data LispError = Parse ParseError | ||
17 | | BadForm String Expr | ||
18 | | ArgCount Int [Expr] | ||
19 | | UnknownFunction String | ||
20 | | TypeMismatch String Expr | ||
21 | |||
22 | unwordsList :: [Expr] -> String | ||
23 | unwordsList = unwords . map show | ||
24 | |||
25 | instance 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 | |||
32 | type LispResult = Either LispError | ||
33 | |||
34 | unwrap :: LispResult t -> t | ||
35 | unwrap (Right v) = v | ||
36 | unwrap (Left _) = undefined -- should panic | ||
37 | |||