aboutsummaryrefslogtreecommitdiff
path: root/src/Error.hs
blob: bfc8d14fad5af09eb00eba4c31d3eabaeabe9870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 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