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 #ifdef AO_SCHEME_FEATURE_BIGINT
28 [AO_SCHEME_BIGINT] = {
29 .write = ao_scheme_bigint_write,
30 .display = ao_scheme_bigint_write,
34 .write = ao_scheme_int_write,
35 .display = ao_scheme_int_write,
38 .write = ao_scheme_atom_write,
39 .display = ao_scheme_atom_write,
41 [AO_SCHEME_BUILTIN] = {
42 .write = ao_scheme_builtin_write,
43 .display = ao_scheme_builtin_write,
46 .write = ao_scheme_frame_write,
47 .display = ao_scheme_frame_write,
49 [AO_SCHEME_FRAME_VALS] = {
53 [AO_SCHEME_LAMBDA] = {
54 .write = ao_scheme_lambda_write,
55 .display = ao_scheme_lambda_write,
58 .write = ao_scheme_stack_write,
59 .display = ao_scheme_stack_write,
62 .write = ao_scheme_bool_write,
63 .display = ao_scheme_bool_write,
65 [AO_SCHEME_STRING] = {
66 .write = ao_scheme_string_write,
67 .display = ao_scheme_string_display,
69 #ifdef AO_SCHEME_FEATURE_FLOAT
71 .write = ao_scheme_float_write,
72 .display = ao_scheme_float_write,
75 #ifdef AO_SCHEME_FEATURE_VECTOR
76 [AO_SCHEME_VECTOR] = {
77 .write = ao_scheme_vector_write,
78 .display = ao_scheme_vector_display
83 static void ao_scheme_invalid_write(ao_poly p) {
84 printf("??? 0x%04x ???", p);
88 static const struct ao_scheme_funcs ao_scheme_invalid_funcs = {
89 .write = ao_scheme_invalid_write,
90 .display = ao_scheme_invalid_write,
93 static const struct ao_scheme_funcs *
96 uint8_t type = ao_scheme_poly_type(p);
98 if (type < AO_SCHEME_NUM_TYPE)
99 return &ao_scheme_funcs[type];
100 return &ao_scheme_invalid_funcs;
103 void (*ao_scheme_poly_write_func(ao_poly p))(ao_poly p)
105 return funcs(p)->write;
108 void (*ao_scheme_poly_display_func(ao_poly p))(ao_poly p)
110 return funcs(p)->display;
114 ao_scheme_ref(ao_poly poly) {
115 if (poly == AO_SCHEME_NIL)
117 if (poly & AO_SCHEME_CONST)
118 return (void *) (ao_scheme_const + (poly & AO_SCHEME_REF_MASK) - 4);
119 return (void *) (ao_scheme_pool + (poly & AO_SCHEME_REF_MASK) - 4);
123 ao_scheme_poly(const void *addr, ao_poly type) {
124 const uint8_t *a = addr;
126 return AO_SCHEME_NIL;
127 if (AO_SCHEME_IS_CONST(a))
128 return AO_SCHEME_CONST | (a - ao_scheme_const + 4) | type;
129 return (a - ao_scheme_pool + 4) | type;