d726321ce2f9e0ddafd95ceec0a14b1b5f15200a
[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 };
72
73 static const struct ao_scheme_funcs *
74 funcs(ao_poly p)
75 {
76         uint8_t type = ao_scheme_poly_type(p);
77
78         if (type < AO_SCHEME_NUM_TYPE)
79                 return &ao_scheme_funcs[type];
80         return NULL;
81 }
82
83 void
84 ao_scheme_poly_write(ao_poly p)
85 {
86         const struct ao_scheme_funcs *f = funcs(p);
87
88         if (f && f->write)
89                 f->write(p);
90 }
91
92 void
93 ao_scheme_poly_display(ao_poly p)
94 {
95         const struct ao_scheme_funcs *f = funcs(p);
96
97         if (f && f->display)
98                 f->display(p);
99 }
100
101 void *
102 ao_scheme_ref(ao_poly poly) {
103         if (poly == AO_SCHEME_NIL)
104                 return NULL;
105         if (poly & AO_SCHEME_CONST)
106                 return (void *) (ao_scheme_const + (poly & AO_SCHEME_REF_MASK) - 4);
107         return (void *) (ao_scheme_pool + (poly & AO_SCHEME_REF_MASK) - 4);
108 }
109
110 ao_poly
111 ao_scheme_poly(const void *addr, ao_poly type) {
112         const uint8_t   *a = addr;
113         if (a == NULL)
114                 return AO_SCHEME_NIL;
115         if (AO_SCHEME_IS_CONST(a))
116                 return AO_SCHEME_CONST | (a - ao_scheme_const + 4) | type;
117         return (a - ao_scheme_pool + 4) | type;
118 }