-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisp.lua
454 lines (318 loc) · 8.4 KB
/
lisp.lua
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
local old_table = table
local old_string = string
local old_tostring = tostring
local old_type = type
local old_getmetatable = getmetatable
local old_setmetatable = setmetatable
local old_select = select
local _ENV = {}
_ENV.table = old_table
_ENV.getmetatable = old_getmetatable
_ENV.setmetatable = old_setmetatable
_ENV.string = old_string
_ENV.tostring = old_tostring
_ENV.type = old_type
_ENV.select = old_select
-------------------------------------------------------------------------
-- -- meta-methods for display -- --
empty_list = {}
local function empty_list_to_string(empty_list)
return "()"
end
local empty_list_meta = {
__tostring = empty_list_to_string
}
setmetatable(empty_list, empty_list_meta)
function list_tostring(list, wrap)
-- treat "list" as custom data with a __tostring metamethod if
-- both car and cdr are nil
--
if type(list) ~= "table" or
(list.car == nil and list.cdr == nil) then
return tostring(list)
end
if list == empty_list then
return tostring(empty_list)
end
local result = {}
result[1] = "("
local delimitor = wrap and "\n" or " "
local function insert_value(sub_list)
if sub_list ~= empty_list then
table.insert(result, list_tostring(car(sub_list), false))
table.insert(result, delimitor)
return insert_value(cdr(sub_list))
else
table.remove(result)
end
end
insert_value(list)
table.insert(result, ")")
return table.concat(result)
end
local function list_is_nested(list)
if list == empty_list or (not is_pair(list)) then
return false
end
local car_item = car(list)
if (not is_pair(car_item)) then
return list_is_nested(cdr(list))
end
if length(car_item) > 1 then
return true
end
if (not list_is_nested(car_item)) then
return list_is_nested(cdr(list))
end
return true
end
local function list_rest_items_tostring_indented(rest_items, level)
if rest_items == empty_list then
return ""
end
level = level or 0
local prefix_str = string.rep(" ", level)
local result = {}
table.insert(result, list_tostring_indented(car(rest_items), level, true))
if cdr(rest_items) ~= empty_list then
table.insert(result, "\n")
table.insert(result, list_rest_items_tostring_indented(cdr(rest_items), level))
end
return table.concat(result)
end
function list_tostring_indented(list, level, head_prefix)
level = level or 0
local prefix_str = ""
local head_prefix_str = ""
if level > 0 then
prefix_str = string.rep(" ", level)
if head_prefix then
head_prefix_str = prefix_str
end
end
if not list_is_nested(list) then
return head_prefix_str .. list_tostring(list, false)
end
local result = {}
table.insert(result, (head_prefix_str .. "("))
table.insert(result, list_tostring_indented(car(list), level + 4, false))
if cdr(list) ~= empty_list then
table.insert(result, "\n")
table.insert(result, list_rest_items_tostring_indented(cdr(list), level + 4))
end
table.insert(result, ")")
return table.concat(result)
end
local cons_meta = {
__tostring = list_tostring,
__eq = list_eq
}
local function list_eq(a, b)
if a == nil and b == nil then
return true
elseif getmetatable(a) ~= cons_meta and
getmetatable(b) ~= cons_meta then
return a == b
else
return a ~= empty_list and b ~= empty_list and
car(a) == car(b) and
list_eq(cdr(a), cdr(b))
end
end
-- -- end of meta-methods for display -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- basic pair operations -- --
local function sub_stack(...)
local function sub_stack_itr(start, ...)
if start > select("#", ...) then
return
else
return select(start, ...),
sub_stack_itr(start + 1, ...)
end
end
return sub_stack_itr(2, ...)
end
function is_pair(p)
return type(p) == "table" and (p.car ~= nil or p.cdr ~= nil)
end
function cons(a, b)
local result = { car = a, cdr = b }
if set_cons_metatable then
setmetatable(result, cons_meta)
end
return result
end
function car(pair)
return pair.car
end
function cdr(pair)
return pair.cdr
end
function cadr(list)
return car(cdr(list))
end
-- -- basic pair operations -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- mutable pair -- --
function set_car(pair, x)
pair.car = x
end
function set_cdr(pair, x)
pair.cdr = x
end
-- -- mutable pair -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- list operations -- --
function list(...)
if select("#", ...) == 0 then
return empty_list
else
return cons(select(1, ...),
list(sub_stack(...)))
end
end
function list_unpack(list)
if cdr(list) == empty_list then
return car(list)
else
return car(list), list_unpack(cdr(list))
end
end
function list_ref(list, n)
if n == 1 then
return car(list)
else
return list_ref(cdr(list), n - 1)
end
end
function length(list)
if list == empty_list then
return 0
else
return 1 + length(cdr(list))
end
end
function remove(item, sequence)
local function retain(x)
return x ~= item
end
return filter(retain, sequence)
end
function append(list1, list2)
if list1 == empty_list or list1 == nil then
return list2 or empty_list
else
return cons(car(list1),
append(cdr(list1), list2))
end
end
function list_contains_nil(list)
if list == nil or list == empty_list then
return false
elseif car(list) == nil then
return true
else
return list_contains_nil(cdr(list))
end
end
function contains_nil(...)
return list_contains_nil(list(...))
end
-- -- list operations -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- filter / map -- --
function map(proc, list)
if list == nil or list == empty_list then
return empty_list
else
return cons(proc(car(list)),
map(proc, cdr(list)))
end
end
function cadr_ex(op, ...)
if select("#", ...) == 0 then
return
else
return op(select(1, ...)),
cadr_ex(op, sub_stack(...))
end
end
local function car_ex(...)
return cadr_ex(car, ...)
end
local function cdr_ex(...)
return cadr_ex(cdr, ...)
end
function map_ex(proc, ...)
if contains_nil(...) then
return nil
else
return cons(proc(car_ex(...)),
map_ex(proc, cdr_ex(...)))
end
end
function filter(predicate, list)
if list == nil or list == empty_list then
return empty_list
elseif predicate(car(list)) then
return cons(car(list), filter(predicate, cdr(list)))
else
return filter(predicate, cdr(list))
end
end
function accumulate(op, initial, sequence)
if sequence == nil or sequence == empty_list then
return initial or empty_list
else
return op(car(sequence),
accumulate(op, initial, cdr(sequence)))
end
end
function flat_map(proc, seq)
return accumulate(append,
empty_list,
map(proc, seq))
end
-- -- filter / map -- --
-------------------------------------------------------------------------
-------------------------------------------------------------------------
-- -- basic tree -- --
function count_leaf(tree)
if tree == nil or tree == empty_list then
return 0
elseif not is_pair(tree) then
return 1
else
return count_leaf(car(tree)) +
count_leaf(cdr(tree))
end
end
function map_tree(proc, tree)
local map_subtree
function map_subtree(subtree)
if is_pair(subtree) then
return map_tree(proc, subtree)
else
return proc(subtree)
end
end
return map(map_subtree, tree)
end
function enum_tree(tree)
if tree == nil or tree == empty_list then
return empty_list
elseif not is_pair(tree) then
return list(tree)
else
return append(enum_tree(car(tree)),
enum_tree(cdr(tree)))
end
end
-- -- basic tree -- --
-------------------------------------------------------------------------
return _ENV