-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
460 lines (407 loc) · 10.9 KB
/
list.c
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
455
456
457
458
459
460
/* Copyright 2014-2015 Drew Thoreson
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
navi_obj navi_vlist(navi_obj first, va_list ap)
{
navi_obj list, listptr;
list = listptr = navi_make_empty_pair();
navi_pair(list)->car = first;
for (navi_obj arg = va_arg(ap, navi_obj);
navi_type(arg) != NAVI_VOID;
arg = va_arg(ap, navi_obj)) {
navi_pair(listptr)->cdr = navi_make_empty_pair();
listptr = navi_cdr(listptr);
navi_pair(listptr)->car = arg;
}
navi_pair(listptr)->cdr = navi_make_nil();
return list;
}
navi_obj _navi_list(navi_obj first, ...)
{
va_list ap;
navi_obj list;
if (navi_type(first) == NAVI_VOID)
return navi_make_nil();
va_start(ap, first);
list = navi_vlist(first, ap);
va_end(ap);
return list;
}
int navi_list_length(navi_obj list)
{
int i;
for (i = 0; navi_type(list) == NAVI_PAIR; i++)
list = navi_cdr(list);
return i;
}
int navi_list_length_safe(navi_obj list)
{
int i;
for (i = 0; navi_is_pair(list); i++)
list = navi_cdr(list);
if (navi_type(list) != NAVI_NIL)
return -1;
return i;
}
int navi_is_proper_list(navi_obj list)
{
navi_obj cons;
enum navi_type type = navi_type(list);
if (type == NAVI_NIL)
return true;
if (type != NAVI_PAIR)
return false;
navi_list_for_each(cons, list);
return navi_is_nil(cons);
}
bool navi_is_type(navi_obj obj, int type)
{
switch (type) {
case NAVI_LIST:
return navi_is_list(obj);
case NAVI_PROPER_LIST:
return navi_is_proper_list(obj);
case NAVI_BYTE:
return navi_is_byte(obj);
case NAVI_ANY:
return true;
default:
return navi_type(obj) == (unsigned)type;
}
}
bool navi_is_list_of(navi_obj list, int type, bool allow_dotted_tail)
{
navi_obj cons;
navi_list_for_each(cons, list) {
if (!navi_is_type(navi_car(cons), type))
return false;
}
if (!allow_dotted_tail && navi_type(cons) != NAVI_NIL)
return false;
if (allow_dotted_tail && navi_type(cons) != NAVI_NIL
&& !navi_is_type(cons, type))
return false;
return true;
}
navi_obj navi_list_append_ip(navi_obj a, navi_obj b)
{
if (navi_is_nil(a))
return b;
if (navi_is_nil(b))
return a;
navi_set_cdr(navi_last_cons(a), b);
return a;
}
DEFUN(cons, "cons", 2, 0, NAVI_ANY, NAVI_ANY)
{
return navi_make_pair(scm_arg1, scm_arg2);
}
DEFUN(car, "car", 1, 0, NAVI_PAIR)
{
return navi_car(scm_arg1);
}
DEFUN(cdr, "cdr", 1, 0, NAVI_PAIR)
{
return navi_cdr(scm_arg1);
}
DEFUN(set_car, "set-car!", 2, 0, NAVI_PAIR, NAVI_ANY)
{
navi_set_car(scm_arg1, scm_arg2);
return navi_unspecified();
}
DEFUN(set_cdr, "set-cdr!", 2, 0, NAVI_PAIR, NAVI_ANY)
{
navi_set_cdr(scm_arg1, scm_arg2);
return navi_unspecified();
}
DEFUN(pairp, "pair?", 1, 0, NAVI_ANY)
{
return navi_make_bool(navi_type(scm_arg1) == NAVI_PAIR);
}
DEFUN(nullp, "null?", 1, 0, NAVI_ANY)
{
return navi_make_bool(navi_type(scm_arg1) == NAVI_NIL);
}
DEFUN(listp, "list?", 1, 0, NAVI_ANY)
{
return navi_make_bool(navi_is_proper_list(scm_arg1));
}
navi_obj navi_make_list(long k, navi_obj obj)
{
navi_obj cons = navi_make_nil();
for (long i = 0; i < k; i++)
cons = navi_make_pair(obj, cons);
return cons;
}
DEFUN(make_list, "make-list", 1, NAVI_PROC_VARIADIC, NAVI_FIXNUM)
{
if (unlikely(scm_nr_args > 2))
navi_arity_error(scm_env, navi_make_symbol("make-list"));
if (unlikely(navi_fixnum(scm_arg1) < 0))
navi_error(scm_env, "type error: not a positive integer");
return navi_make_list(navi_fixnum(scm_arg1),
scm_nr_args == 2 ? scm_arg2 : navi_make_fixnum(0));
}
DEFUN(length, "length", 1, 0, NAVI_LIST)
{
int length = navi_list_length_safe(scm_arg1);
if (unlikely(length < 0))
navi_error(scm_env, "type error: not a proper list");
return navi_make_fixnum(length);
}
DEFUN(list, "list", 0, NAVI_PROC_VARIADIC)
{
return scm_args;
}
navi_obj navi_list_copy(navi_obj list)
{
navi_obj copy, cursor, cons;
if (navi_is_nil(list))
return navi_make_nil();
copy = cursor = navi_make_pair(navi_car(list), navi_make_nil());
navi_list_for_each(cons, navi_cdr(list)) {
navi_obj pair = navi_make_pair(navi_car(cons), navi_make_nil());
navi_set_cdr(cursor, pair);
cursor = navi_cdr(cursor);
}
navi_set_cdr(cursor, cons);
return copy;
}
DEFUN(list_copy, "list-copy", 1, 0, NAVI_LIST)
{
return navi_list_copy(scm_arg1);
}
DEFUN(append, "append", 1, NAVI_PROC_VARIADIC, NAVI_ANY)
{
navi_obj cons, first, last;
first = last = navi_make_pair(navi_make_nil(), navi_make_nil());
navi_list_for_each(cons, scm_args) {
if (navi_is_last_pair(cons)) {
navi_set_cdr(last, navi_car(cons));
break;
}
navi_type_check_proper_list(navi_car(cons), scm_env);
navi_set_cdr(last, navi_list_copy(navi_car(cons)));
last = navi_last_cons(last);
}
return navi_cdr(first);
}
DEFUN(reverse, "reverse", 1, 0, NAVI_LIST)
{
navi_obj cons, reversed = navi_make_nil();
navi_list_for_each(cons, scm_arg1) {
reversed = navi_make_pair(navi_car(cons), reversed);
}
// XXX: we ignore dotted tail on improper list
return reversed;
}
navi_obj navi_list_tail(navi_obj list, long k)
{
navi_obj cons;
long i = 0;
navi_list_for_each(cons, list) {
if (i++ == k)
return cons;
}
if (i == k)
return cons;
return navi_make_void();
}
DEFUN(list_tail, "list-tail", 2, 0, NAVI_LIST, NAVI_FIXNUM)
{
navi_obj result;
if (unlikely(navi_fixnum(scm_arg2) < 0))
navi_error(scm_env, "type error: not a positive integer");
result = navi_list_tail(scm_arg1, navi_fixnum(scm_arg2));
if (unlikely(navi_is_void(result)))
navi_error(scm_env, "list index out of bounds");
return result;
}
navi_obj navi_list_ref(navi_obj list, long k)
{
navi_obj tail = navi_list_tail(list, k);
if (!navi_is_pair(tail))
return navi_make_void();
return navi_car(tail);
}
DEFUN(list_ref, "list-ref", 2, 0, NAVI_PAIR, NAVI_FIXNUM)
{
navi_obj result;
if (unlikely(navi_fixnum(scm_arg2) < 0))
navi_error(scm_env, "type error: not a positive integer");
result = navi_list_tail(scm_arg1, navi_fixnum(scm_arg2));
if (unlikely(!navi_is_pair(result)))
navi_error(scm_env, "list index out of bounds");
return navi_car(result);
}
DEFUN(list_set, "list-set!", 3, 0, NAVI_PAIR, NAVI_FIXNUM, NAVI_ANY)
{
navi_obj result;
if (unlikely(navi_fixnum(scm_arg2) < 0))
navi_error(scm_env, "type error: not a positive integer");
result = navi_list_tail(scm_arg1, navi_fixnum(scm_arg2));
if (unlikely(!navi_is_pair(result)))
navi_error(scm_env, "list index out of bounds");
navi_set_car(result, scm_arg3);
return navi_unspecified();
}
DEFUN(memq, "memq", 2, 0, NAVI_ANY, NAVI_LIST)
{
navi_obj cons;
navi_list_for_each(cons, scm_arg2) {
if (navi_eqp(scm_arg1, navi_car(cons)))
return cons;
}
return navi_make_bool(false);
}
DEFUN(memv, "memv", 2, 0, NAVI_ANY, NAVI_LIST)
{
navi_obj cons;
navi_list_for_each(cons, scm_arg2) {
if (navi_eqvp(scm_arg1, navi_car(cons)))
return cons;
}
return navi_make_bool(false);
}
static navi_obj navi_member(navi_obj obj, navi_obj list)
{
navi_obj cons;
navi_list_for_each(cons, list) {
if (navi_equalp(obj, navi_car(cons)))
return cons;
}
return navi_make_bool(false);
}
static bool do_compare(navi_obj proc, navi_obj fst, navi_obj snd, navi_env env)
{
return navi_is_true(navi_force_tail(navi_apply(navi_procedure(proc),
navi_list(fst, snd, navi_make_void()), env), env));
}
DEFUN(member, "member", 2, NAVI_PROC_VARIADIC, NAVI_ANY, NAVI_LIST)
{
navi_obj cons;
if (unlikely(scm_nr_args > 3))
navi_arity_error(scm_env, navi_make_symbol("member"));
if (scm_nr_args == 2)
return navi_member(scm_arg1, scm_arg2);
navi_type_check_proc(scm_arg3, 2, scm_env);
navi_list_for_each(cons, scm_arg2) {
if (do_compare(scm_arg3, scm_arg1, navi_car(cons), scm_env))
return cons;
}
return navi_make_bool(false);
}
DEFUN(assq, "assq", 2, 0, NAVI_ANY, NAVI_LIST)
{
navi_obj cons;
navi_list_for_each(cons, scm_arg2) {
navi_type_check(navi_car(cons), NAVI_PAIR, scm_env);
if (navi_eqp(scm_arg1, navi_caar(cons)))
return navi_car(cons);
}
return navi_make_bool(false);
}
DEFUN(assv, "assv", 2, 0, NAVI_ANY, NAVI_LIST)
{
navi_obj cons;
navi_list_for_each(cons, scm_arg2) {
navi_type_check(navi_car(cons), NAVI_PAIR, scm_env);
if (navi_eqvp(scm_arg1, navi_caar(cons)))
return navi_car(cons);
}
return navi_make_bool(false);
}
static navi_obj navi_assoc(navi_obj obj, navi_obj list, navi_env env)
{
navi_obj cons;
navi_list_for_each(cons, list) {
navi_type_check(navi_car(cons), NAVI_PAIR, env);
if (navi_equalp(obj, navi_caar(cons)))
return navi_car(cons);
}
return navi_make_bool(false);
}
DEFUN(assoc, "assoc", 2, NAVI_PROC_VARIADIC, NAVI_ANY, NAVI_LIST)
{
navi_obj cons;
if (unlikely(scm_nr_args > 3))
navi_arity_error(scm_env, navi_make_symbol("assoc"));
if (scm_nr_args == 2)
return navi_assoc(scm_arg1, scm_arg2, scm_env);
navi_list_for_each(cons, scm_arg2) {
navi_type_check(navi_car(cons), NAVI_PAIR, scm_env);
if (do_compare(scm_arg3, scm_arg1, navi_caar(cons), scm_env))
return navi_car(cons);
}
return navi_make_bool(false);
}
static navi_obj arg_list(navi_obj *lists, int nr_lists)
{
navi_obj args, cons;
cons = args = navi_make_pair(navi_make_nil(), navi_make_nil());
for (int i = 0; i < nr_lists; i++) {
navi_obj cdr = navi_make_pair(navi_car(lists[i]), navi_make_nil());
navi_set_cdr(cons, cdr);
cons = navi_cdr(cons);
}
return navi_cdr(args);
}
static navi_obj do_apply(navi_obj proc, navi_obj args, navi_env env)
{
return navi_force_tail(navi_apply(navi_procedure(proc), args, env), env);
}
static void cons_array_fill(navi_obj *array, navi_obj args, navi_env env)
{
navi_obj cons;
int i = 0;
navi_list_for_each(cons, args) {
array[i] = navi_car(cons);
navi_type_check_list(array[i++], env);
}
}
static bool cons_array_terminated(navi_obj *array, size_t nr_lists)
{
for (size_t i = 0; i < nr_lists; i++)
if (!navi_is_pair(array[i]))
return true;
return false;
}
static void cons_array_advance(navi_obj *array, size_t nr_lists)
{
for (size_t i = 0; i < nr_lists; i++)
array[i] = navi_cdr(array[i]);
}
DEFUN(for_each, "for-each", 2, NAVI_PROC_VARIADIC, NAVI_PROCEDURE, NAVI_ANY)
{
navi_obj cons[scm_nr_args-1];
navi_check_arity(scm_arg1, scm_nr_args-1, scm_env);
cons_array_fill(cons, navi_cdr(scm_args), scm_env);
for (;;) {
if (cons_array_terminated(cons, scm_nr_args-1))
break;
do_apply(scm_arg1, arg_list(cons, scm_nr_args-1), scm_env);
cons_array_advance(cons, scm_nr_args-1);
}
return navi_unspecified();
}
DEFUN(map, "map", 2, NAVI_PROC_VARIADIC, NAVI_PROCEDURE, NAVI_ANY)
{
navi_obj result, last;
navi_obj cons[scm_nr_args-1];
navi_check_arity(scm_arg1, scm_nr_args-1, scm_env);
cons_array_fill(cons, navi_cdr(scm_args), scm_env);
result = last = navi_make_pair(navi_make_nil(), navi_make_nil());
for (;;) {
navi_obj elm;
if (cons_array_terminated(cons, scm_nr_args-1))
break;
elm = do_apply(scm_arg1, arg_list(cons, scm_nr_args-1), scm_env);
navi_set_cdr(last, navi_make_pair(elm, navi_make_nil()));
last = navi_cdr(last);
cons_array_advance(cons, scm_nr_args-1);
}
return navi_cdr(result);
}