2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-16 07:14:26 +00:00
|
|
|
#include <stdbool.h>
|
2014-02-22 18:12:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/parsenum.h"
|
|
|
|
#include "py/smallint.h"
|
2014-02-22 18:12:43 +00:00
|
|
|
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-21 10:55:08 +00:00
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2015-05-30 23:13:16 +01:00
|
|
|
STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
|
2015-06-23 17:08:51 +01:00
|
|
|
// if lex!=NULL then the parser called us and we need to convert the
|
|
|
|
// exception's type from ValueError to SyntaxError and add traceback info
|
2015-02-08 01:57:40 +00:00
|
|
|
if (lex != NULL) {
|
2015-11-27 17:01:44 +00:00
|
|
|
((mp_obj_base_t*)MP_OBJ_TO_PTR(exc))->type = &mp_type_SyntaxError;
|
2015-02-08 01:57:40 +00:00
|
|
|
mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTR_NULL);
|
|
|
|
}
|
|
|
|
nlr_raise(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base, mp_lexer_t *lex) {
|
2014-06-14 15:56:44 +01:00
|
|
|
const byte *restrict str = (const byte *)str_;
|
|
|
|
const byte *restrict top = str + len;
|
2014-03-21 12:15:59 +00:00
|
|
|
bool neg = false;
|
2014-05-28 14:07:21 +01:00
|
|
|
mp_obj_t ret_val;
|
2014-02-22 18:12:43 +00:00
|
|
|
|
|
|
|
// check radix base
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
2015-02-08 01:57:40 +00:00
|
|
|
// this won't be reached if lex!=NULL
|
2014-04-09 19:56:38 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "int() arg 2 must be >= 2 and <= 36"));
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
2014-03-21 12:15:59 +00:00
|
|
|
|
|
|
|
// skip leading space
|
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// parse optional sign
|
|
|
|
if (str < top) {
|
|
|
|
if (*str == '+') {
|
|
|
|
str++;
|
|
|
|
} else if (*str == '-') {
|
|
|
|
str++;
|
|
|
|
neg = true;
|
|
|
|
}
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// parse optional base prefix
|
2014-06-14 15:56:44 +01:00
|
|
|
str += mp_parse_num_base((const char*)str, top - str, &base);
|
2014-03-21 12:15:59 +00:00
|
|
|
|
|
|
|
// string should be an integer number
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_int_t int_val = 0;
|
2014-06-14 15:56:44 +01:00
|
|
|
const byte *restrict str_val_start = str;
|
2014-03-21 12:15:59 +00:00
|
|
|
for (; str < top; str++) {
|
2014-05-28 14:51:12 +01:00
|
|
|
// get next digit as a value
|
2014-07-03 14:13:33 +01:00
|
|
|
mp_uint_t dig = *str;
|
2014-03-21 12:15:59 +00:00
|
|
|
if (unichar_isdigit(dig) && dig - '0' < base) {
|
|
|
|
// 0-9 digit
|
2014-05-28 14:51:12 +01:00
|
|
|
dig = dig - '0';
|
2014-03-21 12:15:59 +00:00
|
|
|
} else if (base == 16) {
|
|
|
|
dig |= 0x20;
|
|
|
|
if ('a' <= dig && dig <= 'f') {
|
|
|
|
// a-f hex digit
|
2014-05-28 14:51:12 +01:00
|
|
|
dig = dig - 'a' + 10;
|
2014-03-21 12:15:59 +00:00
|
|
|
} else {
|
|
|
|
// unknown character
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// unknown character
|
|
|
|
break;
|
|
|
|
}
|
2014-05-28 14:51:12 +01:00
|
|
|
|
|
|
|
// add next digi and check for overflow
|
|
|
|
if (mp_small_int_mul_overflow(int_val, base)) {
|
2014-03-21 12:15:59 +00:00
|
|
|
goto overflow;
|
2014-05-28 14:51:12 +01:00
|
|
|
}
|
|
|
|
int_val = int_val * base + dig;
|
|
|
|
if (!MP_SMALL_INT_FITS(int_val)) {
|
2014-03-21 12:15:59 +00:00
|
|
|
goto overflow;
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// negate value if needed
|
|
|
|
if (neg) {
|
|
|
|
int_val = -int_val;
|
|
|
|
}
|
2014-02-22 18:12:43 +00:00
|
|
|
|
2014-05-28 14:07:21 +01:00
|
|
|
// create the small int
|
|
|
|
ret_val = MP_OBJ_NEW_SMALL_INT(int_val);
|
|
|
|
|
|
|
|
have_ret_val:
|
|
|
|
// check we parsed something
|
|
|
|
if (str == str_val_start) {
|
|
|
|
goto value_error;
|
|
|
|
}
|
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// skip trailing space
|
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
|
|
|
}
|
2014-02-22 18:12:43 +00:00
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// check we reached the end of the string
|
|
|
|
if (str != top) {
|
2014-03-21 20:46:38 +00:00
|
|
|
goto value_error;
|
2014-03-21 12:15:59 +00:00
|
|
|
}
|
2014-02-22 18:12:43 +00:00
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// return the object
|
2014-05-28 14:07:21 +01:00
|
|
|
return ret_val;
|
2014-03-21 20:46:38 +00:00
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
overflow:
|
2014-05-28 14:07:21 +01:00
|
|
|
// reparse using long int
|
|
|
|
{
|
2014-06-14 15:56:44 +01:00
|
|
|
const char *s2 = (const char*)str_val_start;
|
2014-05-28 14:07:21 +01:00
|
|
|
ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
|
2014-06-14 15:56:44 +01:00
|
|
|
str = (const byte*)s2;
|
2014-05-28 14:07:21 +01:00
|
|
|
goto have_ret_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_error:
|
2014-11-06 17:36:16 +00:00
|
|
|
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
2015-06-23 14:30:49 +01:00
|
|
|
mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
|
2015-02-08 01:57:40 +00:00
|
|
|
"invalid syntax for integer");
|
2015-05-30 23:13:16 +01:00
|
|
|
raise_exc(exc, lex);
|
2015-10-01 17:06:13 +01:00
|
|
|
} else if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL) {
|
2015-02-08 01:57:40 +00:00
|
|
|
mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
2015-10-01 17:06:13 +01:00
|
|
|
"invalid syntax for integer with base %d", base);
|
|
|
|
raise_exc(exc, lex);
|
|
|
|
} else {
|
|
|
|
vstr_t vstr;
|
|
|
|
mp_print_t print;
|
|
|
|
vstr_init_print(&vstr, 50, &print);
|
|
|
|
mp_printf(&print, "invalid syntax for integer with base %d: ", base);
|
|
|
|
mp_str_print_quoted(&print, str_val_start, top - str_val_start, true);
|
|
|
|
mp_obj_t exc = mp_obj_new_exception_arg1(&mp_type_ValueError,
|
|
|
|
mp_obj_new_str_from_vstr(&mp_type_str, &vstr));
|
2015-05-30 23:13:16 +01:00
|
|
|
raise_exc(exc, lex);
|
2014-11-06 17:36:16 +00:00
|
|
|
}
|
2014-03-21 12:15:59 +00:00
|
|
|
}
|
2014-02-22 18:12:43 +00:00
|
|
|
|
2014-07-03 14:13:33 +01:00
|
|
|
typedef enum {
|
|
|
|
PARSE_DEC_IN_INTG,
|
|
|
|
PARSE_DEC_IN_FRAC,
|
|
|
|
PARSE_DEC_IN_EXP,
|
|
|
|
} parse_dec_in_t;
|
2014-02-22 18:12:43 +00:00
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
mp_obj_t mp_parse_num_decimal(const char *str, mp_uint_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) {
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-21 10:55:08 +00:00
|
|
|
const char *top = str + len;
|
2014-02-22 18:12:43 +00:00
|
|
|
mp_float_t dec_val = 0;
|
2014-03-21 10:55:08 +00:00
|
|
|
bool dec_neg = false;
|
2014-02-22 18:12:43 +00:00
|
|
|
bool imag = false;
|
2014-03-21 10:55:08 +00:00
|
|
|
|
|
|
|
// skip leading space
|
2014-03-21 12:15:59 +00:00
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
2014-03-21 10:55:08 +00:00
|
|
|
}
|
|
|
|
|
2014-03-21 12:15:59 +00:00
|
|
|
// parse optional sign
|
2014-03-21 10:55:08 +00:00
|
|
|
if (str < top) {
|
|
|
|
if (*str == '+') {
|
|
|
|
str++;
|
|
|
|
} else if (*str == '-') {
|
|
|
|
str++;
|
|
|
|
dec_neg = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
const char *str_val_start = str;
|
|
|
|
|
2014-03-21 10:55:08 +00:00
|
|
|
// determine what the string is
|
|
|
|
if (str < top && (str[0] | 0x20) == 'i') {
|
|
|
|
// string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
|
|
|
|
if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
|
|
|
|
// inf
|
|
|
|
str += 3;
|
|
|
|
dec_val = INFINITY;
|
|
|
|
if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
|
|
|
|
// infinity
|
|
|
|
str += 5;
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
}
|
|
|
|
} else if (str < top && (str[0] | 0x20) == 'n') {
|
|
|
|
// string starts with 'n', should be 'nan' (case insensitive)
|
|
|
|
if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
|
|
|
|
// NaN
|
|
|
|
str += 3;
|
|
|
|
dec_val = MICROPY_FLOAT_C_FUN(nan)("");
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-21 11:45:46 +00:00
|
|
|
// string should be a decimal number
|
2014-07-03 14:13:33 +01:00
|
|
|
parse_dec_in_t in = PARSE_DEC_IN_INTG;
|
2014-03-21 10:55:08 +00:00
|
|
|
bool exp_neg = false;
|
2015-02-08 01:57:40 +00:00
|
|
|
mp_float_t frac_mult = 0.1;
|
2014-07-03 14:13:33 +01:00
|
|
|
mp_int_t exp_val = 0;
|
2015-02-08 01:57:40 +00:00
|
|
|
while (str < top) {
|
|
|
|
mp_uint_t dig = *str++;
|
2014-03-21 10:55:08 +00:00
|
|
|
if ('0' <= dig && dig <= '9') {
|
|
|
|
dig -= '0';
|
|
|
|
if (in == PARSE_DEC_IN_EXP) {
|
|
|
|
exp_val = 10 * exp_val + dig;
|
|
|
|
} else {
|
|
|
|
if (in == PARSE_DEC_IN_FRAC) {
|
2015-02-08 01:57:40 +00:00
|
|
|
dec_val += dig * frac_mult;
|
|
|
|
frac_mult *= 0.1;
|
|
|
|
} else {
|
|
|
|
dec_val = 10 * dec_val + dig;
|
2014-03-21 10:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (in == PARSE_DEC_IN_INTG && dig == '.') {
|
|
|
|
in = PARSE_DEC_IN_FRAC;
|
|
|
|
} else if (in != PARSE_DEC_IN_EXP && ((dig | 0x20) == 'e')) {
|
|
|
|
in = PARSE_DEC_IN_EXP;
|
2015-02-08 01:57:40 +00:00
|
|
|
if (str < top) {
|
|
|
|
if (str[0] == '+') {
|
|
|
|
str++;
|
|
|
|
} else if (str[0] == '-') {
|
|
|
|
str++;
|
|
|
|
exp_neg = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str == top) {
|
|
|
|
goto value_error;
|
2014-03-21 10:55:08 +00:00
|
|
|
}
|
|
|
|
} else if (allow_imag && (dig | 0x20) == 'j') {
|
|
|
|
imag = true;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// unknown character
|
2015-02-08 01:57:40 +00:00
|
|
|
str--;
|
2014-03-21 10:55:08 +00:00
|
|
|
break;
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// work out the exponent
|
|
|
|
if (exp_neg) {
|
|
|
|
exp_val = -exp_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply the exponent
|
|
|
|
for (; exp_val > 0; exp_val--) {
|
|
|
|
dec_val *= 10;
|
|
|
|
}
|
|
|
|
for (; exp_val < 0; exp_val++) {
|
|
|
|
dec_val *= 0.1;
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
|
|
|
|
// negate value if needed
|
|
|
|
if (dec_neg) {
|
|
|
|
dec_val = -dec_val;
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
// check we parsed something
|
|
|
|
if (str == str_val_start) {
|
|
|
|
goto value_error;
|
|
|
|
}
|
|
|
|
|
2014-03-21 10:55:08 +00:00
|
|
|
// skip trailing space
|
2014-03-21 12:15:59 +00:00
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
|
|
|
|
// check we reached the end of the string
|
|
|
|
if (str != top) {
|
2015-02-08 01:57:40 +00:00
|
|
|
goto value_error;
|
2014-02-22 18:12:43 +00:00
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
|
|
|
|
// return the object
|
2014-06-19 23:48:35 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2014-02-22 18:12:43 +00:00
|
|
|
if (imag) {
|
|
|
|
return mp_obj_new_complex(0, dec_val);
|
2014-03-21 11:45:46 +00:00
|
|
|
} else if (force_complex) {
|
|
|
|
return mp_obj_new_complex(dec_val, 0);
|
2014-06-19 23:48:35 +01:00
|
|
|
#else
|
|
|
|
if (imag || force_complex) {
|
2015-05-30 23:13:16 +01:00
|
|
|
raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "complex values not supported"), lex);
|
2014-06-19 23:48:35 +01:00
|
|
|
#endif
|
2014-02-22 18:12:43 +00:00
|
|
|
} else {
|
|
|
|
return mp_obj_new_float(dec_val);
|
|
|
|
}
|
2014-03-21 10:55:08 +00:00
|
|
|
|
2015-02-08 01:57:40 +00:00
|
|
|
value_error:
|
2015-05-30 23:13:16 +01:00
|
|
|
raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid syntax for number"), lex);
|
2015-02-08 01:57:40 +00:00
|
|
|
|
2014-02-22 18:12:43 +00:00
|
|
|
#else
|
2015-05-30 23:13:16 +01:00
|
|
|
raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, "decimal numbers not supported"), lex);
|
2014-02-22 18:12:43 +00:00
|
|
|
#endif
|
|
|
|
}
|