2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
15 #include "ao_scheme.h"
17 struct ao_scheme_funcs {
18 void (*write)(ao_poly);
19 void (*display)(ao_poly);
22 static const struct ao_scheme_funcs ao_scheme_funcs[AO_SCHEME_NUM_TYPE] = {
24 .write = ao_scheme_cons_write,
25 .display = ao_scheme_cons_display,
27 [AO_SCHEME_STRING] = {
28 .write = ao_scheme_string_write,
29 .display = ao_scheme_string_display,
32 .write = ao_scheme_int_write,
33 .display = ao_scheme_int_write,
36 .write = ao_scheme_atom_write,
37 .display = ao_scheme_atom_write,
39 [AO_SCHEME_BUILTIN] = {
40 .write = ao_scheme_builtin_write,
41 .display = ao_scheme_builtin_write,
44 .write = ao_scheme_frame_write,
45 .display = ao_scheme_frame_write,
47 [AO_SCHEME_FRAME_VALS] = {
51 [AO_SCHEME_LAMBDA] = {
52 .write = ao_scheme_lambda_write,
53 .display = ao_scheme_lambda_write,
56 .write = ao_scheme_stack_write,
57 .display = ao_scheme_stack_write,
60 .write = ao_scheme_bool_write,
61 .display = ao_scheme_bool_write,
63 [AO_SCHEME_BIGINT] = {
64 .write = ao_scheme_bigint_write,
65 .display = ao_scheme_bigint_write,
68 .write = ao_scheme_float_write,
69 .display = ao_scheme_float_write,
73 static const struct ao_scheme_funcs *
76 uint8_t type = ao_scheme_poly_type(p);
78 if (type < AO_SCHEME_NUM_TYPE)
79 return &ao_scheme_funcs[type];
84 ao_scheme_poly_write(ao_poly p)
86 const struct ao_scheme_funcs *f = funcs(p);
93 ao_scheme_poly_display(ao_poly p)
95 const struct ao_scheme_funcs *f = funcs(p);
102 ao_scheme_ref(ao_poly poly) {
103 if (poly == AO_SCHEME_NIL)
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);
111 ao_scheme_poly(const void *addr, ao_poly type) {
112 const uint8_t *a = addr;
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;