Commit 7d414a1b authored by Damien George's avatar Damien George
Browse files

py: Parse big-int/float/imag constants directly in parser.

Previous to this patch, a big-int, float or imag constant was interned
(made into a qstr) and then parsed at runtime to create an object each
time it was needed.  This is wasteful in RAM and not efficient.  Now,
these constants are parsed straight away in the parser and turned into
objects.  This allows constants with large numbers of digits (so
addresses issue #1103) and takes us a step closer to #722.
parent 5f97aaec
......@@ -161,9 +161,9 @@ STATIC mp_obj_t mod_ujson_loads(mp_obj_t obj) {
vstr_add_byte(&vstr, *s);
}
if (flt) {
next = mp_parse_num_decimal(vstr.buf, vstr.len, false, false);
next = mp_parse_num_decimal(vstr.buf, vstr.len, false, false, NULL);
} else {
next = mp_parse_num_integer(vstr.buf, vstr.len, 10);
next = mp_parse_num_integer(vstr.buf, vstr.len, 10, NULL);
}
break;
}
......
......@@ -34,12 +34,10 @@
#define MP_BC_LOAD_CONST_TRUE (0x12)
#define MP_BC_LOAD_CONST_ELLIPSIS (0x13)
#define MP_BC_LOAD_CONST_SMALL_INT (0x14) // signed var-int
#define MP_BC_LOAD_CONST_INT (0x15) // qstr
#define MP_BC_LOAD_CONST_DEC (0x16) // qstr
#define MP_BC_LOAD_CONST_BYTES (0x17) // qstr
#define MP_BC_LOAD_CONST_STRING (0x18) // qstr
#define MP_BC_LOAD_CONST_OBJ (0x09) // ptr; TODO renumber to be in order
#define MP_BC_LOAD_NULL (0x19)
#define MP_BC_LOAD_CONST_BYTES (0x15) // qstr
#define MP_BC_LOAD_CONST_STRING (0x16) // qstr
#define MP_BC_LOAD_CONST_OBJ (0x17) // ptr
#define MP_BC_LOAD_NULL (0x18)
#define MP_BC_LOAD_FAST_N (0x1a) // uint
#define MP_BC_LOAD_DEREF (0x1b) // uint
......
......@@ -47,6 +47,7 @@ typedef enum {
PN_maximum_number_of,
PN_string, // special node for non-interned string
PN_bytes, // special node for non-interned bytes
PN_const_object, // special node for a constant, generic Python object
} pn_kind_t;
#define EMIT(fun) (comp->emit_method_table->fun(comp->emit))
......@@ -174,6 +175,7 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
#endif
case PN_string:
case PN_bytes:
case PN_const_object:
return pn;
}
......@@ -432,6 +434,9 @@ STATIC bool cpython_c_tuple_is_const(mp_parse_node_t pn) {
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_bytes)) {
return true;
}
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
return true;
}
if (!MP_PARSE_NODE_IS_LEAF(pn)) {
return false;
}
......@@ -486,6 +491,12 @@ STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vst
return;
}
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_const_object)) {
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
mp_obj_print((mp_obj_t)pns->nodes[0], PRINT_REPR);
return;
}
assert(MP_PARSE_NODE_IS_LEAF(pn));
if (MP_PARSE_NODE_IS_SMALL_INT(pn)) {
vstr_printf(vstr, INT_FMT, MP_PARSE_NODE_LEAF_SMALL_INT(pn));
......@@ -495,8 +506,6 @@ STATIC void cpython_c_tuple_emit_const(compiler_t *comp, mp_parse_node_t pn, vst
mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
case MP_PARSE_NODE_ID: assert(0);
case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
case MP_PARSE_NODE_DECIMAL: vstr_printf(vstr, "%s", qstr_str(arg)); break;
case MP_PARSE_NODE_STRING:
case MP_PARSE_NODE_BYTES: {
mp_uint_t len;
......@@ -2159,7 +2168,8 @@ STATIC void compile_expr_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
// for non-REPL, evaluate then discard the expression
if ((MP_PARSE_NODE_IS_LEAF(pns->nodes[0]) && !MP_PARSE_NODE_IS_ID(pns->nodes[0]))
|| MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_string)
|| MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)) {
|| MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_bytes)
|| MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_const_object)) {
// do nothing with a lonely constant
} else {
compile_node(comp, pns->nodes[0]); // just an expression
......@@ -2954,6 +2964,10 @@ STATIC void compile_bytes(compiler_t *comp, mp_parse_node_struct_t *pns) {
}
}
STATIC void compile_const_object(compiler_t *comp, mp_parse_node_struct_t *pns) {
EMIT_ARG(load_const_obj, (mp_obj_t)pns->nodes[0]);
}
typedef void (*compile_function_t)(compiler_t*, mp_parse_node_struct_t*);
STATIC compile_function_t compile_function[] = {
#define nc NULL
......@@ -2966,6 +2980,7 @@ STATIC compile_function_t compile_function[] = {
NULL,
compile_string,
compile_bytes,
compile_const_object,
};
STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
......@@ -2978,8 +2993,6 @@ STATIC void compile_node(compiler_t *comp, mp_parse_node_t pn) {
mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break;
case MP_PARSE_NODE_INTEGER: EMIT_ARG(load_const_int, arg); break;
case MP_PARSE_NODE_DECIMAL: EMIT_ARG(load_const_dec, arg); break;
case MP_PARSE_NODE_STRING: EMIT_ARG(load_const_str, arg, false); break;
case MP_PARSE_NODE_BYTES: EMIT_ARG(load_const_str, arg, true); break;
case MP_PARSE_NODE_TOKEN: default:
......
......@@ -77,8 +77,6 @@ typedef struct _emit_method_table_t {
void (*import_star)(emit_t *emit);
void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
void (*load_const_small_int)(emit_t *emit, mp_int_t arg);
void (*load_const_int)(emit_t *emit, qstr qst);
void (*load_const_dec)(emit_t *emit, qstr qst);
void (*load_const_str)(emit_t *emit, qstr qst, bool bytes);
void (*load_const_obj)(emit_t *emit, void *obj);
void (*load_null)(emit_t *emit);
......
......@@ -473,16 +473,6 @@ STATIC void emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
STATIC void emit_bc_load_const_int(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_INT, qst);
}
STATIC void emit_bc_load_const_dec(emit_t *emit, qstr qst) {
emit_bc_pre(emit, 1);
emit_write_bytecode_byte_qstr(emit, MP_BC_LOAD_CONST_DEC, qst);
}
STATIC void emit_bc_load_const_str(emit_t *emit, qstr qst, bool bytes) {
emit_bc_pre(emit, 1);
if (bytes) {
......@@ -932,8 +922,6 @@ const emit_method_table_t emit_bc_method_table = {
emit_bc_import_star,
emit_bc_load_const_tok,
emit_bc_load_const_small_int,
emit_bc_load_const_int,
emit_bc_load_const_dec,
emit_bc_load_const_str,
emit_bc_load_const_obj,
emit_bc_load_null,
......
......@@ -171,20 +171,6 @@ STATIC void emit_cpy_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
STATIC void emit_cpy_load_const_int(emit_t *emit, qstr qst) {
emit_pre(emit, 1, 3);
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST %s\n", qstr_str(qst));
}
}
STATIC void emit_cpy_load_const_dec(emit_t *emit, qstr qst) {
emit_pre(emit, 1, 3);
if (emit->pass == MP_PASS_EMIT) {
printf("LOAD_CONST %s\n", qstr_str(qst));
}
}
STATIC void print_quoted_str(qstr qst, bool bytes) {
const char *str = qstr_str(qst);
int len = strlen(str);
......@@ -839,8 +825,6 @@ const emit_method_table_t emit_cpython_method_table = {
emit_cpy_import_star,
emit_cpy_load_const_tok,
emit_cpy_load_const_small_int,
emit_cpy_load_const_int,
emit_cpy_load_const_dec,
emit_cpy_load_const_str,
emit_cpy_load_const_obj,
emit_cpy_load_null,
......
......@@ -1142,21 +1142,6 @@ STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
}
}
STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
DEBUG_printf("load_const_int %s\n", qstr_str(qst));
// for viper: load integer, check fits in 32 bits
emit_native_pre(emit);
emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
// for viper, a float/complex is just a Python object
emit_native_pre(emit);
emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
}
STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
emit_native_pre(emit);
// TODO: Eventually we want to be able to work with raw pointers in viper to
......@@ -2317,8 +2302,6 @@ const emit_method_table_t EXPORT_FUN(method_table) = {
emit_native_import_star,
emit_native_load_const_tok,
emit_native_load_const_small_int,
emit_native_load_const_int,
emit_native_load_const_dec,
emit_native_load_const_str,
emit_native_load_const_obj,
emit_native_load_null,
......
......@@ -192,8 +192,6 @@ const emit_method_table_t emit_pass1_method_table = {
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
#if MICROPY_PY_BUILTINS_SET
(void*)emit_pass1_dummy,
(void*)emit_pass1_dummy,
......
......@@ -248,7 +248,7 @@ DEF_RULE(power_dbl_star, c(power_dbl_star), and(2), tok(OP_DBL_STAR), rule(facto
// testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
DEF_RULE(atom, nc, or(10), tok(NAME), tok(NUMBER), rule(atom_string), tok(ELLIPSIS), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
DEF_RULE(atom, nc, or(11), tok(NAME), tok(INTEGER), tok(FLOAT_OR_IMAG), rule(atom_string), tok(ELLIPSIS), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
DEF_RULE(atom_string, c(atom_string), one_or_more, rule(string_or_bytes))
DEF_RULE(string_or_bytes, nc, or(2), tok(STRING), tok(BYTES))
DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE))
......
......@@ -104,6 +104,10 @@ STATIC bool is_following_digit(mp_lexer_t *lex) {
return unichar_isdigit(lex->chr1);
}
STATIC bool is_following_letter(mp_lexer_t *lex) {
return unichar_isalpha(lex->chr1);
}
STATIC bool is_following_odigit(mp_lexer_t *lex) {
return lex->chr1 >= '0' && lex->chr1 <= '7';
}
......@@ -540,7 +544,15 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
}
} else if (is_digit(lex) || (is_char(lex, '.') && is_following_digit(lex))) {
lex->tok_kind = MP_TOKEN_NUMBER;
bool forced_integer = false;
if (is_char(lex, '.')) {
lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
} else {
lex->tok_kind = MP_TOKEN_INTEGER;
if (is_char(lex, '0') && is_following_letter(lex)) {
forced_integer = true;
}
}
// get first char
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
......@@ -548,14 +560,18 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
// get tail chars
while (!is_end(lex)) {
if (is_char_or(lex, 'e', 'E')) {
if (!forced_integer && is_char_or(lex, 'e', 'E')) {
lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
vstr_add_char(&lex->vstr, 'e');
next_char(lex);
if (is_char(lex, '+') || is_char(lex, '-')) {
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
next_char(lex);
}
} else if (is_letter(lex) || is_digit(lex) || is_char_or(lex, '_', '.')) {
} else if (is_letter(lex) || is_digit(lex) || is_char(lex, '.')) {
if (is_char_or3(lex, '.', 'j', 'J')) {
lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
}
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
next_char(lex);
} else {
......
......@@ -50,13 +50,14 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEDENT, // 7
MP_TOKEN_NAME, // 8
MP_TOKEN_NUMBER,
MP_TOKEN_INTEGER,
MP_TOKEN_FLOAT_OR_IMAG,
MP_TOKEN_STRING,
MP_TOKEN_BYTES,
MP_TOKEN_ELLIPSIS,
MP_TOKEN_KW_FALSE, // 13
MP_TOKEN_KW_FALSE, // 14
MP_TOKEN_KW_NONE,
MP_TOKEN_KW_TRUE,
MP_TOKEN_KW_AND,
......@@ -65,7 +66,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_BREAK,
MP_TOKEN_KW_CLASS,
MP_TOKEN_KW_CONTINUE,
MP_TOKEN_KW_DEF, // 22
MP_TOKEN_KW_DEF, // 23
MP_TOKEN_KW_DEL,
MP_TOKEN_KW_ELIF,
MP_TOKEN_KW_ELSE,
......@@ -75,7 +76,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_FROM,
MP_TOKEN_KW_GLOBAL,
MP_TOKEN_KW_IF,
MP_TOKEN_KW_IMPORT, // 32
MP_TOKEN_KW_IMPORT, // 33
MP_TOKEN_KW_IN,
MP_TOKEN_KW_IS,
MP_TOKEN_KW_LAMBDA,
......@@ -85,12 +86,12 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_KW_PASS,
MP_TOKEN_KW_RAISE,
MP_TOKEN_KW_RETURN,
MP_TOKEN_KW_TRY, // 42
MP_TOKEN_KW_TRY, // 43
MP_TOKEN_KW_WHILE,
MP_TOKEN_KW_WITH,
MP_TOKEN_KW_YIELD,
MP_TOKEN_OP_PLUS, // 46
MP_TOKEN_OP_PLUS, // 47
MP_TOKEN_OP_MINUS,
MP_TOKEN_OP_STAR,
MP_TOKEN_OP_DBL_STAR,
......@@ -100,7 +101,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_OP_LESS,
MP_TOKEN_OP_DBL_LESS,
MP_TOKEN_OP_MORE,
MP_TOKEN_OP_DBL_MORE, // 56
MP_TOKEN_OP_DBL_MORE, // 57
MP_TOKEN_OP_AMPERSAND,
MP_TOKEN_OP_PIPE,
MP_TOKEN_OP_CARET,
......@@ -110,7 +111,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_OP_DBL_EQUAL,
MP_TOKEN_OP_NOT_EQUAL,
MP_TOKEN_DEL_PAREN_OPEN, // 65
MP_TOKEN_DEL_PAREN_OPEN, // 66
MP_TOKEN_DEL_PAREN_CLOSE,
MP_TOKEN_DEL_BRACKET_OPEN,
MP_TOKEN_DEL_BRACKET_CLOSE,
......@@ -120,7 +121,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_COLON,
MP_TOKEN_DEL_PERIOD,
MP_TOKEN_DEL_SEMICOLON,
MP_TOKEN_DEL_AT, // 75
MP_TOKEN_DEL_AT, // 76
MP_TOKEN_DEL_EQUAL,
MP_TOKEN_DEL_PLUS_EQUAL,
MP_TOKEN_DEL_MINUS_EQUAL,
......@@ -130,7 +131,7 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_DEL_PERCENT_EQUAL,
MP_TOKEN_DEL_AMPERSAND_EQUAL,
MP_TOKEN_DEL_PIPE_EQUAL,
MP_TOKEN_DEL_CARET_EQUAL, // 85
MP_TOKEN_DEL_CARET_EQUAL, // 86
MP_TOKEN_DEL_DBL_MORE_EQUAL,
MP_TOKEN_DEL_DBL_LESS_EQUAL,
MP_TOKEN_DEL_DBL_STAR_EQUAL,
......
......@@ -76,7 +76,7 @@ STATIC mp_uint_t get_fmt_num(const char **p) {
while (unichar_isdigit(*++num)) {
len++;
}
mp_uint_t val = (mp_uint_t)MP_OBJ_SMALL_INT_VALUE(mp_parse_num_integer(*p, len, 10));
mp_uint_t val = (mp_uint_t)MP_OBJ_SMALL_INT_VALUE(mp_parse_num_integer(*p, len, 10, NULL));
*p = num;
return val;
}
......
......@@ -91,8 +91,6 @@ void mp_native_raise(mp_obj_t o) {
void *const mp_fun_table[MP_F_NUMBER_OF] = {
mp_convert_obj_to_native,
mp_convert_native_to_obj,
mp_load_const_int,
mp_load_const_dec,
mp_load_const_str,
mp_load_const_bytes,
mp_load_name,
......
......@@ -95,7 +95,7 @@ STATIC mp_obj_t complex_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
// a string, parse it
mp_uint_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_decimal(s, l, true, true);
return mp_parse_num_decimal(s, l, true, true, NULL);
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
// a complex, just return it
return args[0];
......
......@@ -77,7 +77,7 @@ STATIC mp_obj_t float_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
// a string, parse it
mp_uint_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_decimal(s, l, false, false);
return mp_parse_num_decimal(s, l, false, false, NULL);
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
// a float, just return it
return args[0];
......
......@@ -57,7 +57,7 @@ STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
// a string, parse it
mp_uint_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, 0);
return mp_parse_num_integer(s, l, 0, NULL);
#if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
......@@ -73,7 +73,7 @@ STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
// TODO proper error checking of argument types
mp_uint_t l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]));
return mp_parse_num_integer(s, l, mp_obj_get_int(args[1]), NULL);
}
}
}
......
......@@ -72,6 +72,7 @@ enum {
RULE_maximum_number_of,
RULE_string, // special node for non-interned string
RULE_bytes, // special node for non-interned bytes
RULE_const_object, // special node for a constant, generic Python object
};
#define ident (RULE_ACT_ALLOW_IDENT)
......@@ -170,7 +171,7 @@ mp_parse_node_t mp_parse_node_new_leaf(mp_int_t kind, mp_int_t arg) {
if (kind == MP_PARSE_NODE_SMALL_INT) {
return (mp_parse_node_t)(kind | (arg << 1));
}
return (mp_parse_node_t)(kind | (arg << 5));
return (mp_parse_node_t)(kind | (arg << 4));
}
void mp_parse_node_free(mp_parse_node_t pn) {
......@@ -180,6 +181,8 @@ void mp_parse_node_free(mp_parse_node_t pn) {
mp_uint_t rule_id = MP_PARSE_NODE_STRUCT_KIND(pns);
if (rule_id == RULE_string || rule_id == RULE_bytes) {
m_del(char, (char*)pns->nodes[0], (mp_uint_t)pns->nodes[1]);
} else if (rule_id == RULE_const_object) {
// don't free the const object since it's probably used by the compiled code
} else {
bool adjust = ADD_BLANK_NODE(rules[rule_id]);
if (adjust) {
......@@ -215,8 +218,6 @@ void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent) {
mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_INTEGER: printf("int(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_DECIMAL: printf("dec(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break;
case MP_PARSE_NODE_TOKEN: printf("tok(" INT_FMT ")\n", arg); break;
......@@ -229,6 +230,8 @@ void mp_parse_node_print(mp_parse_node_t pn, mp_uint_t indent) {
printf("literal str(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_bytes) {
printf("literal bytes(%.*s)\n", (int)pns->nodes[1], (char*)pns->nodes[0]);
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == RULE_const_object) {
printf("literal const(%p)\n", (mp_obj_t)pns->nodes[0]);
} else {
mp_uint_t n = MP_PARSE_NODE_STRUCT_NUM_NODES(pns);
#ifdef USE_RULE_NAME
......@@ -285,11 +288,11 @@ STATIC void push_result_node(parser_t *parser, mp_parse_node_t pn) {
parser->result_stack[parser->result_stack_top++] = pn;
}
STATIC void push_result_string_bytes(parser_t *parser, mp_uint_t src_line, mp_uint_t rule_kind, const char *str, mp_uint_t len) {
STATIC mp_parse_node_t make_node_string_bytes(parser_t *parser, mp_uint_t src_line, mp_uint_t rule_kind, const char *str, mp_uint_t len) {
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, 2);
if (pn == NULL) {
memory_error(parser);
return;
return MP_PARSE_NODE_NULL;
}
pn->source_line = src_line;
pn->kind_num_nodes = rule_kind | (2 << 8);
......@@ -297,7 +300,19 @@ STATIC void push_result_string_bytes(parser_t *parser, mp_uint_t src_line, mp_ui
memcpy(p, str, len);
pn->nodes[0] = (mp_int_t)p;
pn->nodes[1] = len;
push_result_node(parser, (mp_parse_node_t)pn);
return (mp_parse_node_t)pn;
}
STATIC mp_parse_node_t make_node_const_object(parser_t *parser, mp_uint_t src_line, mp_obj_t obj) {
mp_parse_node_struct_t *pn = m_new_obj_var_maybe(mp_parse_node_struct_t, mp_parse_node_t, 1);
if (pn == NULL) {
memory_error(parser);
return MP_PARSE_NODE_NULL;
}
pn->source_line = src_line;
pn->kind_num_nodes = RULE_const_object | (1 << 8);
pn->nodes[0] = (mp_uint_t)obj;
return (mp_parse_node_t)pn;
}
STATIC void push_result_token(parser_t *parser) {
......@@ -305,45 +320,16 @@ STATIC void push_result_token(parser_t *parser) {
mp_lexer_t *lex = parser->lexer;
if (lex->tok_kind == MP_TOKEN_NAME) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, qstr_from_strn(lex->vstr.buf, lex->vstr.len));
} else if (lex->tok_kind == MP_TOKEN_NUMBER) {
bool dec = false;
bool small_int = true;
mp_int_t int_val = 0;
mp_uint_t len = lex->vstr.len;
const char *str = lex->vstr.buf;
mp_uint_t base = 0;
mp_uint_t i = mp_parse_num_base(str, len, &base);
bool overflow = false;
for (; i < len; i++) {
mp_uint_t dig;
int clower = str[i] | 0x20;
if (unichar_isdigit(str[i]) && (mp_uint_t)(str[i] - '0') < base) {
dig = str[i] - '0';
} else if (base == 16 && 'a' <= clower && clower <= 'f') {
dig = clower - 'a' + 10;
} else if (str[i] == '.' || clower == 'e' || clower == 'j') {
dec = true;
break;
} else {
small_int = false;
break;
}
// add next digi and check for overflow
if (mp_small_int_mul_overflow(int_val, base)) {
overflow = true;
}
int_val = int_val * base + dig;
if (!MP_SMALL_INT_FITS(int_val)) {
overflow = true;
}
}
if (dec) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_DECIMAL, qstr_from_strn(str, len));
} else if (small_int && !overflow && MP_SMALL_INT_FITS(int_val)) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, int_val);
} else if (lex->tok_kind == MP_TOKEN_INTEGER) {
mp_obj_t o = mp_parse_num_integer(lex->vstr.buf, lex->vstr.len, 0, lex);
if (MP_OBJ_IS_SMALL_INT(o)) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, MP_OBJ_SMALL_INT_VALUE(o));
} else {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_INTEGER, qstr_from_strn(str, len));
pn = make_node_const_object(parser, lex->tok_line, o);
}
} else if (lex->tok_kind == MP_TOKEN_FLOAT_OR_IMAG) {
mp_obj_t o = mp_parse_num_decimal(lex->vstr.buf, lex->vstr.len, true, false, lex);
pn = make_node_const_object(parser, lex->tok_line, o);
} else if (lex->tok_kind == MP_TOKEN_STRING || lex->tok_kind == MP_TOKEN_BYTES) {
// Don't automatically intern all strings/bytes. doc strings (which are usually large)
// will be discarded by the compiler, and so we shouldn't intern them.
......@@ -360,8 +346,7 @@ STATIC void push_result_token(parser_t *parser) {
pn = mp_parse_node_new_leaf(lex->tok_kind == MP_TOKEN_STRING ? MP_PARSE_NODE_STRING : MP_PARSE_NODE_BYTES, qst);
} else {
// not interned, make a node holding a pointer to the string/bytes data
push_result_string_bytes(parser, lex->tok_line, lex->tok_kind == MP_TOKEN_STRING ? RULE_string : RULE_bytes, lex->vstr.buf, lex->vstr.len);
return;
pn = make_node_string_bytes(parser, lex->tok_line, lex->tok_kind == MP_TOKEN_STRING ? RULE_string : RULE_bytes, lex->vstr.buf, lex->vstr.len);
}
} else {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_TOKEN, lex->tok_kind);
......
......@@ -36,21 +36,17 @@ struct _mp_lexer_t;
// - 0000...0000: no node
// - xxxx...xxx1: a small integer; bits 1 and above are the signed value, 2's complement
// - xxxx...xx00: pointer to mp_parse_node_struct_t
// - xx...x00010: an identifier; bits 5 and above are the qstr
// - xx...x00110: an integer; bits 5 and above are the qstr holding the value
// - xx...x01010: a decimal; bits 5 and above are the qstr holding the value
// - xx...x01110: a string; bits 5 and above are the qstr holding the value
// - xx...x10010: a string of bytes; bits 5 and above are the qstr holding the value
// - xx...x10110: a token; bits 5 and above are mp_token_kind_t
// - xx...xx0010: an identifier; bits 4 and above are the qstr
// - xx...xx0110: a string; bits 4 and above are the qstr holding the value
// - xx...xx1010: a string of bytes; bits 4 and above are the qstr holding the value
// - xx...xx1110: a token; bits 4 and above are mp_token_kind_t
#define MP_PARSE_NODE_NULL (0)
#define MP_PARSE_NODE_SMALL_INT (0x1)
#define MP_PARSE_NODE_ID (0x02)
#define MP_PARSE_NODE_INTEGER (0x06)
#define MP_PARSE_NODE_DECIMAL (0x0a)
#define MP_PARSE_NODE_STRING (0x0e)
#define MP_PARSE_NODE_BYTES (0x12)
#define MP_PARSE_NODE_TOKEN (0x16)
#define MP_PARSE_NODE_STRING (0x06)
#define MP_PARSE_NODE_BYTES (0x0a)
#define MP_PARSE_NODE_TOKEN (0x0e)
typedef mp_uint_t mp_parse_node_t; // must be pointer size
......@@ -69,12 +65,12 @@ typedef struct _mp_parse_node_struct_t {
#define MP_PARSE_NODE_IS_STRUCT_KIND(pn, k) ((pn) != MP_PARSE_NODE_NULL && ((pn) & 3) == 0 && MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)(pn)) == (k))
#define MP_PARSE_NODE_IS_SMALL_INT(pn) (((pn) & 0x1) == MP_PARSE_NODE_SMALL_INT)
#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x1f) == MP_PARSE_NODE_ID)
#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x1f) == MP_PARSE_NODE_TOKEN)
#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 5)))
#define MP_PARSE_NODE_IS_ID(pn) (((pn) & 0x0f) == MP_PARSE_NODE_ID)
#define MP_PARSE_NODE_IS_TOKEN(pn) (((pn) & 0x0f) == MP_PARSE_NODE_TOKEN)
#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 4)))
#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x1f)
#define MP_PARSE_NODE_LEAF_ARG(pn) (((mp_uint_t)(pn)) >> 5)
#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x0f)
#define MP_PARSE_NODE_LEAF_ARG(pn) (((mp_uint_t)(pn)) >> 4)
#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((mp_int_t)(pn)) >> 1)
#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
#define MP_PARSE_NODE_STRUCT_NUM_NODES(pns) ((pns)->kind_num_nodes >> 8)
......
......@@ -35,7 +35,16 @@
#include <math.h>
#endif
mp_obj_t mp_parse_num_integer(const char *restrict str_, mp_uint_t len, mp_uint_t base) {
STATIC NORETURN void raise(mp_obj_t exc, mp_lexer_t *lex) {
// if lex!=NULL then the parser called us and we need to make a SyntaxError with traceback