c70aa1caa0f70dacc4e3c86cdcd74fdd62c58965
[fw/altos] / src / lisp / ao_lisp_cons.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, 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_lisp.h"
16
17 static void cons_mark(void *addr)
18 {
19         struct ao_lisp_cons     *cons = addr;
20
21         for (;;) {
22                 ao_poly cdr = cons->cdr;
23
24                 ao_lisp_poly_mark(cons->car, 1);
25                 if (!cdr)
26                         break;
27                 if (ao_lisp_poly_type(cdr) != AO_LISP_CONS) {
28                         ao_lisp_poly_mark(cdr, 1);
29                         break;
30                 }
31                 cons = ao_lisp_poly_cons(cdr);
32                 if (ao_lisp_mark_memory(&ao_lisp_cons_type, cons))
33                         break;
34         }
35 }
36
37 static int cons_size(void *addr)
38 {
39         (void) addr;
40         return sizeof (struct ao_lisp_cons);
41 }
42
43 static void cons_move(void *addr)
44 {
45         struct ao_lisp_cons     *cons = addr;
46
47         if (!cons)
48                 return;
49
50         for (;;) {
51                 ao_poly                 cdr;
52                 struct ao_lisp_cons     *c;
53                 int     ret;
54
55                 MDBG_MOVE("cons_move start %d (%d, %d)\n",
56                           MDBG_OFFSET(cons), MDBG_OFFSET(ao_lisp_ref(cons->car)), MDBG_OFFSET(ao_lisp_ref(cons->cdr)));
57                 (void) ao_lisp_poly_move(&cons->car, 1);
58                 cdr = cons->cdr;
59                 if (!cdr)
60                         break;
61                 if (ao_lisp_poly_type(cdr) != AO_LISP_CONS) {
62                         (void) ao_lisp_poly_move(&cons->cdr, 1);
63                         break;
64                 }
65                 c = ao_lisp_poly_cons(cdr);
66                 ret = ao_lisp_move_memory(&ao_lisp_cons_type, (void **) &c);
67                 if (c != ao_lisp_poly_cons(cons->cdr))
68                         cons->cdr = ao_lisp_cons_poly(c);
69                 MDBG_MOVE("cons_move end %d (%d, %d)\n",
70                           MDBG_OFFSET(cons), MDBG_OFFSET(ao_lisp_ref(cons->car)), MDBG_OFFSET(ao_lisp_ref(cons->cdr)));
71                 if (ret)
72                         break;
73                 cons = c;
74         }
75 }
76
77 const struct ao_lisp_type ao_lisp_cons_type = {
78         .mark = cons_mark,
79         .size = cons_size,
80         .move = cons_move,
81         .name = "cons",
82 };
83
84 struct ao_lisp_cons *ao_lisp_cons_free_list;
85
86 struct ao_lisp_cons *
87 ao_lisp_cons_cons(ao_poly car, ao_poly cdr)
88 {
89         struct ao_lisp_cons     *cons;
90
91         if (ao_lisp_cons_free_list) {
92                 cons = ao_lisp_cons_free_list;
93                 ao_lisp_cons_free_list = ao_lisp_poly_cons(cons->cdr);
94         } else {
95                 ao_lisp_poly_stash(0, car);
96                 ao_lisp_poly_stash(1, cdr);
97                 cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
98                 car = ao_lisp_poly_fetch(0);
99                 cdr = ao_lisp_poly_fetch(1);
100                 if (!cons)
101                         return NULL;
102         }
103         cons->car = car;
104         cons->cdr = cdr;
105         return cons;
106 }
107
108 struct ao_lisp_cons *
109 ao_lisp_cons_cdr(struct ao_lisp_cons *cons)
110 {
111         ao_poly cdr = cons->cdr;
112         if (cdr == AO_LISP_NIL)
113                 return NULL;
114         if (ao_lisp_poly_type(cdr) != AO_LISP_CONS) {
115                 (void) ao_lisp_error(AO_LISP_INVALID, "improper list");
116                 return NULL;
117         }
118         return ao_lisp_poly_cons(cdr);
119 }
120
121 ao_poly
122 ao_lisp__cons(ao_poly car, ao_poly cdr)
123 {
124         return ao_lisp_cons_poly(ao_lisp_cons_cons(car, cdr));
125 }
126
127 void
128 ao_lisp_cons_free(struct ao_lisp_cons *cons)
129 {
130         while (cons) {
131                 ao_poly cdr = cons->cdr;
132                 cons->cdr = ao_lisp_cons_poly(ao_lisp_cons_free_list);
133                 ao_lisp_cons_free_list = cons;
134                 cons = ao_lisp_poly_cons(cdr);
135         }
136 }
137
138 void
139 ao_lisp_cons_write(ao_poly c)
140 {
141         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
142         int     first = 1;
143         printf("(");
144         while (cons) {
145                 if (!first)
146                         printf(" ");
147                 ao_lisp_poly_write(cons->car);
148                 c = cons->cdr;
149                 if (ao_lisp_poly_type(c) == AO_LISP_CONS) {
150                         cons = ao_lisp_poly_cons(c);
151                         first = 0;
152                 } else {
153                         printf(" . ");
154                         ao_lisp_poly_write(c);
155                         cons = NULL;
156                 }
157         }
158         printf(")");
159 }
160
161 void
162 ao_lisp_cons_display(ao_poly c)
163 {
164         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
165
166         while (cons) {
167                 ao_lisp_poly_display(cons->car);
168                 cons = ao_lisp_poly_cons(cons->cdr);
169         }
170 }
171
172 int
173 ao_lisp_cons_length(struct ao_lisp_cons *cons)
174 {
175         int     len = 0;
176         while (cons) {
177                 len++;
178                 cons = ao_lisp_poly_cons(cons->cdr);
179         }
180         return len;
181 }