altos/lisp: add length, pack, unpack and flush
[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 #define OFFSET(a)       ((int) ((uint8_t *) (a) - ao_lisp_const))
18
19 static void cons_mark(void *addr)
20 {
21         struct ao_lisp_cons     *cons = addr;
22
23         for (;;) {
24                 ao_lisp_poly_mark(cons->car, 1);
25                 cons = ao_lisp_poly_cons(cons->cdr);
26                 if (!cons)
27                         break;
28                 if (ao_lisp_mark_memory(cons, sizeof (struct ao_lisp_cons)))
29                         break;
30         }
31 }
32
33 static int cons_size(void *addr)
34 {
35         (void) addr;
36         return sizeof (struct ao_lisp_cons);
37 }
38
39 static void cons_move(void *addr)
40 {
41         struct ao_lisp_cons     *cons = addr;
42
43         if (!cons)
44                 return;
45
46         for (;;) {
47                 struct ao_lisp_cons     *cdr;
48                 int                     ret;
49
50                 (void) ao_lisp_poly_move(&cons->car, 1);
51                 cdr = ao_lisp_poly_cons(cons->cdr);
52                 ret = ao_lisp_move_memory((void **) &cdr, sizeof (struct ao_lisp_cons));
53                 if (cdr != ao_lisp_poly_cons(cons->cdr))
54                         cons->cdr = ao_lisp_cons_poly(cdr);
55                 if (ret)
56                         break;
57                 cons = cdr;
58         }
59 }
60
61 const struct ao_lisp_type ao_lisp_cons_type = {
62         .mark = cons_mark,
63         .size = cons_size,
64         .move = cons_move,
65 };
66
67 struct ao_lisp_cons *
68 ao_lisp_cons_cons(ao_poly car, struct ao_lisp_cons *cdr)
69 {
70         struct ao_lisp_cons     *cons;
71
72         ao_lisp_root_add(&ao_lisp_cons_type, &cdr);
73         ao_lisp_root_poly_add(&car);
74         cons = ao_lisp_alloc(sizeof (struct ao_lisp_cons));
75         ao_lisp_root_clear(&car);
76         ao_lisp_root_clear(&cdr);
77         if (!cons)
78                 return NULL;
79         cons->car = car;
80         cons->cdr = ao_lisp_cons_poly(cdr);
81         return cons;
82 }
83
84 void
85 ao_lisp_cons_print(ao_poly c)
86 {
87         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
88         int     first = 1;
89         printf("(");
90         while (cons) {
91                 if (!first)
92                         printf(" ");
93                 ao_lisp_poly_print(cons->car);
94                 cons = ao_lisp_poly_cons(cons->cdr);
95                 first = 0;
96         }
97         printf(")");
98 }
99
100 void
101 ao_lisp_cons_patom(ao_poly c)
102 {
103         struct ao_lisp_cons *cons = ao_lisp_poly_cons(c);
104
105         while (cons) {
106                 ao_lisp_poly_patom(cons->car);
107                 cons = ao_lisp_poly_cons(cons->cdr);
108         }
109 }
110
111 int
112 ao_lisp_cons_length(struct ao_lisp_cons *cons)
113 {
114         int     len = 0;
115         while (cons) {
116                 len++;
117                 cons = ao_lisp_poly_cons(cons->cdr);
118         }
119         return len;
120 }