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
|
/*
* 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; version 2 of the License.
*
* 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.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "ao_scheme.h"
static int name_size(char *name)
{
return sizeof(struct ao_scheme_atom) + strlen(name) + 1;
}
static int atom_size(void *addr)
{
struct ao_scheme_atom *atom = addr;
if (!atom)
return 0;
return name_size(atom->name);
}
static void atom_mark(void *addr)
{
MDBG_MOVE("mark atom %s\n", ((struct ao_scheme_atom *) addr)->name);
(void) addr;
}
static void atom_move(void *addr)
{
(void) addr;
}
const struct ao_scheme_type ao_scheme_atom_type = {
.mark = atom_mark,
.size = atom_size,
.move = atom_move,
.name = "atom"
};
struct ao_scheme_atom *ao_scheme_atoms;
static struct ao_scheme_atom *
ao_scheme_atom_find(const char *name)
{
struct ao_scheme_atom *atom;
#ifdef ao_builtin_atoms
if (!ao_scheme_atoms)
ao_scheme_atoms = ao_scheme_poly_atom(ao_builtin_atoms);
#endif
for (atom = ao_scheme_atoms; atom; atom = ao_scheme_poly_atom(atom->next)) {
if (!strcmp(atom->name, name))
return atom;
}
return NULL;
}
#ifdef AO_SCHEME_MAKE_CONST
#define AO_SCHEME_BUILTIN_SYNTAX_ATOMS
#include "ao_scheme_builtin.h"
#undef AO_SCHEME_BUILTIN_SYNTAX_ATOMS
static void
ao_scheme_atom_mark_syntax(void)
{
unsigned a;
for (a = 0; a < sizeof(syntax_atoms)/sizeof(syntax_atoms[0]); a++) {
struct ao_scheme_atom *atom = ao_scheme_atom_find(syntax_atoms[a]);
if (atom)
ao_scheme_mark_memory(&ao_scheme_atom_type, atom);
}
}
#else
#define ao_scheme_atom_mark_syntax()
#endif
void
ao_scheme_atom_move(void)
{
struct ao_scheme_atom *atom;
ao_scheme_move_memory(&ao_scheme_atom_type, (void **) (void *) &ao_scheme_atoms);
for (atom = ao_scheme_atoms; atom; atom = ao_scheme_poly_atom(atom->next)) {
if (!ao_scheme_is_pool_addr(atom)) {
MDBG_DO(printf("atom out of pool %s\n", atom->name));
break;
}
MDBG_DO(printf("move atom %s\n", atom->name));
ao_scheme_poly_move(&atom->next, 0);
}
}
void
ao_scheme_atom_check_references(void)
{
struct ao_scheme_atom *atom;
ao_poly *prev = NULL;
ao_scheme_atom_mark_syntax();
for (atom = ao_scheme_atoms; atom; atom = ao_scheme_poly_atom(atom->next)) {
if (!ao_scheme_marked(atom)) {
MDBG_DO(printf("unreferenced atom %s\n", atom->name));
if (prev)
*prev = atom->next;
else
ao_scheme_atoms = ao_scheme_poly_atom(atom->next);
} else
prev = &atom->next;
}
}
static void
ao_scheme_atom_init(struct ao_scheme_atom *atom, char *name)
{
if (atom) {
atom->type = AO_SCHEME_ATOM;
strcpy(atom->name, name);
atom->next = ao_scheme_atom_poly(ao_scheme_atoms);
ao_scheme_atoms = atom;
}
}
struct ao_scheme_atom *
ao_scheme_string_to_atom(struct ao_scheme_string *string)
{
struct ao_scheme_atom *atom = ao_scheme_atom_find(string->val);
if (atom)
return atom;
ao_scheme_string_stash(string);
atom = ao_scheme_alloc(name_size(string->val));
string = ao_scheme_string_fetch();
ao_scheme_atom_init(atom, string->val);
return atom;
}
struct ao_scheme_atom *
ao_scheme_atom_intern(char *name)
{
struct ao_scheme_atom *atom = ao_scheme_atom_find(name);
if (atom)
return atom;
atom = ao_scheme_alloc(name_size(name));
ao_scheme_atom_init(atom, name);
return atom;
}
ao_poly *
ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref)
{
ao_poly *ref;
struct ao_scheme_frame *frame;
for (frame = ao_scheme_frame_current; frame; frame = ao_scheme_poly_frame(frame->prev)) {
ref = ao_scheme_frame_ref(frame, atom);
if (ref) {
if (frame_ref)
*frame_ref = frame;
return ref;
}
}
ref = ao_scheme_frame_ref(ao_scheme_frame_global, atom);
if (ref)
if (frame_ref)
*frame_ref = ao_scheme_frame_global;
return ref;
}
ao_poly
ao_scheme_atom_get(ao_poly atom)
{
ao_poly *ref = ao_scheme_atom_ref(atom, NULL);
#ifdef ao_builtin_frame
if (!ref)
ref = ao_scheme_frame_ref(ao_scheme_poly_frame(ao_builtin_frame), atom);
#endif
if (ref)
return *ref;
return ao_scheme_error(AO_SCHEME_UNDEFINED, "undefined atom %s", ao_scheme_poly_atom(atom)->name);
}
ao_poly
ao_scheme_atom_def(ao_poly atom, ao_poly val)
{
struct ao_scheme_frame *frame;
ao_poly *ref = ao_scheme_atom_ref(atom, &frame);
if (ref) {
if (frame == ao_scheme_frame_current)
return ao_scheme_error(AO_SCHEME_REDEFINED, "attempt to redefine atom %s", ao_scheme_poly_atom(atom)->name);
*ref = val;
return val;
}
return ao_scheme_frame_add(ao_scheme_frame_current ? ao_scheme_frame_current : ao_scheme_frame_global, atom, val);
}
void
ao_scheme_atom_write(FILE *out, ao_poly a, bool write)
{
struct ao_scheme_atom *atom = ao_scheme_poly_atom(a);
(void) write;
fprintf(out, "%s", atom->name);
}
ao_poly
ao_scheme_do_symbolp(struct ao_scheme_cons *cons)
{
return ao_scheme_do_typep(_ao_scheme_atom_symbol3f, AO_SCHEME_ATOM, cons);
}
ao_poly
ao_scheme_do_set(struct ao_scheme_cons *cons)
{
ao_poly atom;
ao_poly val;
ao_poly *ref;
if (!ao_scheme_parse_args(_ao_scheme_atom_set, cons,
AO_SCHEME_ATOM|AO_SCHEME_ARG_RET_POLY, &atom,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
ref = ao_scheme_atom_ref(atom, NULL);
if (!ref)
return ao_scheme_error(AO_SCHEME_UNDEFINED, "%v: undefined atom %v",
_ao_scheme_atom_set, atom);
*ref = val;
return val;
}
ao_poly
ao_scheme_do_def(struct ao_scheme_cons *cons)
{
ao_poly atom;
ao_poly val;
if (!ao_scheme_parse_args(_ao_scheme_atom_set, cons,
AO_SCHEME_ATOM|AO_SCHEME_ARG_RET_POLY, &atom,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_atom_def(atom, val);
}
ao_poly
ao_scheme_do_setq(struct ao_scheme_cons *cons)
{
ao_poly atom;
ao_poly val;
ao_poly p;
if (!ao_scheme_parse_args(_ao_scheme_atom_set21, cons,
AO_SCHEME_ATOM|AO_SCHEME_ARG_RET_POLY, &atom,
AO_SCHEME_POLY, &val,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
if (!ao_scheme_atom_ref(atom, NULL))
return ao_scheme_error(AO_SCHEME_INVALID, "%v: symbol %v not defined",
_ao_scheme_atom_set21, atom);
/*
* Build the macro return -- `(set (quote ,atom) ,val)
*/
ao_scheme_poly_stash(cons->cdr);
p = ao_scheme_cons(atom, AO_SCHEME_NIL);
p = ao_scheme_cons(_ao_scheme_atom_quote, p);
p = ao_scheme_cons(p, ao_scheme_poly_fetch());
return ao_scheme_cons(_ao_scheme_atom_set, p);
}
#ifdef AO_SCHEME_FEATURE_UNDEF
ao_poly
ao_scheme_do_undef(struct ao_scheme_cons *cons)
{
ao_poly atom;
if (!ao_scheme_parse_args(_ao_scheme_atom_set, cons,
AO_SCHEME_ATOM|AO_SCHEME_ARG_RET_POLY, &atom,
AO_SCHEME_ARG_END))
return AO_SCHEME_NIL;
return ao_scheme_frame_del(ao_scheme_frame_global, atom);
}
#endif
|