altos/scheme: Add ports. Split scheme code up.
[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(FILE *out, ao_poly p, bool write)
19 {
20         int i = ao_scheme_poly_int(p);
21         (void) write;
22         fprintf(out, "%d", i);
23 }
24
25 ao_poly
26 ao_scheme_do_integerp(struct ao_scheme_cons *cons)
27 {
28 #ifdef AO_SCHEME_FEATURE_BIGINT
29         ao_poly val;
30
31         if (!ao_scheme_parse_args(_ao_scheme_atom_pair3f, cons,
32                                   AO_SCHEME_POLY, &val,
33                                   AO_SCHEME_ARG_END))
34                 return AO_SCHEME_NIL;
35         switch (ao_scheme_poly_type(val)) {
36         case AO_SCHEME_INT:
37         case AO_SCHEME_BIGINT:
38                 return _ao_scheme_bool_true;
39         default:
40                 return _ao_scheme_bool_false;
41         }
42 #else
43         return ao_scheme_do_typep(_ao_scheme_atom_integer3f, AO_SCHEME_INT, cons);
44 #endif
45 }
46
47 ao_poly
48 ao_scheme_do_numberp(struct ao_scheme_cons *cons)
49 {
50 #if defined(AO_SCHEME_FEATURE_BIGINT) || defined(AO_SCHEME_FEATURE_FLOAT)
51         ao_poly val;
52
53         if (!ao_scheme_parse_args(_ao_scheme_atom_pair3f, cons,
54                                   AO_SCHEME_POLY, &val,
55                                   AO_SCHEME_ARG_END))
56                 return AO_SCHEME_NIL;
57         switch (ao_scheme_poly_type(val)) {
58         case AO_SCHEME_INT:
59 #ifdef AO_SCHEME_FEATURE_BIGINT
60         case AO_SCHEME_BIGINT:
61 #endif
62 #ifdef AO_SCHEME_FEATURE_FLOAT
63         case AO_SCHEME_FLOAT:
64 #endif
65                 return _ao_scheme_bool_true;
66         default:
67                 return _ao_scheme_bool_false;
68         }
69 #else
70         return ao_scheme_do_integerp(cons);
71 #endif
72 }
73
74 #ifdef AO_SCHEME_FEATURE_BIGINT
75
76 int32_t
77 ao_scheme_poly_integer(ao_poly p)
78 {
79         switch (ao_scheme_poly_base_type(p)) {
80         case AO_SCHEME_INT:
81                 return ao_scheme_poly_int(p);
82         case AO_SCHEME_BIGINT:
83                 return ao_scheme_poly_bigint(p)->value;
84         }
85         return 0;
86 }
87
88 ao_poly
89 ao_scheme_integer_poly(int32_t p)
90 {
91         struct ao_scheme_bigint *bi;
92
93         if (AO_SCHEME_MIN_INT <= p && p <= AO_SCHEME_MAX_INT)
94                 return ao_scheme_int_poly(p);
95         bi = ao_scheme_alloc(sizeof (struct ao_scheme_bigint));
96         bi->value = p;
97         return ao_scheme_bigint_poly(bi);
98 }
99
100 static void bigint_mark(void *addr)
101 {
102         (void) addr;
103 }
104
105 static int bigint_size(void *addr)
106 {
107         if (!addr)
108                 return 0;
109         return sizeof (struct ao_scheme_bigint);
110 }
111
112 static void bigint_move(void *addr)
113 {
114         (void) addr;
115 }
116
117 const struct ao_scheme_type ao_scheme_bigint_type = {
118         .mark = bigint_mark,
119         .size = bigint_size,
120         .move = bigint_move,
121         .name = "bigint",
122 };
123
124 void
125 ao_scheme_bigint_write(FILE *out, ao_poly p, bool write)
126 {
127         struct ao_scheme_bigint *bi = ao_scheme_poly_bigint(p);
128
129         (void) write;
130         fprintf(out, "%d", bi->value);
131 }
132 #endif /* AO_SCHEME_FEATURE_BIGINT */