altos/lisp: Add non-cons cdr support
[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_lisp_poly_mark(cons->car, 1);
23                 cons = ao_lisp_poly_cons(cons->cdr);
24                 if (!cons)
25                         break;
26                 if (ao_lisp_mark_memory(&ao_lisp_cons_type, cons))
27                         break;
28         }
29 }
30
31 static int cons_size(void *addr)
32 {
33         (void) addr;
34         return sizeof (struct ao_lisp_cons);
35 }
36
37 static void cons_move(void *addr)
38 {
39         struct ao_lisp_cons     *cons = addr;
40
41         if (!cons)
42                 return;
43
44         for (;;) {
45                 struct ao_lisp_cons     *cdr;
46                 int                     ret;
47
48                 MDBG_MOVE("cons_move start %d (%d, %d)\n",
49                           MDBG_OFFSET(cons), MDBG_OFFSET(ao_lisp_ref(cons->car)), MDBG_OFFSET(ao_lisp_ref(cons->cdr)));
50                 (void) ao_lisp_poly_move(&cons->car, 1);
51                 cdr = ao_lisp_poly_cons(cons->cdr);
52                 if (!cdr)
53                         break;
54                 ret = ao_lisp_move_memory(&ao_lisp_cons_type, (void **) &cdr);
55                 if (cdr != ao_lisp_poly_cons(cons->cdr))
56                         cons->cdr = ao_lisp_cons_poly(cdr);
57                 MDBG_MOVE("cons_move end %d (%d, %d)\n",
58                           MDBG_OFFSET(cons), MDBG_OFFSET(ao_lisp_ref(cons->car)), MDBG_OFFSET(ao_lisp_ref(cons->cdr)));
59                 if (ret)
60                         break;
61                 cons = cdr;
62         }
63 }
64
65 const struct ao_lisp_type ao_lisp_cons_type = {
66         .mark = cons_mark,
67         .size = cons_size,
68         .move = cons_move,
69         .name = "cons",
70 };
71
72 struct ao_lisp_cons *ao_lisp_cons_free_list;
73
74 struct ao_lisp_cons *
75 ao_lisp_cons_cons(ao_poly car, ao_poly cdr)
76 {
77         struct ao_lisp_cons     *cons;
78
79         if (ao_lisp_cons_free_list) {
80                 cons = ao_lisp_cons_free_list;
81                 ao_lisp_cons_free_list = ao_lisp_poly_cons(cons->cdr);
82         } else {
83                 ao_lisp_poly_stash(0, car);
84                 ao_lisp_poly_stash(1, cdr);
85                 cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
86                 car = ao_lisp_poly_fetch(0);
87                 cdr = ao_lisp_poly_fetch(1);
88                 if (!cons)
89                         return NULL;
90         }
91         cons->car = car;
92         cons->cdr = cdr;
93         return cons;
94 }
95
96 ao_poly
97 ao_lisp__cons(ao_poly car, ao_poly cdr)
98 {
99         return ao_lisp_cons_poly(ao_lisp_cons_cons(car, cdr));
100 }
101
102 void
103 ao_lisp_cons_free(struct ao_lisp_cons *cons)
104 {
105         while (cons) {
106                 ao_poly cdr = cons->cdr;
107                 cons->cdr = ao_lisp_cons_poly(ao_lisp_cons_free_list);
108                 ao_lisp_cons_free_list = cons;
109                 cons = ao_lisp_poly_cons(cdr);
110         }
111 }
112
113 void
114 ao_lisp_cons_print(ao_poly c)
115 {
116         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
117         int     first = 1;
118         printf("(");
119         while (cons) {
120                 if (!first)
121                         printf(" ");
122                 ao_lisp_poly_print(cons->car);
123                 c = cons->cdr;
124                 if (ao_lisp_poly_type(c) == AO_LISP_CONS) {
125                         cons = ao_lisp_poly_cons(c);
126                         first = 0;
127                 } else {
128                         printf(" . ");
129                         ao_lisp_poly_print(c);
130                         cons = NULL;
131                 }
132         }
133         printf(")");
134 }
135
136 void
137 ao_lisp_cons_patom(ao_poly c)
138 {
139         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
140
141         while (cons) {
142                 ao_lisp_poly_patom(cons->car);
143                 cons = ao_lisp_poly_cons(cons->cdr);
144         }
145 }
146
147 int
148 ao_lisp_cons_length(struct ao_lisp_cons *cons)
149 {
150         int     len = 0;
151         while (cons) {
152                 len++;
153                 cons = ao_lisp_poly_cons(cons->cdr);
154         }
155         return len;
156 }