py/compile: Fix edge case when constant-folding negation of integer.

Also adds tests specifically for testing constant folding.
This commit is contained in:
Damien George
2015-10-08 13:02:00 +01:00
parent 2a8d7ee0f8
commit 7e12a601b8
3 changed files with 44 additions and 7 deletions

View File

@@ -315,7 +315,10 @@ STATIC mp_parse_node_t fold_constants(compiler_t *comp, mp_parse_node_t pn, mp_m
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
} else if (MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_MINUS)) {
// -int
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, -arg);
arg = -arg;
if (MP_SMALL_INT_FITS(arg)) {
pn = mp_parse_node_new_leaf(MP_PARSE_NODE_SMALL_INT, arg);
}
} else {
assert(MP_PARSE_NODE_IS_TOKEN_KIND(pns->nodes[0], MP_TOKEN_OP_TILDE)); // should be
// ~int