altos/scheme: Support scheme subsetting via feature settings
[fw/altos] / src / scheme / ao_scheme_int.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 void
18 ao_scheme_int_write(ao_poly p)
19 {
20         int i = ao_scheme_poly_int(p);
21         printf("%d", i);
22 }
23
24 #ifdef AO_SCHEME_FEATURE_BIGINT
25
26 int32_t
27 ao_scheme_poly_integer(ao_poly p)
28 {
29         switch (ao_scheme_poly_base_type(p)) {
30         case AO_SCHEME_INT:
31                 return ao_scheme_poly_int(p);
32         case AO_SCHEME_OTHER:
33                 if (ao_scheme_other_type(ao_scheme_poly_other(p)) == AO_SCHEME_BIGINT)
34                         return ao_scheme_bigint_int(ao_scheme_poly_bigint(p)->value);
35         }
36         return AO_SCHEME_NOT_INTEGER;
37 }
38
39 ao_poly
40 ao_scheme_integer_poly(int32_t p)
41 {
42         struct ao_scheme_bigint *bi;
43
44         if (AO_SCHEME_MIN_INT <= p && p <= AO_SCHEME_MAX_INT)
45                 return ao_scheme_int_poly(p);
46         bi = ao_scheme_alloc(sizeof (struct ao_scheme_bigint));
47         bi->value = ao_scheme_int_bigint(p);
48         return ao_scheme_bigint_poly(bi);
49 }
50
51 static void bigint_mark(void *addr)
52 {
53         (void) addr;
54 }
55
56 static int bigint_size(void *addr)
57 {
58         if (!addr)
59                 return 0;
60         return sizeof (struct ao_scheme_bigint);
61 }
62
63 static void bigint_move(void *addr)
64 {
65         (void) addr;
66 }
67
68 const struct ao_scheme_type ao_scheme_bigint_type = {
69         .mark = bigint_mark,
70         .size = bigint_size,
71         .move = bigint_move,
72         .name = "bigint",
73 };
74
75 void
76 ao_scheme_bigint_write(ao_poly p)
77 {
78         struct ao_scheme_bigint *bi = ao_scheme_poly_bigint(p);
79
80         printf("%d", ao_scheme_bigint_int(bi->value));
81 }
82 #endif /* AO_SCHEME_FEATURE_BIGINT */