blob: c73ae54a16752509fdd56ac4168638b382b90a11 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
xbeefe34222014-03-16 00:14:26 -070027#include <stdbool.h>
Damien George20773972014-02-22 18:12:43 +000028#include <stdlib.h>
29
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
Damien Georgedddb98d2016-03-14 22:34:03 +000031#include "py/parsenumbase.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/parsenum.h"
33#include "py/smallint.h"
Damien George20773972014-02-22 18:12:43 +000034
Damien Georgefb510b32014-06-01 13:32:54 +010035#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgec06ea7a2014-03-21 10:55:08 +000036#include <math.h>
37#endif
38
Damien George0ec8cf82015-05-30 23:13:16 +010039STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
Damien George2a1090a2015-06-23 16:08:51 +000040 // if lex!=NULL then the parser called us and we need to convert the
41 // exception's type from ValueError to SyntaxError and add traceback info
Damien George7d414a12015-02-08 01:57:40 +000042 if (lex != NULL) {
Damien George999cedb2015-11-27 17:01:44 +000043 ((mp_obj_base_t*)MP_OBJ_TO_PTR(exc))->type = &mp_type_SyntaxError;
Damien George7d414a12015-02-08 01:57:40 +000044 mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
45 }
46 nlr_raise(exc);
47}
48
Damien Georgedddb98d2016-03-14 22:34:03 +000049mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex) {
Paul Sokolovsky02946612014-06-14 17:56:44 +030050 const byte *restrict str = (const byte *)str_;
51 const byte *restrict top = str + len;
Damien Georgedfbafab2014-03-21 12:15:59 +000052 bool neg = false;
Damien George503d6112014-05-28 14:07:21 +010053 mp_obj_t ret_val;
Damien George20773972014-02-22 18:12:43 +000054
55 // check radix base
56 if ((base != 0 && base < 2) || base > 36) {
Damien George7d414a12015-02-08 01:57:40 +000057 // this won't be reached if lex!=NULL
Andrew Schellerf78cfaf2014-04-09 19:56:38 +010058 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "int() arg 2 must be >= 2 and <= 36"));
Damien George20773972014-02-22 18:12:43 +000059 }
Damien Georgedfbafab2014-03-21 12:15:59 +000060
61 // skip leading space
62 for (; str < top && unichar_isspace(*str); str++) {
Damien George20773972014-02-22 18:12:43 +000063 }
64
Damien Georgedfbafab2014-03-21 12:15:59 +000065 // parse optional sign
66 if (str < top) {
67 if (*str == '+') {
68 str++;
69 } else if (*str == '-') {
70 str++;
71 neg = true;
Damien George20773972014-02-22 18:12:43 +000072 }
73 }
74
Damien Georgedfbafab2014-03-21 12:15:59 +000075 // parse optional base prefix
Paul Sokolovsky02946612014-06-14 17:56:44 +030076 str += mp_parse_num_base((const char*)str, top - str, &base);
Damien George20773972014-02-22 18:12:43 +000077
Damien Georgedfbafab2014-03-21 12:15:59 +000078 // string should be an integer number
Damien George40f3c022014-07-03 13:25:24 +010079 mp_int_t int_val = 0;
Paul Sokolovsky02946612014-06-14 17:56:44 +030080 const byte *restrict str_val_start = str;
Damien Georgedfbafab2014-03-21 12:15:59 +000081 for (; str < top; str++) {
Damien Georged1e355e2014-05-28 14:51:12 +010082 // get next digit as a value
Damien George38161822014-07-03 14:13:33 +010083 mp_uint_t dig = *str;
Damien Georgedfbafab2014-03-21 12:15:59 +000084 if (unichar_isdigit(dig) && dig - '0' < base) {
85 // 0-9 digit
Damien Georged1e355e2014-05-28 14:51:12 +010086 dig = dig - '0';
Damien Georgedfbafab2014-03-21 12:15:59 +000087 } else if (base == 16) {
88 dig |= 0x20;
89 if ('a' <= dig && dig <= 'f') {
90 // a-f hex digit
Damien Georged1e355e2014-05-28 14:51:12 +010091 dig = dig - 'a' + 10;
Damien Georgedfbafab2014-03-21 12:15:59 +000092 } else {
93 // unknown character
94 break;
95 }
96 } else {
97 // unknown character
98 break;
99 }
Damien Georged1e355e2014-05-28 14:51:12 +0100100
101 // add next digi and check for overflow
102 if (mp_small_int_mul_overflow(int_val, base)) {
Damien Georgedfbafab2014-03-21 12:15:59 +0000103 goto overflow;
Damien Georged1e355e2014-05-28 14:51:12 +0100104 }
105 int_val = int_val * base + dig;
106 if (!MP_SMALL_INT_FITS(int_val)) {
Damien Georgedfbafab2014-03-21 12:15:59 +0000107 goto overflow;
108 }
109 }
110
111 // negate value if needed
112 if (neg) {
113 int_val = -int_val;
114 }
115
Damien George503d6112014-05-28 14:07:21 +0100116 // create the small int
117 ret_val = MP_OBJ_NEW_SMALL_INT(int_val);
118
119have_ret_val:
120 // check we parsed something
121 if (str == str_val_start) {
122 goto value_error;
123 }
124
Damien Georgedfbafab2014-03-21 12:15:59 +0000125 // skip trailing space
126 for (; str < top && unichar_isspace(*str); str++) {
127 }
128
129 // check we reached the end of the string
130 if (str != top) {
Damien George7b4b78b2014-03-21 20:46:38 +0000131 goto value_error;
Damien Georgedfbafab2014-03-21 12:15:59 +0000132 }
133
134 // return the object
Damien George503d6112014-05-28 14:07:21 +0100135 return ret_val;
Damien George7b4b78b2014-03-21 20:46:38 +0000136
Damien Georgedfbafab2014-03-21 12:15:59 +0000137overflow:
Damien George503d6112014-05-28 14:07:21 +0100138 // reparse using long int
139 {
Paul Sokolovsky02946612014-06-14 17:56:44 +0300140 const char *s2 = (const char*)str_val_start;
Damien George503d6112014-05-28 14:07:21 +0100141 ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
Paul Sokolovsky02946612014-06-14 17:56:44 +0300142 str = (const byte*)s2;
Damien George503d6112014-05-28 14:07:21 +0100143 goto have_ret_val;
144 }
145
146value_error:
Damien George1e9a92f2014-11-06 17:36:16 +0000147 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Daniel Campora228c68a2015-06-23 15:30:49 +0200148 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
Damien George7d414a12015-02-08 01:57:40 +0000149 "invalid syntax for integer");
Damien George0ec8cf82015-05-30 23:13:16 +0100150 raise_exc(exc, lex);
Damien George5f3c3ec2015-10-01 17:06:13 +0100151 } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
Damien George7d414a12015-02-08 01:57:40 +0000152 mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George5f3c3ec2015-10-01 17:06:13 +0100153 "invalid syntax for integer with base %d", base);
154 raise_exc(exc, lex);
155 } else {
156 vstr_t vstr;
157 mp_print_t print;
158 vstr_init_print(&vstr, 50, &print);
159 mp_printf(&print, "invalid syntax for integer with base %d: ", base);
160 mp_str_print_quoted(&print, str_val_start, top - str_val_start, true);
161 mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
162 mp_obj_new_str_from_vstr(&mp_type_str, &vstr));
Damien George0ec8cf82015-05-30 23:13:16 +0100163 raise_exc(exc, lex);
Damien George1e9a92f2014-11-06 17:36:16 +0000164 }
Damien George20773972014-02-22 18:12:43 +0000165}
166
Damien George38161822014-07-03 14:13:33 +0100167typedef enum {
168 PARSE_DEC_IN_INTG,
169 PARSE_DEC_IN_FRAC,
170 PARSE_DEC_IN_EXP,
171} parse_dec_in_t;
Damien George20773972014-02-22 18:12:43 +0000172
Damien Georgedddb98d2016-03-14 22:34:03 +0000173mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) {
Damien Georgefb510b32014-06-01 13:32:54 +0100174#if MICROPY_PY_BUILTINS_FLOAT
Damien George20773972014-02-22 18:12:43 +0000175 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000176 mp_float_t dec_val = 0;
177 bool dec_neg = false;
178 bool imag = false;
179
180 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000181 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000182 }
183
Damien Georgedfbafab2014-03-21 12:15:59 +0000184 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000185 if (str < top) {
186 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000187 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000188 } else if (*str == '-') {
189 str++;
190 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000191 }
192 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000193
Damien George7d414a12015-02-08 01:57:40 +0000194 const char *str_val_start = str;
195
Damien Georgec06ea7a2014-03-21 10:55:08 +0000196 // determine what the string is
197 if (str < top && (str[0] | 0x20) == 'i') {
198 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
199 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
200 // inf
201 str += 3;
202 dec_val = INFINITY;
203 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
204 // infinity
205 str += 5;
206 }
207 }
208 } else if (str < top && (str[0] | 0x20) == 'n') {
209 // string starts with 'n', should be 'nan' (case insensitive)
210 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
211 // NaN
212 str += 3;
213 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
214 }
215 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000216 // string should be a decimal number
Damien George38161822014-07-03 14:13:33 +0100217 parse_dec_in_t in = PARSE_DEC_IN_INTG;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000218 bool exp_neg = false;
Damien George7d414a12015-02-08 01:57:40 +0000219 mp_float_t frac_mult = 0.1;
Damien George38161822014-07-03 14:13:33 +0100220 mp_int_t exp_val = 0;
Damien George7d414a12015-02-08 01:57:40 +0000221 while (str < top) {
222 mp_uint_t dig = *str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000223 if ('0' <= dig && dig <= '9') {
224 dig -= '0';
225 if (in == PARSE_DEC_IN_EXP) {
226 exp_val = 10 * exp_val + dig;
227 } else {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000228 if (in == PARSE_DEC_IN_FRAC) {
Damien George7d414a12015-02-08 01:57:40 +0000229 dec_val += dig * frac_mult;
230 frac_mult *= 0.1;
231 } else {
232 dec_val = 10 * dec_val + dig;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000233 }
234 }
235 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
236 in = PARSE_DEC_IN_FRAC;
237 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
238 in = PARSE_DEC_IN_EXP;
Damien George7d414a12015-02-08 01:57:40 +0000239 if (str < top) {
240 if (str[0] == '+') {
241 str++;
242 } else if (str[0] == '-') {
243 str++;
244 exp_neg = true;
245 }
246 }
247 if (str == top) {
248 goto value_error;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000249 }
250 } else if (allow_imag && (dig | 0x20) == 'j') {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000251 imag = true;
252 break;
253 } else {
254 // unknown character
Damien George7d414a12015-02-08 01:57:40 +0000255 str--;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000256 break;
257 }
258 }
259
260 // work out the exponent
261 if (exp_neg) {
262 exp_val = -exp_val;
263 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000264
265 // apply the exponent
266 for (; exp_val > 0; exp_val--) {
267 dec_val *= 10;
268 }
269 for (; exp_val < 0; exp_val++) {
270 dec_val *= 0.1;
271 }
272 }
273
274 // negate value if needed
275 if (dec_neg) {
276 dec_val = -dec_val;
277 }
278
Damien George7d414a12015-02-08 01:57:40 +0000279 // check we parsed something
280 if (str == str_val_start) {
281 goto value_error;
282 }
283
Damien Georgec06ea7a2014-03-21 10:55:08 +0000284 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000285 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000286 }
287
288 // check we reached the end of the string
289 if (str != top) {
Damien George7d414a12015-02-08 01:57:40 +0000290 goto value_error;
Damien George20773972014-02-22 18:12:43 +0000291 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000292
293 // return the object
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300294#if MICROPY_PY_BUILTINS_COMPLEX
Damien George20773972014-02-22 18:12:43 +0000295 if (imag) {
296 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000297 } else if (force_complex) {
298 return mp_obj_new_complex(dec_val, 0);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300299#else
300 if (imag || force_complex) {
Damien George0ec8cf82015-05-30 23:13:16 +0100301 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "complex values not supported"), lex);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300302#endif
Damien George20773972014-02-22 18:12:43 +0000303 } else {
304 return mp_obj_new_float(dec_val);
305 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000306
Damien George7d414a12015-02-08 01:57:40 +0000307value_error:
Damien George0ec8cf82015-05-30 23:13:16 +0100308 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid syntax for number"), lex);
Damien George7d414a12015-02-08 01:57:40 +0000309
Damien George20773972014-02-22 18:12:43 +0000310#else
Damien George0ec8cf82015-05-30 23:13:16 +0100311 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "decimal numbers not supported"), lex);
Damien George20773972014-02-22 18:12:43 +0000312#endif
313}