altos/lisp: Change lisp objects to use ao_poly everywhere. Add const
[fw/altos] / src / lisp / ao_lisp_atom.c
1 /*
2  * Copyright © 2016 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include "ao_lisp.h"
19
20 static int name_size(char *name)
21 {
22         return sizeof(struct ao_lisp_atom) + strlen(name) + 1;
23 }
24
25 static int atom_size(void *addr)
26 {
27         struct ao_lisp_atom     *atom = addr;
28         if (!atom)
29                 return 0;
30         return name_size(atom->name);
31 }
32
33 static void atom_mark(void *addr)
34 {
35         struct ao_lisp_atom     *atom = addr;
36
37         for (;;) {
38                 ao_lisp_poly_mark(atom->val);
39                 atom = ao_lisp_poly_atom(atom->next);
40                 if (!atom)
41                         break;
42                 if (ao_lisp_mark_memory(atom, atom_size(atom)))
43                         break;
44         }
45 }
46
47 static void atom_move(void *addr)
48 {
49         struct ao_lisp_atom     *atom = addr;
50
51         for (;;) {
52                 struct ao_lisp_atom     *next;
53
54                 atom->val = ao_lisp_poly_move(atom->val);
55                 next = ao_lisp_poly_atom(atom->next);
56                 next = ao_lisp_move_memory(next, atom_size(next));
57                 if (!next)
58                         break;
59                 atom->next = ao_lisp_atom_poly(next);
60                 atom = next;
61         }
62 }
63
64 const struct ao_lisp_type ao_lisp_atom_type = {
65         .mark = atom_mark,
66         .size = atom_size,
67         .move = atom_move,
68 };
69
70 struct ao_lisp_atom     *ao_lisp_atoms;
71
72 struct ao_lisp_atom *
73 ao_lisp_atom_intern(char *name)
74 {
75         struct ao_lisp_atom     *atom;
76 //      int                     b;
77
78         for (atom = ao_lisp_atoms; atom; atom = ao_lisp_poly_atom(atom->next)) {
79                 if (!strcmp(atom->name, name))
80                         return atom;
81         }
82 #ifdef ao_builtin_atoms
83         for (atom = ao_lisp_poly_atom(ao_builtin_atoms); atom; atom = ao_lisp_poly_atom(atom->next)) {
84                 if (!strcmp(atom->name, name))
85                         return atom;
86         }
87 #endif
88         if (!ao_lisp_atoms)
89                 ao_lisp_root_add(&ao_lisp_atom_type, (void **) &ao_lisp_atoms);
90         atom = ao_lisp_alloc(name_size(name));
91         if (atom) {
92                 atom->type = AO_LISP_ATOM;
93                 atom->next = ao_lisp_atom_poly(ao_lisp_atoms);
94                 ao_lisp_atoms = atom;
95                 strcpy(atom->name, name);
96                 atom->val = AO_LISP_NIL;
97         }
98         return atom;
99 }
100
101 void
102 ao_lisp_atom_print(ao_poly a)
103 {
104         struct ao_lisp_atom *atom = ao_lisp_poly_atom(a);
105         printf("%s", atom->name);
106 }