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,
71 [AO_SCHEME_VECTOR] = {
72 .write = ao_scheme_vector_write,
73 .display = ao_scheme_vector_display
77 static const struct ao_scheme_funcs *
80 uint8_t type = ao_scheme_poly_type(p);
82 if (type < AO_SCHEME_NUM_TYPE)
83 return &ao_scheme_funcs[type];
88 ao_scheme_poly_write(ao_poly p)
90 const struct ao_scheme_funcs *f = funcs(p);
97 ao_scheme_poly_display(ao_poly p)
99 const struct ao_scheme_funcs *f = funcs(p);
106 ao_scheme_ref(ao_poly poly) {
107 if (poly == AO_SCHEME_NIL)
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);
115 ao_scheme_poly(const void *addr, ao_poly type) {
116 const uint8_t *a = addr;
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;