2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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 03:14:26 -04:00
|
|
|
#include <stdbool.h>
|
2014-02-22 13:12:43 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-10-16 21:17:37 -04:00
|
|
|
#include "py/runtime.h"
|
2016-03-14 18:34:03 -04:00
|
|
|
#include "py/parsenumbase.h"
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/parsenum.h"
|
|
|
|
#include "py/smallint.h"
|
2014-02-22 13:12:43 -05:00
|
|
|
|
2014-06-01 08:32:54 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-03-21 06:55:08 -04:00
|
|
|
#include <math.h>
|
|
|
|
#endif
|
|
|
|
|
2015-05-30 18:13:16 -04:00
|
|
|
STATIC NORETURN void raise_exc(mp_obj_t exc, mp_lexer_t *lex) {
|
2015-06-23 12:08:51 -04: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-07 20:57:40 -05:00
|
|
|
if (lex != NULL) {
|
2020-02-26 23:36:53 -05:00
|
|
|
((mp_obj_base_t *)MP_OBJ_TO_PTR(exc))->type = &mp_type_SyntaxError;
|
2019-09-25 01:53:30 -04:00
|
|
|
mp_obj_exception_add_traceback(exc, lex->source_name, lex->tok_line, MP_QSTRnull);
|
2015-02-07 20:57:40 -05:00
|
|
|
}
|
|
|
|
nlr_raise(exc);
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:34:03 -04:00
|
|
|
mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, mp_lexer_t *lex) {
|
2014-06-14 10:56:44 -04:00
|
|
|
const byte *restrict str = (const byte *)str_;
|
|
|
|
const byte *restrict top = str + len;
|
2014-03-21 08:15:59 -04:00
|
|
|
bool neg = false;
|
2014-05-28 09:07:21 -04:00
|
|
|
mp_obj_t ret_val;
|
2014-02-22 13:12:43 -05:00
|
|
|
|
|
|
|
// check radix base
|
|
|
|
if ((base != 0 && base < 2) || base > 36) {
|
2015-02-07 20:57:40 -05:00
|
|
|
// this won't be reached if lex!=NULL
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("int() arg 2 must be >= 2 and <= 36"));
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
2014-03-21 08:15:59 -04:00
|
|
|
|
|
|
|
// skip leading space
|
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
// parse optional sign
|
|
|
|
if (str < top) {
|
|
|
|
if (*str == '+') {
|
|
|
|
str++;
|
|
|
|
} else if (*str == '-') {
|
|
|
|
str++;
|
|
|
|
neg = true;
|
|
|
|
}
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
// parse optional base prefix
|
2020-02-26 23:36:53 -05:00
|
|
|
str += mp_parse_num_base((const char *)str, top - str, &base);
|
2014-03-21 08:15:59 -04:00
|
|
|
|
|
|
|
// string should be an integer number
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t int_val = 0;
|
2014-06-14 10:56:44 -04:00
|
|
|
const byte *restrict str_val_start = str;
|
2014-03-21 08:15:59 -04:00
|
|
|
for (; str < top; str++) {
|
2014-05-28 09:51:12 -04:00
|
|
|
// get next digit as a value
|
2014-07-03 09:13:33 -04:00
|
|
|
mp_uint_t dig = *str;
|
2016-12-27 20:02:49 -05:00
|
|
|
if ('0' <= dig && dig <= '9') {
|
|
|
|
dig -= '0';
|
2018-06-01 10:21:46 -04:00
|
|
|
} else if (dig == '_') {
|
|
|
|
continue;
|
2016-12-27 20:02:49 -05:00
|
|
|
} else {
|
|
|
|
dig |= 0x20; // make digit lower-case
|
|
|
|
if ('a' <= dig && dig <= 'z') {
|
|
|
|
dig -= 'a' - 10;
|
2014-03-21 08:15:59 -04:00
|
|
|
} else {
|
|
|
|
// unknown character
|
|
|
|
break;
|
|
|
|
}
|
2016-12-27 20:02:49 -05:00
|
|
|
}
|
2016-12-27 20:25:00 -05:00
|
|
|
if (dig >= (mp_uint_t)base) {
|
2014-03-21 08:15:59 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-05-28 09:51:12 -04:00
|
|
|
|
|
|
|
// add next digi and check for overflow
|
|
|
|
if (mp_small_int_mul_overflow(int_val, base)) {
|
2014-03-21 08:15:59 -04:00
|
|
|
goto overflow;
|
2014-05-28 09:51:12 -04:00
|
|
|
}
|
|
|
|
int_val = int_val * base + dig;
|
|
|
|
if (!MP_SMALL_INT_FITS(int_val)) {
|
2014-03-21 08:15:59 -04:00
|
|
|
goto overflow;
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
// negate value if needed
|
|
|
|
if (neg) {
|
|
|
|
int_val = -int_val;
|
|
|
|
}
|
2014-02-22 13:12:43 -05:00
|
|
|
|
2014-05-28 09:07:21 -04: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 08:15:59 -04:00
|
|
|
// skip trailing space
|
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
|
|
|
}
|
2014-02-22 13:12:43 -05:00
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
// check we reached the end of the string
|
|
|
|
if (str != top) {
|
2014-03-21 16:46:38 -04:00
|
|
|
goto value_error;
|
2014-03-21 08:15:59 -04:00
|
|
|
}
|
2014-02-22 13:12:43 -05:00
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
// return the object
|
2014-05-28 09:07:21 -04:00
|
|
|
return ret_val;
|
2014-03-21 16:46:38 -04:00
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
overflow:
|
2014-05-28 09:07:21 -04:00
|
|
|
// reparse using long int
|
|
|
|
{
|
2020-02-26 23:36:53 -05:00
|
|
|
const char *s2 = (const char *)str_val_start;
|
2014-05-28 09:07:21 -04:00
|
|
|
ret_val = mp_obj_new_int_from_str_len(&s2, top - str_val_start, neg, base);
|
2020-02-26 23:36:53 -05:00
|
|
|
str = (const byte *)s2;
|
2014-05-28 09:07:21 -04:00
|
|
|
goto have_ret_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_error:
|
2019-09-26 08:52:04 -04:00
|
|
|
{
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
|
2015-06-23 09:30:49 -04:00
|
|
|
mp_obj_t exc = mp_obj_new_exception_msg(&mp_type_ValueError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("invalid syntax for integer"));
|
2015-05-30 18:13:16 -04:00
|
|
|
raise_exc(exc, lex);
|
2019-09-26 08:52:04 -04:00
|
|
|
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
|
2015-02-07 20:57:40 -05:00
|
|
|
mp_obj_t exc = mp_obj_new_exception_msg_varg(&mp_type_ValueError,
|
2020-03-02 06:35:22 -05:00
|
|
|
MP_ERROR_TEXT("invalid syntax for integer with base %d"), base);
|
2015-10-01 12:06:13 -04:00
|
|
|
raise_exc(exc, lex);
|
2019-09-26 08:52:04 -04:00
|
|
|
#else
|
2015-10-01 12:06:13 -04:00
|
|
|
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,
|
2022-08-23 04:35:37 -04:00
|
|
|
mp_obj_new_str_from_utf8_vstr(&vstr));
|
2015-05-30 18:13:16 -04:00
|
|
|
raise_exc(exc, lex);
|
2019-09-26 08:52:04 -04:00
|
|
|
#endif
|
2014-11-06 12:36:16 -05:00
|
|
|
}
|
2014-03-21 08:15:59 -04:00
|
|
|
}
|
2014-02-22 13:12:43 -05:00
|
|
|
|
2021-08-06 12:16:59 -04:00
|
|
|
enum {
|
|
|
|
REAL_IMAG_STATE_START = 0,
|
|
|
|
REAL_IMAG_STATE_HAVE_REAL = 1,
|
|
|
|
REAL_IMAG_STATE_HAVE_IMAG = 2,
|
|
|
|
};
|
|
|
|
|
2014-07-03 09:13:33 -04:00
|
|
|
typedef enum {
|
|
|
|
PARSE_DEC_IN_INTG,
|
|
|
|
PARSE_DEC_IN_FRAC,
|
|
|
|
PARSE_DEC_IN_EXP,
|
|
|
|
} parse_dec_in_t;
|
2014-02-22 13:12:43 -05:00
|
|
|
|
2022-07-28 23:21:00 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-11-26 20:51:52 -05:00
|
|
|
// DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing
|
2018-02-07 22:02:50 -05:00
|
|
|
// SMALL_NORMAL_VAL is the smallest power of 10 that is still a normal float
|
2018-09-13 08:00:42 -04:00
|
|
|
// EXACT_POWER_OF_10 is the largest value of x so that 10^x can be stored exactly in a float
|
|
|
|
// Note: EXACT_POWER_OF_10 is at least floor(log_5(2^mantissa_length)). Indeed, 10^n = 2^n * 5^n
|
|
|
|
// so we only have to store the 5^n part in the mantissa (the 2^n part will go into the float's
|
|
|
|
// exponent).
|
2022-07-28 23:21:00 -04:00
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
2017-11-26 20:51:52 -05:00
|
|
|
#define DEC_VAL_MAX 1e20F
|
2018-02-07 22:02:50 -05:00
|
|
|
#define SMALL_NORMAL_VAL (1e-37F)
|
|
|
|
#define SMALL_NORMAL_EXP (-37)
|
2018-09-13 08:00:42 -04:00
|
|
|
#define EXACT_POWER_OF_10 (9)
|
2022-07-28 23:21:00 -04:00
|
|
|
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
2017-11-26 20:51:52 -05:00
|
|
|
#define DEC_VAL_MAX 1e200
|
2018-02-07 22:02:50 -05:00
|
|
|
#define SMALL_NORMAL_VAL (1e-307)
|
|
|
|
#define SMALL_NORMAL_EXP (-307)
|
2018-09-13 08:00:42 -04:00
|
|
|
#define EXACT_POWER_OF_10 (22)
|
2022-07-28 23:21:00 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Break out inner digit accumulation routine to ease trailing zero deferral.
|
|
|
|
static void accept_digit(mp_float_t *p_dec_val, int dig, int *p_exp_extra, int in) {
|
|
|
|
// Core routine to ingest an additional digit.
|
|
|
|
if (*p_dec_val < DEC_VAL_MAX) {
|
|
|
|
// dec_val won't overflow so keep accumulating
|
|
|
|
*p_dec_val = 10 * *p_dec_val + dig;
|
|
|
|
if (in == PARSE_DEC_IN_FRAC) {
|
|
|
|
--(*p_exp_extra);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// dec_val might overflow and we anyway can't represent more digits
|
|
|
|
// of precision, so ignore the digit and just adjust the exponent
|
|
|
|
if (in == PARSE_DEC_IN_INTG) {
|
|
|
|
++(*p_exp_extra);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 11:41:23 -04:00
|
|
|
#endif // MICROPY_PY_BUILTINS_FLOAT
|
2022-07-28 23:21:00 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
|
|
|
mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex)
|
|
|
|
#else
|
|
|
|
mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lexer_t *lex)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-11-26 20:51:52 -05:00
|
|
|
|
2014-03-21 06:55:08 -04:00
|
|
|
const char *top = str + len;
|
2014-02-22 13:12:43 -05:00
|
|
|
mp_float_t dec_val = 0;
|
2014-03-21 06:55:08 -04:00
|
|
|
bool dec_neg = false;
|
2021-08-06 12:16:59 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2022-06-21 20:37:48 -04:00
|
|
|
unsigned int real_imag_state = REAL_IMAG_STATE_START;
|
2021-08-06 12:16:59 -04:00
|
|
|
mp_float_t dec_real = 0;
|
|
|
|
parse_start:
|
|
|
|
#endif
|
2014-03-21 06:55:08 -04:00
|
|
|
|
|
|
|
// skip leading space
|
2014-03-21 08:15:59 -04:00
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
2014-03-21 06:55:08 -04:00
|
|
|
}
|
|
|
|
|
2014-03-21 08:15:59 -04:00
|
|
|
// parse optional sign
|
2014-03-21 06:55:08 -04:00
|
|
|
if (str < top) {
|
|
|
|
if (*str == '+') {
|
|
|
|
str++;
|
|
|
|
} else if (*str == '-') {
|
|
|
|
str++;
|
|
|
|
dec_neg = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 20:57:40 -05:00
|
|
|
const char *str_val_start = str;
|
|
|
|
|
2014-03-21 06:55:08 -04: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;
|
2020-04-09 03:05:48 -04:00
|
|
|
dec_val = (mp_float_t)INFINITY;
|
2014-03-21 06:55:08 -04:00
|
|
|
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 13:12:43 -05:00
|
|
|
}
|
2014-03-21 06:55:08 -04: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 07:45:46 -04:00
|
|
|
// string should be a decimal number
|
2014-07-03 09:13:33 -04:00
|
|
|
parse_dec_in_t in = PARSE_DEC_IN_INTG;
|
2014-03-21 06:55:08 -04:00
|
|
|
bool exp_neg = false;
|
2018-05-20 22:27:38 -04:00
|
|
|
int exp_val = 0;
|
|
|
|
int exp_extra = 0;
|
2022-07-28 23:21:00 -04:00
|
|
|
int trailing_zeros_intg = 0, trailing_zeros_frac = 0;
|
2015-02-07 20:57:40 -05:00
|
|
|
while (str < top) {
|
2018-05-20 22:27:38 -04:00
|
|
|
unsigned int dig = *str++;
|
2014-03-21 06:55:08 -04:00
|
|
|
if ('0' <= dig && dig <= '9') {
|
|
|
|
dig -= '0';
|
|
|
|
if (in == PARSE_DEC_IN_EXP) {
|
2018-05-19 11:56:34 -04:00
|
|
|
// don't overflow exp_val when adding next digit, instead just truncate
|
|
|
|
// it and the resulting float will still be correct, either inf or 0.0
|
|
|
|
// (use INT_MAX/2 to allow adding exp_extra at the end without overflow)
|
|
|
|
if (exp_val < (INT_MAX / 2 - 9) / 10) {
|
|
|
|
exp_val = 10 * exp_val + dig;
|
|
|
|
}
|
2014-03-21 06:55:08 -04:00
|
|
|
} else {
|
2022-07-28 23:21:00 -04:00
|
|
|
if (dig == 0 || dec_val >= DEC_VAL_MAX) {
|
|
|
|
// Defer treatment of zeros in fractional part. If nothing comes afterwards, ignore them.
|
|
|
|
// Also, once we reach DEC_VAL_MAX, treat every additional digit as a trailing zero.
|
|
|
|
if (in == PARSE_DEC_IN_INTG) {
|
|
|
|
++trailing_zeros_intg;
|
|
|
|
} else {
|
|
|
|
++trailing_zeros_frac;
|
2017-11-26 20:51:52 -05:00
|
|
|
}
|
|
|
|
} else {
|
2022-07-28 23:21:00 -04:00
|
|
|
// Time to un-defer any trailing zeros. Intg zeros first.
|
|
|
|
while (trailing_zeros_intg) {
|
|
|
|
accept_digit(&dec_val, 0, &exp_extra, PARSE_DEC_IN_INTG);
|
|
|
|
--trailing_zeros_intg;
|
|
|
|
}
|
|
|
|
while (trailing_zeros_frac) {
|
|
|
|
accept_digit(&dec_val, 0, &exp_extra, PARSE_DEC_IN_FRAC);
|
|
|
|
--trailing_zeros_frac;
|
2017-11-26 20:51:52 -05:00
|
|
|
}
|
2022-07-28 23:21:00 -04:00
|
|
|
accept_digit(&dec_val, dig, &exp_extra, in);
|
2014-03-21 06:55:08 -04: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-07 20:57:40 -05: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 06:55:08 -04:00
|
|
|
}
|
2018-06-01 10:21:46 -04:00
|
|
|
} else if (dig == '_') {
|
|
|
|
continue;
|
2014-03-21 06:55:08 -04:00
|
|
|
} else {
|
|
|
|
// unknown character
|
2015-02-07 20:57:40 -05:00
|
|
|
str--;
|
2014-03-21 06:55:08 -04:00
|
|
|
break;
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
2014-03-21 06:55:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// work out the exponent
|
|
|
|
if (exp_neg) {
|
|
|
|
exp_val = -exp_val;
|
|
|
|
}
|
|
|
|
|
2018-02-07 22:02:50 -05:00
|
|
|
// apply the exponent, making sure it's not a subnormal value
|
2022-07-28 23:21:00 -04:00
|
|
|
exp_val += exp_extra + trailing_zeros_intg;
|
2018-02-07 22:02:50 -05:00
|
|
|
if (exp_val < SMALL_NORMAL_EXP) {
|
|
|
|
exp_val -= SMALL_NORMAL_EXP;
|
|
|
|
dec_val *= SMALL_NORMAL_VAL;
|
|
|
|
}
|
2018-09-13 08:00:42 -04:00
|
|
|
|
|
|
|
// At this point, we need to multiply the mantissa by its base 10 exponent. If possible,
|
|
|
|
// we would rather manipulate numbers that have an exact representation in IEEE754. It
|
|
|
|
// turns out small positive powers of 10 do, whereas small negative powers of 10 don't.
|
|
|
|
// So in that case, we'll yield a division of exact values rather than a multiplication
|
|
|
|
// of slightly erroneous values.
|
|
|
|
if (exp_val < 0 && exp_val >= -EXACT_POWER_OF_10) {
|
|
|
|
dec_val /= MICROPY_FLOAT_C_FUN(pow)(10, -exp_val);
|
|
|
|
} else {
|
|
|
|
dec_val *= MICROPY_FLOAT_C_FUN(pow)(10, exp_val);
|
|
|
|
}
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
2014-03-21 06:55:08 -04:00
|
|
|
|
2022-06-21 09:45:27 -04:00
|
|
|
if (allow_imag && str < top && (*str | 0x20) == 'j') {
|
2022-06-21 20:37:48 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2022-06-21 09:45:27 -04:00
|
|
|
if (str == str_val_start) {
|
|
|
|
// Convert "j" to "1j".
|
|
|
|
dec_val = 1;
|
|
|
|
}
|
|
|
|
++str;
|
|
|
|
real_imag_state |= REAL_IMAG_STATE_HAVE_IMAG;
|
2022-06-21 20:37:48 -04:00
|
|
|
#else
|
|
|
|
raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("complex values not supported")), lex);
|
|
|
|
#endif
|
2022-06-21 09:45:27 -04:00
|
|
|
}
|
|
|
|
|
2014-03-21 06:55:08 -04:00
|
|
|
// negate value if needed
|
|
|
|
if (dec_neg) {
|
|
|
|
dec_val = -dec_val;
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
2014-03-21 06:55:08 -04:00
|
|
|
|
2015-02-07 20:57:40 -05:00
|
|
|
// check we parsed something
|
|
|
|
if (str == str_val_start) {
|
|
|
|
goto value_error;
|
|
|
|
}
|
|
|
|
|
2014-03-21 06:55:08 -04:00
|
|
|
// skip trailing space
|
2014-03-21 08:15:59 -04:00
|
|
|
for (; str < top && unichar_isspace(*str); str++) {
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
2014-03-21 06:55:08 -04:00
|
|
|
|
|
|
|
// check we reached the end of the string
|
|
|
|
if (str != top) {
|
2021-08-06 12:16:59 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
|
|
|
if (force_complex && real_imag_state == REAL_IMAG_STATE_START) {
|
|
|
|
// If we've only seen a real so far, keep parsing for the imaginary part.
|
|
|
|
dec_real = dec_val;
|
|
|
|
dec_val = 0;
|
|
|
|
real_imag_state |= REAL_IMAG_STATE_HAVE_REAL;
|
|
|
|
goto parse_start;
|
|
|
|
}
|
|
|
|
#endif
|
2015-02-07 20:57:40 -05:00
|
|
|
goto value_error;
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|
2014-03-21 06:55:08 -04:00
|
|
|
|
2021-08-06 12:16:59 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
|
|
|
if (real_imag_state == REAL_IMAG_STATE_HAVE_REAL) {
|
|
|
|
// We're on the second part, but didn't get the expected imaginary number.
|
|
|
|
goto value_error;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-21 06:55:08 -04:00
|
|
|
// return the object
|
2022-06-21 20:37:48 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_COMPLEX
|
2021-08-06 12:16:59 -04:00
|
|
|
if (real_imag_state != REAL_IMAG_STATE_START) {
|
|
|
|
return mp_obj_new_complex(dec_real, dec_val);
|
2014-03-21 07:45:46 -04:00
|
|
|
} else if (force_complex) {
|
|
|
|
return mp_obj_new_complex(dec_val, 0);
|
2018-05-21 23:20:00 -04:00
|
|
|
}
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2022-06-21 20:37:48 -04:00
|
|
|
|
|
|
|
return mp_obj_new_float(dec_val);
|
2014-03-21 06:55:08 -04:00
|
|
|
|
2015-02-07 20:57:40 -05:00
|
|
|
value_error:
|
2020-03-02 06:35:22 -05:00
|
|
|
raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("invalid syntax for number")), lex);
|
2015-02-07 20:57:40 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
#else
|
2020-03-02 06:35:22 -05:00
|
|
|
raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("decimal numbers not supported")), lex);
|
2020-02-26 23:36:53 -05:00
|
|
|
#endif
|
2014-02-22 13:12:43 -05:00
|
|
|
}
|