-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSExp.hs
62 lines (46 loc) · 1.05 KB
/
SExp.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
module SExp where
import Text.PrettyPrint
data SExp
= SymS Symbol
| LitS Literal
| ListS [SExp]
| DirectiveS ReaderDirective
| NilS
deriving (Eq)
instance Show SExp where
show = showSexp
data Literal
= IntL Integer
| StringL String
deriving (Show, Eq)
type ReaderDirective = ()
type Symbol = String
pprint :: SExp -> Doc
pprint (ListS es) = parens $ sep $ map pprint es
pprint (LitS lit) = case lit of
StringL s -> text $ show s
IntL n -> integer n
pprint (SymS sym) = text sym
pprint (DirectiveS d) = text $ "DIRECTIVE<" ++ show d ++ ">"
pprint NilS = text "<nil>"
showSexp :: SExp -> String
showSexp = render . pprint
sym :: String -> SExp
sym = SymS
str :: String -> SExp
str s = LitS $ StringL s
asStr :: SExp -> String
asStr (LitS (StringL str)) = str
asStr _ = ""
car :: SExp -> SExp
car (ListS (x:y)) = x
car _ = NilS
cdr :: SExp -> SExp
cdr (ListS (x:y)) = ListS y
cdr _ = NilS
cons :: SExp -> SExp -> SExp
cons a d = ListS (a : unListS d)
unListS :: SExp -> [SExp]
unListS (ListS x) = x
unListS _ = []
-- vim: sw=2 ts=2 sts=2