altos/scheme: Add ports. Split scheme code up.
[fw/altos] / src / scheme / ao_scheme_bool.c
1 /*
2  * Copyright © 2017 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 static void bool_mark(void *addr)
18 {
19         (void) addr;
20 }
21
22 static int bool_size(void *addr)
23 {
24         (void) addr;
25         return sizeof (struct ao_scheme_bool);
26 }
27
28 static void bool_move(void *addr)
29 {
30         (void) addr;
31 }
32
33 const struct ao_scheme_type ao_scheme_bool_type = {
34         .mark = bool_mark,
35         .size = bool_size,
36         .move = bool_move,
37         .name = "bool"
38 };
39
40 void
41 ao_scheme_bool_write(FILE *out, ao_poly v, bool write)
42 {
43         struct ao_scheme_bool   *b = ao_scheme_poly_bool(v);
44
45         (void) write;
46         if (b->value)
47                 fprintf(out, "#t");
48         else
49                 fprintf(out, "#f");
50 }
51
52 ao_poly
53 ao_scheme_do_booleanp(struct ao_scheme_cons *cons)
54 {
55         return ao_scheme_do_typep(_ao_scheme_atom_boolean3f, AO_SCHEME_BOOL, cons);
56 }
57
58 #ifdef AO_SCHEME_MAKE_CONST
59
60 struct ao_scheme_bool   *ao_scheme_true, *ao_scheme_false;
61
62 struct ao_scheme_bool *
63 ao_scheme_bool_get(uint8_t value)
64 {
65         struct ao_scheme_bool   **b;
66
67         if (value)
68                 b = &ao_scheme_true;
69         else
70                 b = &ao_scheme_false;
71
72         if (!*b) {
73                 *b = ao_scheme_alloc(sizeof (struct ao_scheme_bool));
74                 (*b)->type = AO_SCHEME_BOOL;
75                 (*b)->value = value;
76         }
77         return *b;
78 }
79
80 #endif