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.
20 static int name_size(char *name)
22 return sizeof(struct ao_lisp_atom) + strlen(name) + 1;
25 static int atom_size(void *addr)
27 struct ao_lisp_atom *atom = addr;
30 return name_size(atom->name);
33 static void atom_mark(void *addr)
35 struct ao_lisp_atom *atom = addr;
38 atom = ao_lisp_poly_atom(atom->next);
41 if (ao_lisp_mark_memory(&ao_lisp_atom_type, atom))
46 static void atom_move(void *addr)
48 struct ao_lisp_atom *atom = addr;
52 struct ao_lisp_atom *next = ao_lisp_poly_atom(atom->next);
56 ret = ao_lisp_move_memory(&ao_lisp_atom_type, (void **) &next);
57 if (next != ao_lisp_poly_atom(atom->next))
58 atom->next = ao_lisp_atom_poly(next);
65 const struct ao_lisp_type ao_lisp_atom_type = {
72 struct ao_lisp_atom *ao_lisp_atoms;
75 ao_lisp_atom_intern(char *name)
77 struct ao_lisp_atom *atom;
79 for (atom = ao_lisp_atoms; atom; atom = ao_lisp_poly_atom(atom->next)) {
80 if (!strcmp(atom->name, name))
83 #ifdef ao_builtin_atoms
84 for (atom = ao_lisp_poly_atom(ao_builtin_atoms); atom; atom = ao_lisp_poly_atom(atom->next)) {
85 if (!strcmp(atom->name, name))
89 ao_lisp_string_stash(0, name);
90 atom = ao_lisp_alloc(name_size(name));
91 name = ao_lisp_string_fetch(0);
93 atom->type = AO_LISP_ATOM;
94 atom->next = ao_lisp_atom_poly(ao_lisp_atoms);
96 strcpy(atom->name, name);
101 struct ao_lisp_frame *ao_lisp_frame_global;
102 struct ao_lisp_frame *ao_lisp_frame_current;
105 ao_lisp_atom_init(void)
107 if (!ao_lisp_frame_global)
108 ao_lisp_frame_global = ao_lisp_frame_new(0);
112 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom)
117 ref = ao_lisp_frame_ref(frame, atom);
120 frame = ao_lisp_poly_frame(frame->prev);
122 if (ao_lisp_frame_global) {
123 ref = ao_lisp_frame_ref(ao_lisp_frame_global, atom);
131 ao_lisp_atom_get(ao_poly atom)
133 ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_current, atom);
135 if (!ref && ao_lisp_frame_global)
136 ref = ao_lisp_frame_ref(ao_lisp_frame_global, atom);
137 #ifdef ao_builtin_frame
139 ref = ao_lisp_frame_ref(ao_lisp_poly_frame(ao_builtin_frame), atom);
143 return ao_lisp_error(AO_LISP_UNDEFINED, "undefined atom %s", ao_lisp_poly_atom(atom)->name);
147 ao_lisp_atom_set(ao_poly atom, ao_poly val)
149 ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_current, atom);
151 if (!ref && ao_lisp_frame_global)
152 ref = ao_lisp_frame_ref(ao_lisp_frame_global, atom);
156 ao_lisp_frame_add(&ao_lisp_frame_global, atom, val);
161 ao_lisp_atom_print(ao_poly a)
163 struct ao_lisp_atom *atom = ao_lisp_poly_atom(a);
164 printf("%s", atom->name);