py/lexer: Add support for underscores in numeric literals.

This is a very convenient feature introduced in Python 3.6 by PEP 515.
crypto-aes
Damien George 5 years ago
parent b2fa1b50ed
commit 6a445b60fa

@ -590,6 +590,8 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
}
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
next_char(lex);
} else if (is_char(lex, '_')) {
next_char(lex);
} else {
break;
}

@ -83,6 +83,8 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m
mp_uint_t dig = *str;
if ('0' <= dig && dig <= '9') {
dig -= '0';
} else if (dig == '_') {
continue;
} else {
dig |= 0x20; // make digit lower-case
if ('a' <= dig && dig <= 'z') {
@ -273,6 +275,8 @@ mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool
} else if (allow_imag && (dig | 0x20) == 'j') {
imag = true;
break;
} else if (dig == '_') {
continue;
} else {
// unknown character
str--;

@ -0,0 +1,10 @@
# tests for things that only Python 3.6 supports
# underscores in numeric literals
print(100_000)
print(0b1010_0101)
print(0xff_ff)
# underscore supported by int constructor
print(int('1_2_3'))
print(int('0o1_2_3', 8))

@ -0,0 +1,5 @@
100000
165
65535
123
83

@ -0,0 +1,10 @@
# tests for things that only Python 3.6 supports, needing floats
# underscores in numeric literals
print(1_000.1_8)
print('%.2g' % 1e1_2)
# underscore supported by int/float constructors
print(float('1_2_3'))
print(float('1_2_3.4'))
print('%.2g' % float('1e1_3'))

@ -0,0 +1,5 @@
1000.18
1e+12
123.0
123.4
1e+13
Loading…
Cancel
Save