diff options
author | Damien George <damien.p.george@gmail.com> | 2017-02-24 13:03:44 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-02-24 13:03:44 +1100 |
commit | 74f4d2c65945d727821503e768c71f4e6b37f471 (patch) | |
tree | cc0f3bc09270efc2f96477e6eb44a06349625701 /py/parse.c | |
parent | 1034d9acc84317b21b953d3e9fd6ad5519d1c570 (diff) | |
download | micropython-74f4d2c65945d727821503e768c71f4e6b37f471.tar.gz micropython-74f4d2c65945d727821503e768c71f4e6b37f471.zip |
py/parse: Allow parser/compiler consts to be bignums.
This patch allows uPy consts to be bignums, eg:
X = const(1 << 100)
The infrastructure for consts to be a bignum (rather than restricted to
small integers) has been in place for a while, ever since constant folding
was upgraded to allow bignums. It just required a small change (in this
patch) to enable it.
Diffstat (limited to 'py/parse.c')
-rw-r--r-- | py/parse.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/py/parse.c b/py/parse.c index d15af41581..7280f74870 100644 --- a/py/parse.c +++ b/py/parse.c @@ -436,7 +436,11 @@ STATIC void push_result_token(parser_t *parser, const rule_t *rule) { mp_map_elem_t *elem; if (rule->rule_id == RULE_atom && (elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP)) != NULL) { - pn = mp_parse_node_new_small_int(MP_OBJ_SMALL_INT_VALUE(elem->value)); + if (MP_OBJ_IS_SMALL_INT(elem->value)) { + pn = mp_parse_node_new_small_int(MP_OBJ_SMALL_INT_VALUE(elem->value)); + } else { + pn = make_node_const_object(parser, lex->tok_line, elem->value); + } } else { pn = mp_parse_node_new_leaf(MP_PARSE_NODE_ID, id); } @@ -666,16 +670,16 @@ STATIC bool fold_constants(parser_t *parser, const rule_t *rule, size_t num_args // get the value mp_parse_node_t pn_value = ((mp_parse_node_struct_t*)((mp_parse_node_struct_t*)pn1)->nodes[1])->nodes[0]; - if (!MP_PARSE_NODE_IS_SMALL_INT(pn_value)) { + mp_obj_t value; + if (!mp_parse_node_get_int_maybe(pn_value, &value)) { parser->parse_error = PARSE_ERROR_CONST; return false; } - mp_int_t value = MP_PARSE_NODE_LEAF_SMALL_INT(pn_value); // store the value in the table of dynamic constants mp_map_elem_t *elem = mp_map_lookup(&parser->consts, MP_OBJ_NEW_QSTR(id), MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); assert(elem->value == MP_OBJ_NULL); - elem->value = MP_OBJ_NEW_SMALL_INT(value); + elem->value = value; // If the constant starts with an underscore then treat it as a private // variable and don't emit any code to store the value to the id. |