diff options
| author | Damien George <damien@micropython.org> | 2022-06-22 10:37:48 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2022-06-23 11:46:47 +1000 |
| commit | 627ba381545b73a383b9e56a4b29c03ac5914a4d (patch) | |
| tree | d667515be03eb09cda47310bae544ee1e92bee4f /py/parsenum.c | |
| parent | 61ce260ff73b1191a9190c8cac7a7a1bd5b2f274 (diff) | |
py/parsenum: Optimise when building with complex disabled.
To reduce code size when MICROPY_PY_BUILTINS_COMPLEX is disabled.
Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'py/parsenum.c')
| -rw-r--r-- | py/parsenum.c | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/py/parsenum.c b/py/parsenum.c index b90dfaaaf..5e8e5dfe8 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -178,7 +178,12 @@ typedef enum { PARSE_DEC_IN_EXP, } parse_dec_in_t; -mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex) { +#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 // DEC_VAL_MAX only needs to be rough and is used to retain precision while not overflowing @@ -202,9 +207,9 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool const char *top = str + len; mp_float_t dec_val = 0; bool dec_neg = false; - unsigned int real_imag_state = REAL_IMAG_STATE_START; #if MICROPY_PY_BUILTINS_COMPLEX + unsigned int real_imag_state = REAL_IMAG_STATE_START; mp_float_t dec_real = 0; parse_start: #endif @@ -325,12 +330,16 @@ parse_start: } if (allow_imag && str < top && (*str | 0x20) == 'j') { + #if MICROPY_PY_BUILTINS_COMPLEX if (str == str_val_start) { // Convert "j" to "1j". dec_val = 1; } ++str; real_imag_state |= REAL_IMAG_STATE_HAVE_IMAG; + #else + raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("complex values not supported")), lex); + #endif } // negate value if needed @@ -369,20 +378,16 @@ parse_start: #endif // return the object + #if MICROPY_PY_BUILTINS_COMPLEX if (real_imag_state != REAL_IMAG_STATE_START) { return mp_obj_new_complex(dec_real, dec_val); } else if (force_complex) { return mp_obj_new_complex(dec_val, 0); } - #else - if (real_imag_state != REAL_IMAG_STATE_START || force_complex) { - raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("complex values not supported")), lex); - } #endif - else { - return mp_obj_new_float(dec_val); - } + + return mp_obj_new_float(dec_val); value_error: raise_exc(mp_obj_new_exception_msg(&mp_type_ValueError, MP_ERROR_TEXT("invalid syntax for number")), lex); |
