-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLisp.hs
More file actions
360 lines (303 loc) · 10.4 KB
/
Lisp.hs
File metadata and controls
360 lines (303 loc) · 10.4 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
-- This file is part of KSQuant2.
-- Copyright (c) 2010 - 2011, Kilian Sprotte. All rights reserved.
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Lisp (Sexp
,toSexp
,fromSexp
,printSexp
,LispVal(..)
,mapcar'
,mapcarUpToPlist
,readLisp
,readLisp'
,cons
,clNull
,car
,cdr
,getf
,getf'
,atom
,fromLispList
,parseLisp
,printLisp
,propertyListP
,lispEscapeString
,nil
,n60
,lispToRational
,rationalToLisp
,ensureList)
where
import qualified Types as T (WRat, WInt)
import Text.ParserCombinators.Parsec (Parser
,parse
,anyToken
,char
,space
,letter
,digit
,oneOf
,noneOf
,many
,many1
,skipMany
,skipMany1
,endBy
,endBy1
,eof
,(<|>)
,try)
import Data.Char (toUpper)
import Data.List (elemIndex)
import Data.Maybe (fromMaybe)
import Data.Ratio (numerator, denominator)
data LispVal = LispInteger Integer
| LispKeyword String
| LispSymbol String
| LispString String
| LispFloat Rational
| LispRatio Rational
| LispList [LispVal]
deriving (Eq)
instance Show LispVal where
show x = "readLisp \"" ++ printLisp x ++ "\""
lispEscapeString :: String -> String
lispEscapeString s = "\"" ++ rec s ++ "\""
where
rec [] = []
rec (x:xs) | x == '\"' = "\\\"" ++ rec xs
| x == '\\' = "\\\\" ++ rec xs
| otherwise = x : rec xs
-- |Print a LispVal to String.
printLisp :: LispVal -> String
printLisp (LispInteger x) = show x
printLisp (LispFloat x) = show x'
where x' = fromRational x
x' :: Double
printLisp (LispRatio r) = show (numerator r) ++ "/" ++ show (denominator r)
printLisp (LispKeyword x) = ':' : x
printLisp (LispSymbol x) = x
printLisp (LispString x) = lispEscapeString x
printLisp (LispList xs) =
"(" ++ unwords (map printLisp xs) ++ ")"
comment :: Parser ()
comment = char ';' >> many (noneOf "\n") >> return ()
whitespace :: Parser ()
whitespace = skipMany (skipMany1 space <|> comment)
symbol :: Parser Char
symbol = oneOf "/!$%-"
parseKeyword :: Parser LispVal
parseKeyword =
do
_ <- char ':'
s <- many1 (letter <|> symbol <|> digit)
return (LispKeyword (map toUpper s))
parseSymbol :: Parser LispVal
parseSymbol =
do
s <- many1 (letter <|> symbol <|> digit)
let su = map toUpper s
return (case su of
"NIL" -> LispList []
_ -> LispSymbol su)
parseString :: Parser LispVal
parseString =
do
_ <- char '\"'
s <- many ((char '\\' >> anyToken) <|> noneOf "\"")
_ <- char '\"'
return (LispString s)
parseSign :: (Num a) => Parser a
parseSign = (char '-' >> return (-1)) <|>
(char '+' >> return 1) <|>
return 1
parseInteger :: Parser LispVal
parseInteger =
do
sign <- parseSign
ds <- many1 digit
return $ (LispInteger . (*sign) . read) ds
parseFloat :: Parser LispVal
parseFloat =
do
sign <- parseSign
ds <- many1 digit
_ <- char '.'
ds2 <- many1 digit
let ds' = read ds :: Integer
let ds2' = read ds2 :: Integer
let n = fromInteger ds'
let numerator = fromInteger ds2'
let denominator = 10^^(fromIntegral (length ds2) :: Integer)
let r = (n + (numerator / denominator)) :: Rational
(LispInteger e) <- (oneOf "sfdleSFDLE" >> parseInteger) <|>
return (LispInteger 0)
return $ (LispFloat . (*10^^e) . (*sign)) r
parseRatio :: Parser LispVal
parseRatio =
do
sign <- (char '-' >> return (-1)) <|>
return 1
numerator <- many1 digit
_ <- char '/'
denominator <- many1 digit
let numerator' = read numerator :: Integer
let denominator' = read denominator :: Integer
return $ (LispRatio . (*sign)) (fromIntegral numerator' / fromIntegral denominator')
parseList :: Parser LispVal
parseList =
do
char '(' >> whitespace
elts <- endBy parseVal whitespace
_ <- char ')'
return $ LispList elts
parseVal :: Parser LispVal
parseVal = parseKeyword <|>
parseList <|>
parseString <|>
try parseFloat <|>
try parseRatio <|>
parseInteger <|>
parseSymbol
parseValsAndEof :: Parser [LispVal]
parseValsAndEof = do
whitespace
xs <- endBy1 parseVal whitespace
eof
return xs
-- | Parse a String to either a list of LispVals (the string can
-- | contain more than one form), or to ParseError.
parseLisp :: String -> Either String [LispVal]
parseLisp s = case parse parseValsAndEof "" s of
Right x -> Right x
Left err -> Left $ "parse error " ++ show err
readLisp :: String -> Either String LispVal
readLisp s = case parseLisp s of
Right [x] -> Right x
Right _ -> Left "readLisp: expecting only a single form"
Left _ -> Left $ "readLisp: cannot parse '" ++ s ++ "'"
readLisp' :: String -> LispVal
readLisp' s = case readLisp s of
Right x -> x
Left x -> error x
----------------------------------------------
listp :: LispVal -> Bool
listp (LispList _) = True
listp _ = False
atom :: LispVal -> Bool
atom = not . listp
clNull :: LispVal -> Bool
clNull (LispSymbol "NIL") = True
clNull (LispList []) = True
clNull _ = False
cons :: LispVal -> LispVal -> LispVal
cons x (LispList ys) = LispList (x:ys)
cons x y =
error ("cons `" ++ show x ++ "' to `" ++ show y ++ "'")
car :: LispVal -> LispVal
car (LispList (x:_)) = x
car x = error $ "car on '" ++ show x ++ "'"
cdr :: LispVal -> LispVal
cdr (LispList (_:xs)) = LispList xs
cdr x = error $ "cdr on '" ++ show x ++ "'"
fromLispList :: LispVal -> [LispVal]
fromLispList (LispList xs) = xs
fromLispList _ = error "fromLispList: not a list"
-- mapcar :: (LispVal -> LispVal) -> LispVal -> LispVal
-- mapcar _ (LispList []) = LispList []
-- mapcar f xs@(LispList _) = f a `cons` mapcar f b
-- where a = car xs
-- b = cdr xs
-- mapcar _ _ = error "mapcar: not a list"
mapcar' :: (LispVal -> a) -> LispVal -> [a]
mapcar' _ (LispList []) = []
mapcar' f xs@(LispList _) = f a : mapcar' f b
where a = car xs
b = cdr xs
mapcar' _ _ = error "mapcar': not a list"
mapcarUpToPlist :: (LispVal -> a) -> LispVal -> ([a], LispVal)
mapcarUpToPlist _ (LispList []) = ([], LispList [])
mapcarUpToPlist _ xs@(LispList _) | propertyListP xs = ([], xs)
mapcarUpToPlist f xs@(LispList _) = (f a : ys, plist)
where a = car xs
b = cdr xs
(ys,plist) = mapcarUpToPlist f b
mapcarUpToPlist _ _ = error "mapcarUpToPlist: not a list"
keywordp :: LispVal -> Bool
keywordp (LispKeyword _) = True
keywordp _ = False
propertyListP :: LispVal -> Bool
propertyListP (LispList xs) = (even . length) xs &&
all keywordp (everySecond xs)
where everySecond [] = []
everySecond (a:_:ys) = a : everySecond ys
everySecond [_] = error "propertyListP: is this really a plist?"
propertyListP _ = False
getf :: LispVal -> LispVal -> Maybe LispVal
getf xs@(LispList _) field | propertyListP xs =
let xs' = fromLispList xs
in do
index <- elemIndex field xs'
return (xs'!!(index+1))
getf x y =
error ("getf `" ++ show x ++ "' to `" ++ show y ++ "'")
-- | getf with default value
getf' :: LispVal -> LispVal -> LispVal -> LispVal
getf' list field def = fromMaybe def (getf list field)
-- minus :: LispVal -> LispVal
-- minus (LispInteger x) = LispInteger (-x)
-- minus (LispFloat x) = LispFloat (-x)
-- minus _ = error "minus"
----------------------------------------------------
class Sexp a where
toSexp :: a -> LispVal
fromSexp :: LispVal -> a
printSexp :: a -> String
printSexp = printLisp . toSexp
instance Sexp LispVal where
toSexp = id
fromSexp = id
instance Sexp T.WInt where
toSexp = LispInteger
fromSexp (LispInteger x) = x
fromSexp _ = error "fromSexp: not (LispInteger x)"
instance Sexp T.WRat where
toSexp r | denominator r == 1 = LispInteger $ numerator r
| otherwise = LispRatio r
fromSexp (LispRatio x) = x
fromSexp (LispFloat x) = x
fromSexp (LispInteger x) = fromInteger x
fromSexp x = error $ "fromSexp cannot interpret as WRat: " ++ show x
instance Sexp [LispVal] where
toSexp = LispList
fromSexp (LispList xs) = xs
fromSexp _ = error "fromSexp: not (LispList xs)"
instance Sexp [T.WRat] where
toSexp = toSexp . map toSexp
fromSexp (LispList xs) = map fromSexp xs
fromSexp _ = error "fromSexp: not (LispList xs)"
n60 :: LispVal
n60 = readLisp' "(60)"
nil :: LispVal
nil = readLisp' "()"
lispToRational :: LispVal -> T.WRat
lispToRational (LispInteger x) = fromInteger x
lispToRational (LispRatio x) = x
lispToRational (LispFloat x) = x
lispToRational x = error $ "cannot convert to rational " ++ show x
rationalToLisp :: T.WRat -> LispVal
rationalToLisp = LispRatio
ensureList :: LispVal -> LispVal
ensureList l@(LispList _) = l
ensureList x = LispList [x]