From 56e5ef203b01ac8dfb1cb46143f6f7c53237b79d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 22 Feb 2014 16:39:45 +0200 Subject: parse: Refactor parse node encoding to support full range of small ints. Based on suggestion by @dpgeorge at https://github.com/micropython/micropython/pull/313 --- py/parse.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'py/parse.c') diff --git a/py/parse.c b/py/parse.c index 57d78a05b1..e70456e814 100644 --- a/py/parse.c +++ b/py/parse.c @@ -125,7 +125,10 @@ STATIC void pop_rule(parser_t *parser, const rule_t **rule, uint *arg_i, uint *s } mp_parse_node_t mp_parse_node_new_leaf(machine_int_t kind, machine_int_t arg) { - return (mp_parse_node_t)(kind | (arg << 4)); + if (kind == MP_PARSE_NODE_SMALL_INT) { + return (mp_parse_node_t)(kind | (arg << 1)); + } + return (mp_parse_node_t)(kind | (arg << 5)); } //int num_parse_nodes_allocated = 0; @@ -171,11 +174,13 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) { } if (MP_PARSE_NODE_IS_NULL(pn)) { printf("NULL\n"); + } else if (MP_PARSE_NODE_IS_SMALL_INT(pn)) { + machine_int_t arg = MP_PARSE_NODE_LEAF_SMALL_INT(pn); + printf("int(" INT_FMT ")\n", arg); } else if (MP_PARSE_NODE_IS_LEAF(pn)) { - machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pn); + machine_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_SMALL_INT: printf("int(" INT_FMT ")\n", 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; -- cgit v1.2.3