Skip to content

Commit ae8a38e

Browse files
committed
WIP
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent a367529 commit ae8a38e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

py/parsenum.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
198198
bool dec_neg = false;
199199
bool imag = false;
200200

201+
#if MICROPY_PY_BUILTINS_COMPLEX
202+
mp_float_t dec_real = 0;
203+
bool has_real = false;
204+
#endif
205+
206+
parse:
201207
// skip leading space
202208
for (; str < top && unichar_isspace(*str); str++) {
203209
}
@@ -332,13 +338,29 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
332338

333339
// check we reached the end of the string
334340
if (str != top) {
341+
#if MICROPY_PY_BUILTINS_COMPLEX
342+
if (force_complex && !imag && !has_real) {
343+
// If we've only seen a real so far, keep parsing for the imaginary part.
344+
dec_real = dec_val;
345+
dec_val = 0;
346+
has_real = true;
347+
goto parse;
348+
}
349+
#endif
335350
goto value_error;
336351
}
337352

353+
#if MICROPY_PY_BUILTINS_COMPLEX
354+
if (has_real && !imag) {
355+
// We're on the second part, but didn't get the expected imaginary number.
356+
goto value_error;
357+
}
358+
#endif
359+
338360
// return the object
339361
#if MICROPY_PY_BUILTINS_COMPLEX
340362
if (imag) {
341-
return mp_obj_new_complex(0, dec_val);
363+
return mp_obj_new_complex(dec_real, dec_val);
342364
} else if (force_complex) {
343365
return mp_obj_new_complex(dec_val, 0);
344366
}

tests/float/complex1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
print(complex("1"))
88
print(complex("1.2"))
99
print(complex("1.2j"))
10+
print(complex("1+2j"))
11+
print(complex("-1-2j"))
12+
print(complex("+1-2j"))
13+
print(complex(" -1-2j "))
14+
print(complex(" +1-2j "))
1015
print(complex(1, 2))
1116
print(complex(1j, 2j))
1217

0 commit comments

Comments
 (0)