altos/scheme: Rework display/write code
[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, bool write)
77 {
78         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
79         unsigned int i;
80         int was_marked = 0;
81
82         ao_scheme_print_start();
83         was_marked = ao_scheme_print_mark_addr(vector);
84         if (was_marked) {
85                 printf ("...");
86         } else {
87                 printf("#(");
88                 for (i = 0; i < vector->length; i++) {
89                         if (i != 0)
90                                 printf(" ");
91                         ao_scheme_poly_write(vector->vals[i], write);
92                 }
93                 printf(")");
94         }
95         if (ao_scheme_print_stop() && !was_marked)
96                 ao_scheme_print_clear_addr(vector);
97 }
98
99 static int32_t
100 ao_scheme_vector_offset(struct ao_scheme_vector *vector, ao_poly i)
101 {
102         bool    fail;
103         int32_t offset = ao_scheme_poly_integer(i, &fail);
104
105         if (fail)
106                 ao_scheme_error(AO_SCHEME_INVALID, "vector index %v not integer", i);
107         if (offset < 0 || vector->length <= offset) {
108                 ao_scheme_error(AO_SCHEME_INVALID, "vector index %v out of range (max %d)",
109                                 i, vector->length);
110                 offset = -1;
111         }
112         return offset;
113 }
114
115 ao_poly
116 ao_scheme_vector_get(ao_poly v, ao_poly i)
117 {
118         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
119         int32_t                 offset = ao_scheme_vector_offset(vector, i);
120
121         if (offset < 0)
122                 return AO_SCHEME_NIL;
123         return vector->vals[offset];
124 }
125
126 ao_poly
127 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p)
128 {
129         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
130         int32_t                 offset = ao_scheme_vector_offset(vector, i);
131
132         if (offset < 0)
133                 return AO_SCHEME_NIL;
134         return vector->vals[offset] = p;
135 }
136
137 struct ao_scheme_vector *
138 ao_scheme_list_to_vector(struct ao_scheme_cons *cons)
139 {
140         uint16_t                length;
141         uint16_t                i;
142         struct ao_scheme_vector *vector;
143
144         length = (uint16_t) ao_scheme_cons_length (cons);
145         if (ao_scheme_exception)
146                 return NULL;
147
148         ao_scheme_cons_stash(0, cons);
149         vector = ao_scheme_vector_alloc(length, AO_SCHEME_NIL);
150         cons = ao_scheme_cons_fetch(0);
151         if (!vector)
152                 return NULL;
153         i = 0;
154         while (cons) {
155                 vector->vals[i++] = cons->car;
156                 cons = ao_scheme_cons_cdr(cons);
157         }
158         return vector;
159 }
160
161 struct ao_scheme_cons *
162 ao_scheme_vector_to_list(struct ao_scheme_vector *vector)
163 {
164         unsigned int            i;
165         uint16_t                length = vector->length;
166         struct ao_scheme_cons   *cons = NULL;
167
168         for (i = length; i-- > 0;) {
169                 ao_scheme_poly_stash(2, ao_scheme_vector_poly(vector));
170                 cons = ao_scheme_cons_cons(vector->vals[i], ao_scheme_cons_poly(cons));
171                 vector = ao_scheme_poly_vector(ao_scheme_poly_fetch(2));
172                 if (!cons)
173                         return NULL;
174         }
175         return cons;
176 }
177
178 #endif /* AO_SCHEME_FEATURE_VECTOR */