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
|
/*
* Copyright © 2017 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"
#ifdef AO_SCHEME_FEATURE_VECTOR
static void vector_mark(void *addr)
{
struct ao_scheme_vector *vector = addr;
unsigned int i;
for (i = 0; i < vector->length; i++) {
ao_poly v = vector->vals[i];
ao_scheme_poly_mark(v, 1);
}
}
static int vector_len_size(uint16_t length)
{
return sizeof (struct ao_scheme_vector) + length * sizeof (ao_poly);
}
static int vector_size(void *addr)
{
struct ao_scheme_vector *vector = addr;
return vector_len_size(vector->length);
}
static void vector_move(void *addr)
{
struct ao_scheme_vector *vector = addr;
unsigned int i;
for (i = 0; i < vector->length; i++)
(void) ao_scheme_poly_move(&vector->vals[i], 1);
}
const struct ao_scheme_type ao_scheme_vector_type = {
.mark = vector_mark,
.size = vector_size,
.move = vector_move,
.name = "vector",
};
struct ao_scheme_vector *
ao_scheme_vector_alloc(uint16_t length, ao_poly fill)
{
struct ao_scheme_vector *vector;
unsigned int i;
vector = ao_scheme_alloc(vector_len_size(length));
if (!vector)
return NULL;
vector->type = AO_SCHEME_VECTOR;
vector->length = length;
for (i = 0; i < length; i++)
vector->vals[i] = fill;
return vector;
}
struct vl {
struct ao_scheme_vector *vector;
struct vl *prev;
};
static struct vl *vl;
static unsigned int vd;
void
ao_scheme_vector_write(FILE *out, ao_poly v, bool write)
{
struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
unsigned int i, j;
int was_marked = 0;
struct vl *ve;
++vd;
for (ve = vl; ve; ve = ve->prev)
if (ve->vector == vector)
abort();
ve = malloc(sizeof (struct vl));
ve->prev = vl;
ve->vector = vector;
vl = ve;
ao_scheme_print_start();
was_marked = ao_scheme_print_mark_addr(vector);
if (was_marked) {
fputs("...", out);
} else {
fputs("#(\n", out);
for (i = 0; i < vector->length; i++) {
printf("%3d: ", i);
for (j = 0; j < vd; j++)
printf(".");
ao_scheme_poly_write(out, vector->vals[i], write);
printf("\n");
}
printf(" ");
for (j = 0; j < vd; j++)
printf(".");
printf(")");
}
if (ao_scheme_print_stop() && !was_marked)
ao_scheme_print_clear_addr(vector);
if (vl != ve)
abort();
vl = ve->prev;
free(ve);
--vd;
}
struct ao_scheme_vector *
ao_scheme_list_to_vector(struct ao_scheme_cons *cons)
{
uint16_t length;
uint16_t i;
struct ao_scheme_vector *vector;
length = (uint16_t) ao_scheme_cons_length (cons);
if (ao_scheme_exception)
return NULL;
ao_scheme_cons_stash(cons);
vector = ao_scheme_vector_alloc(length, AO_SCHEME_NIL);
cons = ao_scheme_cons_fetch();
if (!vector)
return NULL;
i = 0;
while (cons) {
vector->vals[i++] = cons->car;
cons = ao_scheme_cons_cdr(cons);
}
return vector;
}
struct ao_scheme_cons *
ao_scheme_vector_to_list(struct ao_scheme_vector *vector, int start, int end)
{
int i;
uint16_t length = vector->length;
struct ao_scheme_cons *cons = NULL;
if (end == -1)
end = length;
if (start < 0)
start = 0;
if (end > length)
end = length;
for (i = end; i-- > start;) {
ao_scheme_vector_stash(vector);
cons = ao_scheme_cons_cons(vector->vals[i], ao_scheme_cons_poly(cons));
vector = ao_scheme_vector_fetch();
if (!cons)
return NULL;
}
return cons;
}
ao_poly
ao_scheme_do_vector(struct ao_scheme_cons *cons)
{
return ao_scheme_vector_poly(ao_scheme_list_to_vector(cons));
}
ao_poly
ao_scheme_do_make_vector(struct ao_scheme_cons *cons)
{
int32_t len;
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_make2dvector, cons,
AO_SCHEME_INT, &len,
AO_SCHEME_POLY|AO_SCHEME_ARG_OPTIONAL, _ao_scheme_bool_false, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_vector_poly(ao_scheme_vector_alloc(len, val));
}
static bool
ao_scheme_check_vector(ao_poly proc, struct ao_scheme_vector *vector, int32_t offset)
{
if (offset < 0 || vector->length <= offset) {
(void) ao_scheme_error(AO_SCHEME_INVALID, "%v: vector index %d out of range (max %d)",
proc,
offset, vector->length);
return false;
}
return true;
}
ao_poly
ao_scheme_do_vector_ref(struct ao_scheme_cons *cons)
{
struct ao_scheme_vector *vector;
int32_t offset;
if (!ao_scheme_parse_args(_ao_scheme_atom_vector2dref, cons,
AO_SCHEME_VECTOR, &vector,
AO_SCHEME_INT, &offset,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
if (!ao_scheme_check_vector(_ao_scheme_atom_vector2dref, vector, offset))
return AO_SCHEME_NIL;
return vector->vals[offset];
}
ao_poly
ao_scheme_do_vector_set(struct ao_scheme_cons *cons)
{
struct ao_scheme_vector *vector;
int32_t offset;
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_vector2dset21, cons,
AO_SCHEME_VECTOR, &vector,
AO_SCHEME_INT, &offset,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
if (!ao_scheme_check_vector(_ao_scheme_atom_vector2dset21, vector, offset))
return AO_SCHEME_NIL;
vector->vals[offset] = val;
return val;
}
ao_poly
ao_scheme_do_list_to_vector(struct ao_scheme_cons *cons)
{
struct ao_scheme_cons *pair;
if (!ao_scheme_parse_args(_ao_scheme_atom_list2d3evector, cons,
AO_SCHEME_CONS|AO_SCHEME_ARG_NIL_OK, &pair,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_vector_poly(ao_scheme_list_to_vector(pair));
}
ao_poly
ao_scheme_do_vector_to_list(struct ao_scheme_cons *cons)
{
struct ao_scheme_vector *vector;
int32_t start, end;
if (!ao_scheme_parse_args(_ao_scheme_atom_vector2d3elist, cons,
AO_SCHEME_VECTOR, &vector,
AO_SCHEME_INT|AO_SCHEME_ARG_OPTIONAL, ao_scheme_int_poly(0), &start,
AO_SCHEME_INT|AO_SCHEME_ARG_OPTIONAL, ao_scheme_int_poly(-1), &end,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
if (end == -1)
end = vector->length;
return ao_scheme_cons_poly(ao_scheme_vector_to_list(vector, start, end));
}
ao_poly
ao_scheme_do_vector_length(struct ao_scheme_cons *cons)
{
struct ao_scheme_vector *vector;
if (!ao_scheme_parse_args(_ao_scheme_atom_vector2d3elist, cons,
AO_SCHEME_VECTOR, &vector,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_integer_poly(vector->length);
}
ao_poly
ao_scheme_do_vectorp(struct ao_scheme_cons *cons)
{
return ao_scheme_do_typep(_ao_scheme_atom_vector3f, AO_SCHEME_VECTOR, cons);
}
#endif /* AO_SCHEME_FEATURE_VECTOR */
|