8 | data SExp = SExpList (List SExp)
11 | | IntegerAtom Integer
14 | escape : String -> String
15 | escape = pack . concatMap escapeChar . unpack
17 | escapeChar : Char -> List Char
18 | escapeChar '\\' = ['\\', '\\']
19 | escapeChar '"' = ['\\', '\"']
24 | show (SExpList xs) = assert_total $
"(" ++ joinBy " " (map show xs) ++ ")"
25 | show (StringAtom str) = "\"" ++ escape str ++ "\""
26 | show (BoolAtom b) = ":" ++ show b
27 | show (IntegerAtom i) = show i
28 | show (SymbolAtom s) = ":" ++ s
31 | interface SExpable a where
36 | interface FromSExpable a where
37 | fromSExp : SExp -> Maybe a
48 | FromSExpable Bool where
49 | fromSExp (BoolAtom b) = Just b
50 | fromSExp _ = Nothing
53 | SExpable String where
57 | FromSExpable String where
58 | fromSExp (StringAtom s) = Just s
59 | fromSExp _ = Nothing
62 | SExpable Integer where
63 | toSExp = IntegerAtom
66 | FromSExpable Integer where
67 | fromSExp (IntegerAtom a) = Just a
68 | fromSExp _ = Nothing
72 | toSExp = IntegerAtom . cast
75 | FromSExpable Int where
76 | fromSExp a = do Just $
cast {from = Integer }$
!(fromSExp a)
80 | toSExp = IntegerAtom . cast
83 | FromSExpable Nat where
84 | fromSExp a = do Just $
cast {from = Integer }$
!(fromSExp a)
87 | (SExpable a, SExpable b) => SExpable (a, b) where
90 | SExpList xs => SExpList (toSExp x :: xs)
91 | y' => SExpList [toSExp x, y']
94 | (FromSExpable a, FromSExpable b) => FromSExpable (a, b) where
95 | fromSExp (SExpList xs) = case xs of
96 | [x,y] => do pure $
(!(fromSExp x), !(fromSExp y))
97 | (x :: xs) => do pure $
(!(fromSExp x), !(fromSExp $
SExpList xs))
99 | fromSExp _ = Nothing
102 | SExpable a => SExpable (List a) where
104 | = SExpList (map toSExp xs)
107 | FromSExpable a => FromSExpable (List a) where
108 | fromSExp (SExpList sexps) = traverse fromSExp sexps
109 | fromSExp _ = Nothing
112 | SExpable a => SExpable (List1 a) where
114 | = SExpList (map toSExp (toList xs))
117 | FromSExpable a => FromSExpable (List1 a) where
118 | fromSExp (SExpList (sexp :: sexps)) = traverse fromSExp (sexp ::: sexps)
119 | fromSExp _ = Nothing
122 | SExpable a => SExpable (Maybe a) where
123 | toSExp Nothing = SExpList []
124 | toSExp (Just x) = toSExp x