Debian Codegraph @

src/c/reader/parse_number.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 *
6 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
7 * Copyright (c) 1990 Giuseppe Attardi
8 * Copyright (c) 2001 Juan Jose Garcia Ripoll
9 *
10 * See file 'LICENSE' for the copyright details.
11 *
12 */
13
14 #include <ecl/ecl.h>
15
16 static bool
17 exponent_charp(cl_fixnum c)
18 {
19 return (c == 'e') || (c == 'E') || (c == 'f') || (c == 'F') ||
20 (c == 's') || (c == 'S') || (c == 'd') || (c == 'D') ||
21 (c == 'l') || (c == 'L');
22 }
23
24 static cl_object
25 expt10(cl_index expt)
26 {
27 cl_object accum = _ecl_big_register0();
28 cl_object factor = _ecl_big_register1();
29 _ecl_big_set_ui(accum, 1);
30 _ecl_big_set_ui(factor, 10);
31 for (; expt; expt >>= 1) {
32 if (expt & 1) {
33 _ecl_big_mul(accum, accum, factor);
34 }
35 _ecl_big_mul(factor, factor, factor);
36 }
37 _ecl_big_register_free(factor);
38 return _ecl_big_register_normalize(accum);
39 }
40
41 static cl_object
42 infinity(cl_index exp_char, int sign)
43 {
44 cl_object var;
45 switch (exp_char) {
46 #ifdef ECL_IEEE_FP
47 case 'e': case 'E':
48 return infinity(ecl_current_read_default_float_format(), sign);
49 case 's': case 'S':
50 case 'f': case 'F':
51 var = (sign<0)?
52 @'ext::single-float-negative-infinity' :
53 @'ext::single-float-positive-infinity';
54 break;
55 case 'd': case 'D':
56 var = (sign<0)?
57 @'ext::double-float-negative-infinity' :
58 @'ext::double-float-positive-infinity';
59 break;
60 case 'l': case 'L':
61 var = (sign<0)?
62 @'ext::long-float-negative-infinity' :
63 @'ext::long-float-positive-infinity';
64 break;
65 #endif /* ECL_IEEE_FP */
66 default:
67 return OBJNULL;
68 }
69 return ecl_symbol_value(var);
70 }
71
72 static cl_object
73 make_float(cl_object num, cl_object exp, cl_index exp_char, int sign)
74 {
75 if (!ECL_FIXNUMP(exp)) {
76 return infinity(exp_char, sign);
77 } else {
78 cl_fixnum fix_exp = ecl_fixnum(exp);
79 if (fix_exp > 0) {
80 num = ecl_times(num, expt10(fix_exp));
81 } else if (fix_exp < 0) {
82 num = ecl_divide(num, expt10(-fix_exp));
83 }
84 }
85 AGAIN:
86 switch (exp_char) {
87 case 'e': case 'E':
88 exp_char = ecl_current_read_default_float_format();
89 goto AGAIN;
90 case 's': case 'S':
91 case 'f': case 'F':
92 return ecl_make_single_float(sign * ecl_to_double(num));
93 case 'd': case 'D': {
94 return ecl_make_double_float(sign * ecl_to_double(num));
95 case 'l': case 'L':
96 return ecl_make_long_float(sign * ecl_to_long_double(num));
97 }
98 default:
99 return OBJNULL;
100 }
101 }
102
103 /*
104 ecl_parse_number(str, start, end, ep, radix) parses C string str
105 up to (but not including) str[end]
106 using radix as the radix for the rational number.
107 (For floating numbers, the radix is ignored and replaced with 10)
108 When parsing succeeds,
109 the index of the next character is assigned to *ep,
110 and the number is returned as a lisp data object.
111 If not, OBJNULL is returned.
112 */
113 cl_object
114 ecl_parse_number(cl_object str, cl_index start, cl_index end,
115 cl_index *ep, unsigned int radix)
116 {
117 int sign = -1, d;
118 cl_index c, i, decimal = end;
119 cl_object num = _ecl_big_register0();
120 bool some_digit = 0;
121 if (end <= start || radix > 36) {
122 *ep = start;
123 return OBJNULL;
124 }
125 AGAIN:
126 _ecl_big_set_ui(num, 0);
127 c = ecl_char(str, i = start);
128 sign = 1;
129 if (c == '+') {
130 if (++i == end) goto NOT_A_NUMBER;
131 c = ecl_char(str, i);
132 } else if (c == '-') {
133 sign = -1;
134 if (++i == end) goto NOT_A_NUMBER;
135 c = ecl_char(str, i);
136 }
137 if (c == '/') {
138 goto NOT_A_NUMBER;
139 }
140 for (; i < end; i++) {
141 c = ecl_char(str, i);
142 if (c == '/') {
143 cl_object den;
144 if (sign < 0) _ecl_big_complement(num, num);
145 num = _ecl_big_register_normalize(num);
146 c = ecl_char(str, ++i);
147 if (ecl_digitp(c, radix) < 0)
148 goto NOT_A_NUMBER;
149 den = ecl_parse_integer(str, i, end, ep, radix);
150 if (den == OBJNULL || (*ep < end)) {
151 return OBJNULL;
152 } else if (den == ecl_make_fixnum(0)) {
153 return ECL_NIL;
154 } else {
155 return ecl_make_ratio(num, den);
156 }
157 } else if (c == '.') {
158 if (decimal <= i) {
159 goto NOT_A_NUMBER;
160 }
161 if (radix != 10) {
162 radix = 10;
163 goto AGAIN;
164 }
165 /* For a number xxxx.1234...nEyyy
166 * we have stored in num the number xxxx1234...n and
167 * will get in the exponent yyy. What we do is to simply
168 * shift the exponent by -n. */
169 decimal = i+1;
170 } else if ((d = ecl_digitp(c, radix)) >= 0) {
171 _ecl_big_mul_ui(num, num, radix);
172 _ecl_big_add_ui(num, num, d);
173 some_digit = 1;
174 } else if (exponent_charp(c)) {
175 cl_object exp, decimals;
176 if (!some_digit)
177 goto NOT_A_NUMBER;
178 if (radix != 10) {
179 radix = 10;
180 goto AGAIN;
181 }
182 num = _ecl_big_register_normalize(num);
183 decimals = (decimal < i) ?
184 ecl_make_fixnum(decimal - i):
185 ecl_make_fixnum(0);
186 exp = ecl_parse_integer(str, ++i, end, ep, 10);
187 if (exp == OBJNULL || (*ep < end))
188 return OBJNULL;
189 return make_float(num, ecl_plus(decimals, exp),
190 c, sign);
191 } else if (radix != 10) {
192 num = ecl_parse_number(str, start, end, ep, 10);
193 if (num != OBJNULL) {
194 if (floatp(num))
195 return num;
196 if (ECL_FIXNUMP(num) || ECL_BIGNUMP(num)) {
197 i = *ep;
198 if (i > start && ecl_char(str, i-1) == '.')
199 return num;
200 }
201 }
202 return OBJNULL;
203 } else {
204 NOT_A_NUMBER:
205 *ep = i;
206 return OBJNULL;
207 }
208 }
209 if (!some_digit) goto NOT_A_NUMBER;
210 *ep = i;
211 /* If we have reached the end without decimals (for instance
212 * 1., 2, 13., etc) we return an integer */
213 if (decimal < i) {
214 return make_float(_ecl_big_register_normalize(num),
215 ecl_make_fixnum(decimal - i), 'e', sign);
216 } else {
217 if (sign < 0) _ecl_big_complement(num, num);
218 return _ecl_big_register_normalize(num);
219 }
220 }