Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / scheme / ao_scheme_poly.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_scheme.h"
16
17 struct ao_scheme_funcs {
18         void (*write)(ao_poly);
19         void (*display)(ao_poly);
20 };
21
22 static const struct ao_scheme_funcs ao_scheme_funcs[AO_SCHEME_NUM_TYPE] = {
23         [AO_SCHEME_CONS] = {
24                 .write = ao_scheme_cons_write,
25                 .display = ao_scheme_cons_display,
26         },
27         [AO_SCHEME_STRING] = {
28                 .write = ao_scheme_string_write,
29                 .display = ao_scheme_string_display,
30         },
31         [AO_SCHEME_INT] = {
32                 .write = ao_scheme_int_write,
33                 .display = ao_scheme_int_write,
34         },
35         [AO_SCHEME_ATOM] = {
36                 .write = ao_scheme_atom_write,
37                 .display = ao_scheme_atom_write,
38         },
39         [AO_SCHEME_BUILTIN] = {
40                 .write = ao_scheme_builtin_write,
41                 .display = ao_scheme_builtin_write,
42         },
43         [AO_SCHEME_FRAME] = {
44                 .write = ao_scheme_frame_write,
45                 .display = ao_scheme_frame_write,
46         },
47         [AO_SCHEME_FRAME_VALS] = {
48                 .write = NULL,
49                 .display = NULL,
50         },
51         [AO_SCHEME_LAMBDA] = {
52                 .write = ao_scheme_lambda_write,
53                 .display = ao_scheme_lambda_write,
54         },
55         [AO_SCHEME_STACK] = {
56                 .write = ao_scheme_stack_write,
57                 .display = ao_scheme_stack_write,
58         },
59         [AO_SCHEME_BOOL] = {
60                 .write = ao_scheme_bool_write,
61                 .display = ao_scheme_bool_write,
62         },
63         [AO_SCHEME_BIGINT] = {
64                 .write = ao_scheme_bigint_write,
65                 .display = ao_scheme_bigint_write,
66         },
67         [AO_SCHEME_FLOAT] = {
68                 .write = ao_scheme_float_write,
69                 .display = ao_scheme_float_write,
70         },
71         [AO_SCHEME_VECTOR] = {
72                 .write = ao_scheme_vector_write,
73                 .display = ao_scheme_vector_display
74         },
75 };
76
77 static const struct ao_scheme_funcs *
78 funcs(ao_poly p)
79 {
80         uint8_t type = ao_scheme_poly_type(p);
81
82         if (type < AO_SCHEME_NUM_TYPE)
83                 return &ao_scheme_funcs[type];
84         return NULL;
85 }
86
87 void
88 ao_scheme_poly_write(ao_poly p)
89 {
90         const struct ao_scheme_funcs *f = funcs(p);
91
92         if (f && f->write)
93                 f->write(p);
94 }
95
96 void
97 ao_scheme_poly_display(ao_poly p)
98 {
99         const struct ao_scheme_funcs *f = funcs(p);
100
101         if (f && f->display)
102                 f->display(p);
103 }
104
105 void *
106 ao_scheme_ref(ao_poly poly) {
107         if (poly == AO_SCHEME_NIL)
108                 return NULL;
109         if (poly & AO_SCHEME_CONST)
110                 return (void *) (ao_scheme_const + (poly & AO_SCHEME_REF_MASK) - 4);
111         return (void *) (ao_scheme_pool + (poly & AO_SCHEME_REF_MASK) - 4);
112 }
113
114 ao_poly
115 ao_scheme_poly(const void *addr, ao_poly type) {
116         const uint8_t   *a = addr;
117         if (a == NULL)
118                 return AO_SCHEME_NIL;
119         if (AO_SCHEME_IS_CONST(a))
120                 return AO_SCHEME_CONST | (a - ao_scheme_const + 4) | type;
121         return (a - ao_scheme_pool + 4) | type;
122 }