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
|
/*
* Copyright © 2016 Keith Packard <keithp@keithp.com>
*
* 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 2 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.
*/
#include "ao_scheme.h"
static void cons_mark(void *addr)
{
struct ao_scheme_cons *cons = addr;
for (;;) {
ao_poly cdr = cons->cdr;
ao_scheme_poly_mark(cons->car, 1);
if (!cdr)
break;
if (!ao_scheme_is_cons(cdr)) {
ao_scheme_poly_mark(cdr, 0);
break;
}
cons = ao_scheme_poly_cons(cdr);
if (ao_scheme_mark_memory(&ao_scheme_cons_type, cons))
break;
}
}
static int cons_size(void *addr)
{
(void) addr;
return sizeof (struct ao_scheme_cons);
}
static void cons_move(void *addr)
{
struct ao_scheme_cons *cons = addr;
if (!cons)
return;
for (;;) {
ao_poly cdr;
struct ao_scheme_cons *c;
int ret;
MDBG_MOVE("cons_move start %d (%d, %d)\n",
MDBG_OFFSET(cons), MDBG_OFFSET(ao_scheme_ref(cons->car)), MDBG_OFFSET(ao_scheme_ref(cons->cdr)));
(void) ao_scheme_poly_move(&cons->car, 1);
cdr = cons->cdr;
if (!cdr)
break;
if (!ao_scheme_is_cons(cdr)) {
(void) ao_scheme_poly_move(&cons->cdr, 0);
break;
}
c = ao_scheme_poly_cons(cdr);
ret = ao_scheme_move_memory(&ao_scheme_cons_type, (void **) &c);
if (c != ao_scheme_poly_cons(cons->cdr))
cons->cdr = ao_scheme_cons_poly(c);
MDBG_MOVE("cons_move end %d (%d, %d)\n",
MDBG_OFFSET(cons), MDBG_OFFSET(ao_scheme_ref(cons->car)), MDBG_OFFSET(ao_scheme_ref(cons->cdr)));
if (ret)
break;
cons = c;
}
}
const struct ao_scheme_type ao_scheme_cons_type = {
.mark = cons_mark,
.size = cons_size,
.move = cons_move,
.name = "cons",
};
struct ao_scheme_cons *ao_scheme_cons_free_list;
struct ao_scheme_cons *
ao_scheme_cons_cons(ao_poly car, ao_poly cdr)
{
struct ao_scheme_cons *cons;
if (ao_scheme_cons_free_list) {
cons = ao_scheme_cons_free_list;
ao_scheme_cons_free_list = ao_scheme_poly_cons(cons->cdr);
} else {
ao_scheme_poly_stash(car);
ao_scheme_poly_stash(cdr);
cons = ao_scheme_alloc(sizeof (struct ao_scheme_cons));
cdr = ao_scheme_poly_fetch();
car = ao_scheme_poly_fetch();
if (!cons)
return NULL;
}
cons->car = car;
cons->cdr = cdr;
return cons;
}
struct ao_scheme_cons *
ao_scheme_cons_cdr(struct ao_scheme_cons *cons)
{
ao_poly cdr = cons->cdr;
if (cdr == AO_SCHEME_NIL)
return NULL;
if (!ao_scheme_is_cons(cdr)) {
(void) ao_scheme_error(AO_SCHEME_INVALID, "improper cdr %v", cdr);
return NULL;
}
return ao_scheme_poly_cons(cdr);
}
ao_poly
ao_scheme_cons(ao_poly car, ao_poly cdr)
{
return ao_scheme_cons_poly(ao_scheme_cons_cons(car, cdr));
}
static struct ao_scheme_cons *
ao_scheme_cons_copy(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *head = NULL;
struct ao_scheme_cons *tail = NULL;
while (cons) {
struct ao_scheme_cons *new;
ao_poly cdr;
ao_scheme_cons_stash(cons);
ao_scheme_cons_stash(head);
ao_scheme_cons_stash(tail);
new = ao_scheme_alloc(sizeof (struct ao_scheme_cons));
tail = ao_scheme_cons_fetch();
head = ao_scheme_cons_fetch();
cons = ao_scheme_cons_fetch();
if (!new)
return AO_SCHEME_NIL;
new->car = cons->car;
new->cdr = AO_SCHEME_NIL;
if (!head)
head = new;
else
tail->cdr = ao_scheme_cons_poly(new);
tail = new;
cdr = cons->cdr;
if (!ao_scheme_is_cons(cdr)) {
tail->cdr = cdr;
break;
}
cons = ao_scheme_poly_cons(cdr);
}
return head;
}
void
ao_scheme_cons_free(struct ao_scheme_cons *cons)
{
#if DBG_FREE_CONS
ao_scheme_cons_check(cons);
#endif
while (cons) {
ao_poly cdr = cons->cdr;
cons->cdr = ao_scheme_cons_poly(ao_scheme_cons_free_list);
ao_scheme_cons_free_list = cons;
cons = ao_scheme_poly_cons(cdr);
}
}
void
ao_scheme_cons_write(FILE *out, ao_poly c, bool write)
{
struct ao_scheme_cons *cons = ao_scheme_poly_cons(c);
struct ao_scheme_cons *clear = cons;
ao_poly cdr;
int written = 0;
ao_scheme_print_start();
fprintf(out, "(");
while (cons) {
if (written != 0)
fprintf(out, " ");
/* Note if there's recursion in printing. Not
* as good as actual references, but at least
* we don't infinite loop...
*/
if (ao_scheme_print_mark_addr(cons)) {
fprintf(out, "...");
break;
}
ao_scheme_poly_write(out, cons->car, write);
/* keep track of how many pairs have been printed */
written++;
cdr = cons->cdr;
if (!ao_scheme_is_cons(cdr)) {
fprintf(out, " . ");
ao_scheme_poly_write(out, cdr, write);
break;
}
cons = ao_scheme_poly_cons(cdr);
}
fprintf(out, ")");
if (ao_scheme_print_stop()) {
/* If we're still printing, clear the print marks on
* all printed pairs
*/
while (written--) {
ao_scheme_print_clear_addr(clear);
clear = ao_scheme_poly_cons(clear->cdr);
}
}
}
int
ao_scheme_cons_length(struct ao_scheme_cons *cons)
{
int len = 0;
while (cons) {
len++;
cons = ao_scheme_cons_cdr(cons);
}
return len;
}
ao_poly
ao_scheme_do_car(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
if (!ao_scheme_parse_args(_ao_scheme_atom_car, cons,
AO_SCHEME_CONS, &pair,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return pair->car;
}
ao_poly
ao_scheme_do_cdr(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
if (!ao_scheme_parse_args(_ao_scheme_atom_cdr, cons,
AO_SCHEME_CONS, &pair,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return pair->cdr;
}
ao_poly
ao_scheme_do_cons(struct ao_scheme_cons *cons)
{
ao_poly car, cdr;
if (!ao_scheme_parse_args(_ao_scheme_atom_cons, cons,
AO_SCHEME_POLY, &car,
AO_SCHEME_POLY, &cdr,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_cons(car, cdr);
}
ao_poly
ao_scheme_do_last(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
if (!ao_scheme_parse_args(_ao_scheme_atom_last, cons,
AO_SCHEME_CONS | AO_SCHEME_ARG_NIL_OK, &pair,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
while (pair) {
if (!pair->cdr)
return pair->car;
pair = ao_scheme_cons_cdr(pair);
}
return AO_SCHEME_NIL;
}
ao_poly
ao_scheme_do_length(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
if (!ao_scheme_parse_args(_ao_scheme_atom_length, cons,
AO_SCHEME_CONS | AO_SCHEME_ARG_NIL_OK, &pair,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_integer_poly(ao_scheme_cons_length(pair));
}
ao_poly
ao_scheme_do_list_copy(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
if (!ao_scheme_parse_args(_ao_scheme_atom_list2dcopy, cons,
AO_SCHEME_CONS | AO_SCHEME_ARG_NIL_OK, &pair,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_cons_poly(ao_scheme_cons_copy(pair));
}
ao_poly
ao_scheme_do_list_tail(struct ao_scheme_cons *cons)
{
ao_poly list;
int32_t v;
if (!ao_scheme_parse_args(_ao_scheme_atom_list2dtail, cons,
AO_SCHEME_CONS | AO_SCHEME_ARG_NIL_OK | AO_SCHEME_ARG_RET_POLY, &list,
AO_SCHEME_INT, &v,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
while (v > 0) {
if (!list)
return ao_scheme_error(AO_SCHEME_INVALID, "%v: ran off end", _ao_scheme_atom_list2dtail);
if (!ao_scheme_is_cons(list))
return ao_scheme_error(AO_SCHEME_INVALID, "%v: invalid list", _ao_scheme_atom_list2dtail);
list = ao_scheme_poly_cons(list)->cdr;
v--;
}
return list;
}
ao_poly
ao_scheme_do_pairp(struct ao_scheme_cons *cons)
{
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_pair3f, cons,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
if (ao_scheme_is_pair(val))
return _ao_scheme_bool_true;
return _ao_scheme_bool_false;
}
/* This one is special -- a list is either nil or
* a 'proper' list with only cons cells
*/
ao_poly
ao_scheme_do_listp(struct ao_scheme_cons *cons)
{
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_pair3f, cons,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
for (;;) {
if (val == AO_SCHEME_NIL)
return _ao_scheme_bool_true;
if (!ao_scheme_is_cons(val))
return _ao_scheme_bool_false;
val = ao_scheme_poly_cons(val)->cdr;
}
}
ao_poly
ao_scheme_do_set_car(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_set2dcar21, cons,
AO_SCHEME_CONS, &pair,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
pair->car = val;
return val;
}
ao_poly
ao_scheme_do_set_cdr(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_set2dcar21, cons,
AO_SCHEME_CONS, &pair,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
pair->cdr = val;
return val;
}
|