blob: 17711884340017a7ca73397eaf66f635a8c27b1d [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 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 George20773972014-02-22 18:12:43 +0000173 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000174 mp_float_t dec_val = 0;
175 bool dec_neg = false;
176 bool imag = false;
177
178 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000179 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000180 }
181
Damien Georgedfbafab2014-03-21 12:15:59 +0000182 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000183 if (str < top) {
184 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000185 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000186 } else if (*str == '-') {
187 str++;
188 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000189 }
190 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000191
Damien George7d414a12015-02-08 01:57:40 +0000192 const char *str_val_start = str;
193
Damien Georgec06ea7a2014-03-21 10:55:08 +0000194 // determine what the string is
195 if (str < top && (str[0] | 0x20) == 'i') {
196 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
197 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
198 // inf
199 str += 3;
200 dec_val = INFINITY;
201 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
202 // infinity
203 str += 5;
204 }
205 }
206 } else if (str < top && (str[0] | 0x20) == 'n') {
207 // string starts with 'n', should be 'nan' (case insensitive)
208 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
209 // NaN
210 str += 3;
211 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
212 }
213 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000214 // string should be a decimal number
Damien George38161822014-07-03 14:13:33 +0100215 parse_dec_in_t in = PARSE_DEC_IN_INTG;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000216 bool exp_neg = false;
Damien George7d414a12015-02-08 01:57:40 +0000217 mp_float_t frac_mult = 0.1;
Damien George38161822014-07-03 14:13:33 +0100218 mp_int_t exp_val = 0;
Damien George7d414a12015-02-08 01:57:40 +0000219 while (str < top) {
220 mp_uint_t dig = *str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000221 if ('0' <= dig && dig <= '9') {
222 dig -= '0';
223 if (in == PARSE_DEC_IN_EXP) {
224 exp_val = 10 * exp_val + dig;
225 } else {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000226 if (in == PARSE_DEC_IN_FRAC) {
Damien George7d414a12015-02-08 01:57:40 +0000227 dec_val += dig * frac_mult;
Damien George561844f2016-11-03 12:33:01 +1100228 frac_mult *= MICROPY_FLOAT_CONST(0.1);
Damien George7d414a12015-02-08 01:57:40 +0000229 } else {
230 dec_val = 10 * dec_val + dig;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000231 }
232 }
233 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
234 in = PARSE_DEC_IN_FRAC;
235 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
236 in = PARSE_DEC_IN_EXP;
Damien George7d414a12015-02-08 01:57:40 +0000237 if (str < top) {
238 if (str[0] == '+') {
239 str++;
240 } else if (str[0] == '-') {
241 str++;
242 exp_neg = true;
243 }
244 }
245 if (str == top) {
246 goto value_error;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000247 }
248 } else if (allow_imag && (dig | 0x20) == 'j') {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000249 imag = true;
250 break;
251 } else {
252 // unknown character
Damien George7d414a12015-02-08 01:57:40 +0000253 str--;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000254 break;
255 }
256 }
257
258 // work out the exponent
259 if (exp_neg) {
260 exp_val = -exp_val;
261 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000262
263 // apply the exponent
Damien George25996722016-03-29 22:12:07 +0100264 dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
Damien Georgec06ea7a2014-03-21 10:55:08 +0000265 }
266
267 // negate value if needed
268 if (dec_neg) {
269 dec_val = -dec_val;
270 }
271
Damien George7d414a12015-02-08 01:57:40 +0000272 // check we parsed something
273 if (str == str_val_start) {
274 goto value_error;
275 }
276
Damien Georgec06ea7a2014-03-21 10:55:08 +0000277 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000278 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000279 }
280
281 // check we reached the end of the string
282 if (str != top) {
Damien George7d414a12015-02-08 01:57:40 +0000283 goto value_error;
Damien George20773972014-02-22 18:12:43 +0000284 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000285
286 // return the object
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300287#if MICROPY_PY_BUILTINS_COMPLEX
Damien George20773972014-02-22 18:12:43 +0000288 if (imag) {
289 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000290 } else if (force_complex) {
291 return mp_obj_new_complex(dec_val, 0);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300292#else
293 if (imag || force_complex) {
Damien George0ec8cf82015-05-30 23:13:16 +0100294 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "complex values not supported"), lex);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300295#endif
Damien George20773972014-02-22 18:12:43 +0000296 } else {
297 return mp_obj_new_float(dec_val);
298 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000299
Damien George7d414a12015-02-08 01:57:40 +0000300value_error:
Damien George0ec8cf82015-05-30 23:13:16 +0100301 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid syntax for number"), lex);
Damien George7d414a12015-02-08 01:57:40 +0000302
Damien George20773972014-02-22 18:12:43 +0000303#else
Damien George0ec8cf82015-05-30 23:13:16 +0100304 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "decimal numbers not supported"), lex);
Damien George20773972014-02-22 18:12:43 +0000305#endif
306}