-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispVal.hs
90 lines (81 loc) · 2.14 KB
/
LispVal.hs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module LispVal (
LispVal(
LispAtom,
LispList,
LispDottedList,
LispNumber,
LispChar,
LispString,
LispBool,
PrimitiveFunc,
Func,
IOFunc,
Port
),
ThrowsLispError,
IOThrowsLispError,
makeFunc,
makeNormalFunc,
makeVargsFunc
) where
import IError
import Environment
import Util
import Control.Monad.Error
import System.IO
data LispVal = LispAtom String
| LispList [LispVal]
| LispDottedList [LispVal] LispVal
| LispNumber Integer
| LispChar Char
| LispString String Bool
| LispBool Bool
| PrimitiveFunc ([LispVal] -> ThrowsLispError LispVal)
| Func
{ params :: [String]
, vararg :: (Maybe String)
, body :: [LispVal]
, closure :: Environment LispVal
}
| IOFunc ([LispVal] -> IOThrowsLispError LispVal)
| Port Handle
type ThrowsLispError a = ThrowsError LispVal a
type IOThrowsLispError a = IOThrowsError LispVal a
showVal :: LispVal -> String
showVal (LispChar char) = "#\\" ++ [char]
showVal (LispString contents _) = "\"" ++ contents ++ "\""
showVal (LispAtom name) = name
showVal (LispNumber contents) = show contents
showVal (LispBool True) = "#t"
showVal (LispBool False) = "#f"
showVal (LispList contents) = "(" ++ unwordsList contents ++ ")"
showVal (LispDottedList head tail) = "(" ++ h ++ " . " ++ t where
h = unwordsList head
t = showVal tail
showVal (PrimitiveFunc _) = "<primitive>"
showVal (Func {params = args, vararg = varargs, body = body, closure = env}) =
"(lambda (" ++ unwords (map show args) ++ vaStr ++ ") ...)" where
vaStr = case varargs of
Nothing -> ""
Just arg -> " . " ++ arg
showVal (IOFunc _) = "<IO primitive>"
showVal (Port _) = "<IO port>"
instance Show LispVal where show = showVal
makeFunc :: (Maybe String)
-> (Environment LispVal)
-> [LispVal]
-> [LispVal]
-> IOThrowsLispError LispVal
makeFunc vargs env params body =
return $ Func (map showVal params) vargs body env
makeNormalFunc :: (Environment LispVal)
-> [LispVal]
-> [LispVal]
-> IOThrowsLispError LispVal
makeNormalFunc = makeFunc Nothing
makeVargsFunc :: LispVal
-> (Environment LispVal)
-> [LispVal]
-> [LispVal]
-> IOThrowsLispError LispVal
makeVargsFunc = makeFunc . Just . showVal