summaryrefslogtreecommitdiffstatshomepage
path: root/py/parsenum.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2021-08-07 02:16:59 +1000
committerDamien George <damien@micropython.org>2022-06-23 11:46:47 +1000
commit0172292762649db91f588107b2163ab6449dc7ca (patch)
treeee0fe2455737728a8c0ef925426c749374dfcfff /py/parsenum.c
parent7861eddd0fe7c9e290cdbc4586dde24d10b68c96 (diff)
downloadmicropython-0172292762649db91f588107b2163ab6449dc7ca.tar.gz
micropython-0172292762649db91f588107b2163ab6449dc7ca.zip
py/parsenum: Support parsing complex numbers of the form "a+bj".
To conform with CPython. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py/parsenum.c')
-rw-r--r--py/parsenum.c37
1 files changed, 32 insertions, 5 deletions
diff --git a/py/parsenum.c b/py/parsenum.c
index 1cfe842577..848a7732d9 100644
--- a/py/parsenum.c
+++ b/py/parsenum.c
@@ -166,6 +166,12 @@ value_error:
}
}
+enum {
+ REAL_IMAG_STATE_START = 0,
+ REAL_IMAG_STATE_HAVE_REAL = 1,
+ REAL_IMAG_STATE_HAVE_IMAG = 2,
+};
+
typedef enum {
PARSE_DEC_IN_INTG,
PARSE_DEC_IN_FRAC,
@@ -196,7 +202,12 @@ 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;
- bool imag = false;
+ unsigned int real_imag_state = REAL_IMAG_STATE_START;
+
+ #if MICROPY_PY_BUILTINS_COMPLEX
+ mp_float_t dec_real = 0;
+parse_start:
+ #endif
// skip leading space
for (; str < top && unichar_isspace(*str); str++) {
@@ -281,7 +292,7 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
goto value_error;
}
} else if (allow_imag && (dig | 0x20) == 'j') {
- imag = true;
+ real_imag_state |= REAL_IMAG_STATE_HAVE_IMAG;
break;
} else if (dig == '_') {
continue;
@@ -332,18 +343,34 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
// check we reached the end of the string
if (str != top) {
+ #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
goto value_error;
}
+ #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
+
// return the object
#if MICROPY_PY_BUILTINS_COMPLEX
- if (imag) {
- return mp_obj_new_complex(0, dec_val);
+ 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 (imag || force_complex) {
+ 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