blob: 98e7736851d123926ce2f55712b3c638a86bac0a [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 George7d0d7212016-10-17 12:17:37 +110030#include "py/runtime.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
Damien George94c41bb2017-03-28 22:37:26 +110058 mp_raise_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 Georgec2dd4942016-12-28 12:02:49 +110084 if ('0' <= dig && dig <= '9') {
85 dig -= '0';
86 } else {
87 dig |= 0x20; // make digit lower-case
88 if ('a' <= dig && dig <= 'z') {
89 dig -= 'a' - 10;
Damien Georgedfbafab2014-03-21 12:15:59 +000090 } else {
91 // unknown character
92 break;
93 }
Damien Georgec2dd4942016-12-28 12:02:49 +110094 }
Damien Georgeca7af9a2016-12-28 12:25:00 +110095 if (dig >= (mp_uint_t)base) {
Damien Georgedfbafab2014-03-21 12:15:59 +000096 break;
97 }
Damien Georged1e355e2014-05-28 14:51:12 +010098
99 // add next digi and check for overflow
100 if (mp_small_int_mul_overflow(int_val, base)) {
Damien Georgedfbafab2014-03-21 12:15:59 +0000101 goto overflow;
Damien Georged1e355e2014-05-28 14:51:12 +0100102 }
103 int_val = int_val * base + dig;
104 if (!MP_SMALL_INT_FITS(int_val)) {
Damien Georgedfbafab2014-03-21 12:15:59 +0000105 goto overflow;
106 }
107 }
108
109 // negate value if needed
110 if (neg) {
111 int_val = -int_val;
112 }
113
Damien George503d6112014-05-28 14:07:21 +0100114 // create the small int
115 ret_val = MP_OBJ_NEW_SMALL_INT(int_val);
116
117have_ret_val:
118 // check we parsed something
119 if (str == str_val_start) {
120 goto value_error;
121 }
122
Damien Georgedfbafab2014-03-21 12:15:59 +0000123 // skip trailing space
124 for (; str < top && unichar_isspace(*str); str++) {
125 }
126
127 // check we reached the end of the string
128 if (str != top) {
Damien George7b4b78b2014-03-21 20:46:38 +0000129 goto value_error;
Damien Georgedfbafab2014-03-21 12:15:59 +0000130 }
131
132 // return the object
Damien George503d6112014-05-28 14:07:21 +0100133 return ret_val;
Damien George7b4b78b2014-03-21 20:46:38 +0000134
Damien Georgedfbafab2014-03-21 12:15:59 +0000135overflow:
Damien George503d6112014-05-28 14:07:21 +0100136 // reparse using long int
137 {
Paul Sokolovsky02946612014-06-14 17:56:44 +0300138 const char *s2 = (const char*)str_val_start;
Damien George503d6112014-05-28 14:07:21 +0100139 ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
Paul Sokolovsky02946612014-06-14 17:56:44 +0300140 str = (const byte*)s2;
Damien George503d6112014-05-28 14:07:21 +0100141 goto have_ret_val;
142 }
143
144value_error:
Damien George1e9a92f2014-11-06 17:36:16 +0000145 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Daniel Campora228c68a2015-06-23 15:30:49 +0200146 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
Damien George7d414a12015-02-08 01:57:40 +0000147 "invalid syntax for integer");
Damien George0ec8cf82015-05-30 23:13:16 +0100148 raise_exc(exc, lex);
Damien George5f3c3ec2015-10-01 17:06:13 +0100149 } else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
Damien George7d414a12015-02-08 01:57:40 +0000150 mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien George5f3c3ec2015-10-01 17:06:13 +0100151 "invalid syntax for integer with base %d", base);
152 raise_exc(exc, lex);
153 } else {
154 vstr_t vstr;
155 mp_print_t print;
156 vstr_init_print(&vstr, 50, &print);
157 mp_printf(&print, "invalid syntax for integer with base %d: ", base);
158 mp_str_print_quoted(&print, str_val_start, top - str_val_start, true);
159 mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
160 mp_obj_new_str_from_vstr(&mp_type_str, &vstr));
Damien George0ec8cf82015-05-30 23:13:16 +0100161 raise_exc(exc, lex);
Damien George1e9a92f2014-11-06 17:36:16 +0000162 }
Damien George20773972014-02-22 18:12:43 +0000163}
164
Damien George38161822014-07-03 14:13:33 +0100165typedef enum {
166 PARSE_DEC_IN_INTG,
167 PARSE_DEC_IN_FRAC,
168 PARSE_DEC_IN_EXP,
169} parse_dec_in_t;
Damien George20773972014-02-22 18:12:43 +0000170
Damien Georgedddb98d2016-03-14 22:34:03 +0000171mp_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 +0100172#if MICROPY_PY_BUILTINS_FLOAT
Damien George84895f12017-11-27 12:51:52 +1100173
174// DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing
175#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
176#define DEC_VAL_MAX 1e20F
177#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
178#define DEC_VAL_MAX 1e200
179#endif
180
Damien George20773972014-02-22 18:12:43 +0000181 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000182 mp_float_t dec_val = 0;
183 bool dec_neg = false;
184 bool imag = false;
185
186 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000187 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000188 }
189
Damien Georgedfbafab2014-03-21 12:15:59 +0000190 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000191 if (str < top) {
192 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000193 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000194 } else if (*str == '-') {
195 str++;
196 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000197 }
198 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000199
Damien George7d414a12015-02-08 01:57:40 +0000200 const char *str_val_start = str;
201
Damien Georgec06ea7a2014-03-21 10:55:08 +0000202 // determine what the string is
203 if (str < top && (str[0] | 0x20) == 'i') {
204 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
205 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
206 // inf
207 str += 3;
208 dec_val = INFINITY;
209 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
210 // infinity
211 str += 5;
212 }
213 }
214 } else if (str < top && (str[0] | 0x20) == 'n') {
215 // string starts with 'n', should be 'nan' (case insensitive)
216 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
217 // NaN
218 str += 3;
219 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
220 }
221 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000222 // string should be a decimal number
Damien George38161822014-07-03 14:13:33 +0100223 parse_dec_in_t in = PARSE_DEC_IN_INTG;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000224 bool exp_neg = false;
Damien George38161822014-07-03 14:13:33 +0100225 mp_int_t exp_val = 0;
Damien George84895f12017-11-27 12:51:52 +1100226 mp_int_t exp_extra = 0;
Damien George7d414a12015-02-08 01:57:40 +0000227 while (str < top) {
228 mp_uint_t dig = *str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000229 if ('0' <= dig && dig <= '9') {
230 dig -= '0';
231 if (in == PARSE_DEC_IN_EXP) {
232 exp_val = 10 * exp_val + dig;
233 } else {
Damien George84895f12017-11-27 12:51:52 +1100234 if (dec_val < DEC_VAL_MAX) {
235 // dec_val won't overflow so keep accumulating
Damien George7d414a12015-02-08 01:57:40 +0000236 dec_val = 10 * dec_val + dig;
Damien George84895f12017-11-27 12:51:52 +1100237 if (in == PARSE_DEC_IN_FRAC) {
238 --exp_extra;
239 }
240 } else {
241 // dec_val might overflow and we anyway can't represent more digits
242 // of precision, so ignore the digit and just adjust the exponent
243 if (in == PARSE_DEC_IN_INTG) {
244 ++exp_extra;
245 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000246 }
247 }
248 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
249 in = PARSE_DEC_IN_FRAC;
250 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
251 in = PARSE_DEC_IN_EXP;
Damien George7d414a12015-02-08 01:57:40 +0000252 if (str < top) {
253 if (str[0] == '+') {
254 str++;
255 } else if (str[0] == '-') {
256 str++;
257 exp_neg = true;
258 }
259 }
260 if (str == top) {
261 goto value_error;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000262 }
263 } else if (allow_imag && (dig | 0x20) == 'j') {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000264 imag = true;
265 break;
266 } else {
267 // unknown character
Damien George7d414a12015-02-08 01:57:40 +0000268 str--;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000269 break;
270 }
271 }
272
273 // work out the exponent
274 if (exp_neg) {
275 exp_val = -exp_val;
276 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000277
278 // apply the exponent
Damien George84895f12017-11-27 12:51:52 +1100279 dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val + exp_extra);
Damien Georgec06ea7a2014-03-21 10:55:08 +0000280 }
281
282 // negate value if needed
283 if (dec_neg) {
284 dec_val = -dec_val;
285 }
286
Damien George7d414a12015-02-08 01:57:40 +0000287 // check we parsed something
288 if (str == str_val_start) {
289 goto value_error;
290 }
291
Damien Georgec06ea7a2014-03-21 10:55:08 +0000292 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000293 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000294 }
295
296 // check we reached the end of the string
297 if (str != top) {
Damien George7d414a12015-02-08 01:57:40 +0000298 goto value_error;
Damien George20773972014-02-22 18:12:43 +0000299 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000300
301 // return the object
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300302#if MICROPY_PY_BUILTINS_COMPLEX
Damien George20773972014-02-22 18:12:43 +0000303 if (imag) {
304 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000305 } else if (force_complex) {
306 return mp_obj_new_complex(dec_val, 0);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300307#else
308 if (imag || force_complex) {
Damien George0ec8cf82015-05-30 23:13:16 +0100309 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "complex values not supported"), lex);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300310#endif
Damien George20773972014-02-22 18:12:43 +0000311 } else {
312 return mp_obj_new_float(dec_val);
313 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000314
Damien George7d414a12015-02-08 01:57:40 +0000315value_error:
Damien George0ec8cf82015-05-30 23:13:16 +0100316 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid syntax for number"), lex);
Damien George7d414a12015-02-08 01:57:40 +0000317
Damien George20773972014-02-22 18:12:43 +0000318#else
Damien George0ec8cf82015-05-30 23:13:16 +0100319 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "decimal numbers not supported"), lex);
Damien George20773972014-02-22 18:12:43 +0000320#endif
321}