summaryrefslogtreecommitdiffstatshomepage
path: root/py/compile.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-10-08 13:02:00 +0100
committerDamien George <damien.p.george@gmail.com>2015-10-08 13:02:00 +0100
commit7e12a601b8e9d90e5a7498523a9023c8c47bc4ca (patch)
tree12f14c1241de3b889746cb12bbd142beb4aa2bb6 /py/compile.c
parent2a8d7ee0f89b43fb2e65452343891897837f8231 (diff)
downloadmicropython-7e12a601b8e9d90e5a7498523a9023c8c47bc4ca.tar.gz
micropython-7e12a601b8e9d90e5a7498523a9023c8c47bc4ca.zip
py/compile: Fix edge case when constant-folding negation of integer.
Also adds tests specifically for testing constant folding.
Diffstat (limited to 'py/compile.c')
-rw-r--r--py/compile.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/py/compile.c b/py/compile.c
index f9d53bc689..9d2a6f2af8 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -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