2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 #include "ao_scheme.h"
20 static int name_size(char *name)
22 return sizeof(struct ao_scheme_atom) + strlen(name) + 1;
25 static int atom_size(void *addr)
27 struct ao_scheme_atom *atom = addr;
30 return name_size(atom->name);
33 static void atom_mark(void *addr)
35 struct ao_scheme_atom *atom = addr;
38 atom = ao_scheme_poly_atom(atom->next);
41 if (ao_scheme_mark_memory(&ao_scheme_atom_type, atom))
46 static void atom_move(void *addr)
48 struct ao_scheme_atom *atom = addr;
52 struct ao_scheme_atom *next = ao_scheme_poly_atom(atom->next);
56 ret = ao_scheme_move_memory(&ao_scheme_atom_type, (void **) &next);
57 if (next != ao_scheme_poly_atom(atom->next))
58 atom->next = ao_scheme_atom_poly(next);
65 const struct ao_scheme_type ao_scheme_atom_type = {
72 struct ao_scheme_atom *ao_scheme_atoms;
74 struct ao_scheme_atom *
75 ao_scheme_atom_intern(char *name)
77 struct ao_scheme_atom *atom;
79 for (atom = ao_scheme_atoms; atom; atom = ao_scheme_poly_atom(atom->next)) {
80 if (!strcmp(atom->name, name))
83 #ifdef ao_builtin_atoms
84 for (atom = ao_scheme_poly_atom(ao_builtin_atoms); atom; atom = ao_scheme_poly_atom(atom->next)) {
85 if (!strcmp(atom->name, name))
89 ao_scheme_string_stash(0, name);
90 atom = ao_scheme_alloc(name_size(name));
91 name = ao_scheme_string_fetch(0);
93 atom->type = AO_SCHEME_ATOM;
94 atom->next = ao_scheme_atom_poly(ao_scheme_atoms);
95 ao_scheme_atoms = atom;
96 strcpy(atom->name, name);
102 ao_scheme_atom_ref(ao_poly atom, struct ao_scheme_frame **frame_ref)
105 struct ao_scheme_frame *frame;
107 for (frame = ao_scheme_frame_current; frame; frame = ao_scheme_poly_frame(frame->prev)) {
108 ref = ao_scheme_frame_ref(frame, atom);
115 ref = ao_scheme_frame_ref(ao_scheme_frame_global, atom);
118 *frame_ref = ao_scheme_frame_global;
123 ao_scheme_atom_get(ao_poly atom)
125 ao_poly *ref = ao_scheme_atom_ref(atom, NULL);
127 #ifdef ao_builtin_frame
129 ref = ao_scheme_frame_ref(ao_scheme_poly_frame(ao_builtin_frame), atom);
133 return ao_scheme_error(AO_SCHEME_UNDEFINED, "undefined atom %s", ao_scheme_poly_atom(atom)->name);
137 ao_scheme_atom_set(ao_poly atom, ao_poly val)
139 ao_poly *ref = ao_scheme_atom_ref(atom, NULL);
142 return ao_scheme_error(AO_SCHEME_UNDEFINED, "undefined atom %s", ao_scheme_poly_atom(atom)->name);
148 ao_scheme_atom_def(ao_poly atom, ao_poly val)
150 struct ao_scheme_frame *frame;
151 ao_poly *ref = ao_scheme_atom_ref(atom, &frame);
154 if (frame == ao_scheme_frame_current)
155 return ao_scheme_error(AO_SCHEME_REDEFINED, "attempt to redefine atom %s", ao_scheme_poly_atom(atom)->name);
159 return ao_scheme_frame_add(ao_scheme_frame_current ? ao_scheme_frame_current : ao_scheme_frame_global, atom, val);
163 ao_scheme_atom_write(ao_poly a)
165 struct ao_scheme_atom *atom = ao_scheme_poly_atom(a);
166 printf("%s", atom->name);