blob: ae04aa42688ba2737ab15268184467e839dafed4 [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"
31#include "py/parsenum.h"
32#include "py/smallint.h"
Damien George20773972014-02-22 18:12:43 +000033
Damien Georgefb510b32014-06-01 13:32:54 +010034#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgec06ea7a2014-03-21 10:55:08 +000035#include <math.h>
36#endif
37
Damien George0ec8cf82015-05-30 23:13:16 +010038STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
Damien George7d414a12015-02-08 01:57:40 +000039 // if lex!=NULL then the parser called us and we need to make a SyntaxError with traceback
40 if (lex != NULL) {
41 ((mp_obj_base_t*)exc)->type = &mp_type_SyntaxError;
42 mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
43 }
44 nlr_raise(exc);
45}
46
47mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base, mp_lexer_t *lex) {
Paul Sokolovsky02946612014-06-14 17:56:44 +030048 const byte *restrict str = (const byte *)str_;
49 const byte *restrict top = str + len;
Damien Georgedfbafab2014-03-21 12:15:59 +000050 bool neg = false;
Damien George503d6112014-05-28 14:07:21 +010051 mp_obj_t ret_val;
Damien George20773972014-02-22 18:12:43 +000052
53 // check radix base
54 if ((base != 0 && base < 2) || base > 36) {
Damien George7d414a12015-02-08 01:57:40 +000055 // this won't be reached if lex!=NULL
Andrew Schellerf78cfaf2014-04-09 19:56:38 +010056 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 +000057 }
Damien Georgedfbafab2014-03-21 12:15:59 +000058
59 // skip leading space
60 for (; str < top && unichar_isspace(*str); str++) {
Damien George20773972014-02-22 18:12:43 +000061 }
62
Damien Georgedfbafab2014-03-21 12:15:59 +000063 // parse optional sign
64 if (str < top) {
65 if (*str == '+') {
66 str++;
67 } else if (*str == '-') {
68 str++;
69 neg = true;
Damien George20773972014-02-22 18:12:43 +000070 }
71 }
72
Damien Georgedfbafab2014-03-21 12:15:59 +000073 // parse optional base prefix
Paul Sokolovsky02946612014-06-14 17:56:44 +030074 str += mp_parse_num_base((const char*)str, top - str, &base);
Damien George20773972014-02-22 18:12:43 +000075
Damien Georgedfbafab2014-03-21 12:15:59 +000076 // string should be an integer number
Damien George40f3c022014-07-03 13:25:24 +010077 mp_int_t int_val = 0;
Paul Sokolovsky02946612014-06-14 17:56:44 +030078 const byte *restrict str_val_start = str;
Damien Georgedfbafab2014-03-21 12:15:59 +000079 for (; str < top; str++) {
Damien Georged1e355e2014-05-28 14:51:12 +010080 // get next digit as a value
Damien George38161822014-07-03 14:13:33 +010081 mp_uint_t dig = *str;
Damien Georgedfbafab2014-03-21 12:15:59 +000082 if (unichar_isdigit(dig) && dig - '0' < base) {
83 // 0-9 digit
Damien Georged1e355e2014-05-28 14:51:12 +010084 dig = dig - '0';
Damien Georgedfbafab2014-03-21 12:15:59 +000085 } else if (base == 16) {
86 dig |= 0x20;
87 if ('a' <= dig && dig <= 'f') {
88 // a-f hex digit
Damien Georged1e355e2014-05-28 14:51:12 +010089 dig = dig - 'a' + 10;
Damien Georgedfbafab2014-03-21 12:15:59 +000090 } else {
91 // unknown character
92 break;
93 }
94 } else {
95 // unknown character
96 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:
Daniel Campora228c68a2015-06-23 15:30:49 +0200145 // if lex!=NULL then the parser called us and we need to make a ValueError with traceback
Damien George1e9a92f2014-11-06 17:36:16 +0000146 if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
Daniel Campora228c68a2015-06-23 15:30:49 +0200147 mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
Damien George7d414a12015-02-08 01:57:40 +0000148 "invalid syntax for integer");
Damien George0ec8cf82015-05-30 23:13:16 +0100149 raise_exc(exc, lex);
Damien George1e9a92f2014-11-06 17:36:16 +0000150 } else {
Damien George7d414a12015-02-08 01:57:40 +0000151 mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
Damien Georged4bd37a2015-03-16 10:42:50 +0000152 "invalid syntax for integer with base %d: '%.*s'", base, top - str_val_start, str_val_start);
Damien George0ec8cf82015-05-30 23:13:16 +0100153 raise_exc(exc, lex);
Damien George1e9a92f2014-11-06 17:36:16 +0000154 }
Damien George20773972014-02-22 18:12:43 +0000155}
156
Damien George38161822014-07-03 14:13:33 +0100157typedef enum {
158 PARSE_DEC_IN_INTG,
159 PARSE_DEC_IN_FRAC,
160 PARSE_DEC_IN_EXP,
161} parse_dec_in_t;
Damien George20773972014-02-22 18:12:43 +0000162
Damien George7d414a12015-02-08 01:57:40 +0000163mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) {
Damien Georgefb510b32014-06-01 13:32:54 +0100164#if MICROPY_PY_BUILTINS_FLOAT
Damien George20773972014-02-22 18:12:43 +0000165 const char *top = str + len;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000166 mp_float_t dec_val = 0;
167 bool dec_neg = false;
168 bool imag = false;
169
170 // skip leading space
Damien Georgedfbafab2014-03-21 12:15:59 +0000171 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000172 }
173
Damien Georgedfbafab2014-03-21 12:15:59 +0000174 // parse optional sign
Damien Georgec06ea7a2014-03-21 10:55:08 +0000175 if (str < top) {
176 if (*str == '+') {
Damien George20773972014-02-22 18:12:43 +0000177 str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000178 } else if (*str == '-') {
179 str++;
180 dec_neg = true;
Damien George20773972014-02-22 18:12:43 +0000181 }
182 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000183
Damien George7d414a12015-02-08 01:57:40 +0000184 const char *str_val_start = str;
185
Damien Georgec06ea7a2014-03-21 10:55:08 +0000186 // determine what the string is
187 if (str < top && (str[0] | 0x20) == 'i') {
188 // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
189 if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
190 // inf
191 str += 3;
192 dec_val = INFINITY;
193 if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
194 // infinity
195 str += 5;
196 }
197 }
198 } else if (str < top && (str[0] | 0x20) == 'n') {
199 // string starts with 'n', should be 'nan' (case insensitive)
200 if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
201 // NaN
202 str += 3;
203 dec_val = MICROPY_FLOAT_C_FUN(nan)("");
204 }
205 } else {
Damien George6e48f7f2014-03-21 11:45:46 +0000206 // string should be a decimal number
Damien George38161822014-07-03 14:13:33 +0100207 parse_dec_in_t in = PARSE_DEC_IN_INTG;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000208 bool exp_neg = false;
Damien George7d414a12015-02-08 01:57:40 +0000209 mp_float_t frac_mult = 0.1;
Damien George38161822014-07-03 14:13:33 +0100210 mp_int_t exp_val = 0;
Damien George7d414a12015-02-08 01:57:40 +0000211 while (str < top) {
212 mp_uint_t dig = *str++;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000213 if ('0' <= dig && dig <= '9') {
214 dig -= '0';
215 if (in == PARSE_DEC_IN_EXP) {
216 exp_val = 10 * exp_val + dig;
217 } else {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000218 if (in == PARSE_DEC_IN_FRAC) {
Damien George7d414a12015-02-08 01:57:40 +0000219 dec_val += dig * frac_mult;
220 frac_mult *= 0.1;
221 } else {
222 dec_val = 10 * dec_val + dig;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000223 }
224 }
225 } else if (in == PARSE_DEC_IN_INTG && dig == '.') {
226 in = PARSE_DEC_IN_FRAC;
227 } else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
228 in = PARSE_DEC_IN_EXP;
Damien George7d414a12015-02-08 01:57:40 +0000229 if (str < top) {
230 if (str[0] == '+') {
231 str++;
232 } else if (str[0] == '-') {
233 str++;
234 exp_neg = true;
235 }
236 }
237 if (str == top) {
238 goto value_error;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000239 }
240 } else if (allow_imag && (dig | 0x20) == 'j') {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000241 imag = true;
242 break;
243 } else {
244 // unknown character
Damien George7d414a12015-02-08 01:57:40 +0000245 str--;
Damien Georgec06ea7a2014-03-21 10:55:08 +0000246 break;
247 }
248 }
249
250 // work out the exponent
251 if (exp_neg) {
252 exp_val = -exp_val;
253 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000254
255 // apply the exponent
256 for (; exp_val > 0; exp_val--) {
257 dec_val *= 10;
258 }
259 for (; exp_val < 0; exp_val++) {
260 dec_val *= 0.1;
261 }
262 }
263
264 // negate value if needed
265 if (dec_neg) {
266 dec_val = -dec_val;
267 }
268
Damien George7d414a12015-02-08 01:57:40 +0000269 // check we parsed something
270 if (str == str_val_start) {
271 goto value_error;
272 }
273
Damien Georgec06ea7a2014-03-21 10:55:08 +0000274 // skip trailing space
Damien Georgedfbafab2014-03-21 12:15:59 +0000275 for (; str < top && unichar_isspace(*str); str++) {
Damien Georgec06ea7a2014-03-21 10:55:08 +0000276 }
277
278 // check we reached the end of the string
279 if (str != top) {
Damien George7d414a12015-02-08 01:57:40 +0000280 goto value_error;
Damien George20773972014-02-22 18:12:43 +0000281 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000282
283 // return the object
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300284#if MICROPY_PY_BUILTINS_COMPLEX
Damien George20773972014-02-22 18:12:43 +0000285 if (imag) {
286 return mp_obj_new_complex(0, dec_val);
Damien George6e48f7f2014-03-21 11:45:46 +0000287 } else if (force_complex) {
288 return mp_obj_new_complex(dec_val, 0);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300289#else
290 if (imag || force_complex) {
Damien George0ec8cf82015-05-30 23:13:16 +0100291 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "complex values not supported"), lex);
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300292#endif
Damien George20773972014-02-22 18:12:43 +0000293 } else {
294 return mp_obj_new_float(dec_val);
295 }
Damien Georgec06ea7a2014-03-21 10:55:08 +0000296
Damien George7d414a12015-02-08 01:57:40 +0000297value_error:
Damien George0ec8cf82015-05-30 23:13:16 +0100298 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid syntax for number"), lex);
Damien George7d414a12015-02-08 01:57:40 +0000299
Damien George20773972014-02-22 18:12:43 +0000300#else
Damien George0ec8cf82015-05-30 23:13:16 +0100301 raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "decimal numbers not supported"), lex);
Damien George20773972014-02-22 18:12:43 +0000302#endif
303}