altos/scheme: Add ao_scheme_vector.c
[fw/altos] / src / scheme / ao_scheme_vector.c
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_scheme.h"
16
17 #ifdef AO_SCHEME_FEATURE_VECTOR
18
19 static void vector_mark(void *addr)
20 {
21         struct ao_scheme_vector *vector = addr;
22         unsigned int    i;
23
24         for (i = 0; i < vector->length; i++) {
25                 ao_poly v = vector->vals[i];
26
27                 ao_scheme_poly_mark(v, 1);
28         }
29 }
30
31 static int vector_len_size(uint16_t length)
32 {
33         return sizeof (struct ao_scheme_vector) + length * sizeof (ao_poly);
34 }
35
36 static int vector_size(void *addr)
37 {
38         struct ao_scheme_vector *vector = addr;
39
40         return vector_len_size(vector->length);
41 }
42
43 static void vector_move(void *addr)
44 {
45         struct ao_scheme_vector *vector = addr;
46         unsigned int    i;
47
48         for (i = 0; i < vector->length; i++)
49                 (void) ao_scheme_poly_move(&vector->vals[i], 1);
50 }
51
52 const struct ao_scheme_type ao_scheme_vector_type = {
53         .mark = vector_mark,
54         .size = vector_size,
55         .move = vector_move,
56         .name = "vector",
57 };
58
59 struct ao_scheme_vector *
60 ao_scheme_vector_alloc(uint16_t length, ao_poly fill)
61 {
62         struct ao_scheme_vector *vector;
63         unsigned int i;
64
65         vector = ao_scheme_alloc(vector_len_size(length));
66         if (!vector)
67                 return NULL;
68         vector->type = AO_SCHEME_VECTOR;
69         vector->length = length;
70         for (i = 0; i < length; i++)
71                 vector->vals[i] = fill;
72         return vector;
73 }
74
75 void
76 ao_scheme_vector_write(ao_poly v)
77 {
78         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
79         unsigned int i;
80
81         printf("#(");
82         for (i = 0; i < vector->length; i++) {
83                 if (i != 0)
84                         printf(" ");
85                 if (vector->vals[i] == v)
86                         printf ("...");
87                 else
88                         ao_scheme_poly_write(vector->vals[i]);
89         }
90         printf(")");
91 }
92
93 void
94 ao_scheme_vector_display(ao_poly v)
95 {
96         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
97         unsigned int i;
98
99         for (i = 0; i < vector->length; i++) {
100                 if (vector->vals[i] == v)
101                         printf("...");
102                 else
103                         ao_scheme_poly_display(vector->vals[i]);
104         }
105 }
106
107 static int32_t
108 ao_scheme_vector_offset(struct ao_scheme_vector *vector, ao_poly i)
109 {
110         int32_t offset = ao_scheme_poly_integer(i);
111
112         if (offset == AO_SCHEME_NOT_INTEGER)
113                 ao_scheme_error(AO_SCHEME_INVALID, "vector index %v not integer", i);
114         if (offset < 0 || vector->length <= offset) {
115                 ao_scheme_error(AO_SCHEME_INVALID, "vector index %v out of range (max %d)",
116                                 i, vector->length);
117                 offset = AO_SCHEME_NOT_INTEGER;
118         }
119         return offset;
120 }
121
122 ao_poly
123 ao_scheme_vector_get(ao_poly v, ao_poly i)
124 {
125         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
126         int32_t                 offset = ao_scheme_vector_offset(vector, i);
127
128         if (offset == AO_SCHEME_NOT_INTEGER)
129                 return AO_SCHEME_NIL;
130         return vector->vals[offset];
131 }
132
133 ao_poly
134 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p)
135 {
136         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
137         int32_t                 offset = ao_scheme_vector_offset(vector, i);
138
139         if (offset == AO_SCHEME_NOT_INTEGER)
140                 return AO_SCHEME_NIL;
141         return vector->vals[offset] = p;
142 }
143
144 struct ao_scheme_vector *
145 ao_scheme_list_to_vector(struct ao_scheme_cons *cons)
146 {
147         uint16_t                length;
148         uint16_t                i;
149         struct ao_scheme_vector *vector;
150
151         length = (uint16_t) ao_scheme_cons_length (cons);
152         if (ao_scheme_exception)
153                 return NULL;
154
155         ao_scheme_cons_stash(0, cons);
156         vector = ao_scheme_vector_alloc(length, AO_SCHEME_NIL);
157         cons = ao_scheme_cons_fetch(0);
158         if (!vector)
159                 return NULL;
160         i = 0;
161         while (cons) {
162                 vector->vals[i++] = cons->car;
163                 cons = ao_scheme_cons_cdr(cons);
164         }
165         return vector;
166 }
167
168 struct ao_scheme_cons *
169 ao_scheme_vector_to_list(struct ao_scheme_vector *vector)
170 {
171         unsigned int            i;
172         uint16_t                length = vector->length;
173         struct ao_scheme_cons   *cons = NULL;
174
175         for (i = length; i-- > 0;) {
176                 ao_scheme_poly_stash(2, ao_scheme_vector_poly(vector));
177                 cons = ao_scheme_cons_cons(vector->vals[i], ao_scheme_cons_poly(cons));
178                 vector = ao_scheme_poly_vector(ao_scheme_poly_fetch(2));
179                 if (!cons)
180                         return NULL;
181         }
182         return cons;
183 }
184
185 #endif /* AO_SCHEME_FEATURE_VECTOR */