diff options
author | Damien George <damien.p.george@gmail.com> | 2014-08-29 20:04:01 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-08-29 20:04:01 +0100 |
commit | eb4e18f0578db65573bf6db708f7f001dcb43715 (patch) | |
tree | b7ac94fe2adf146a0d5be87989c48354c3d8453e /py/compile.c | |
parent | 15d2fe8da4d08e92be4a79137343183a089cf6d0 (diff) | |
download | micropython-eb4e18f0578db65573bf6db708f7f001dcb43715.tar.gz micropython-eb4e18f0578db65573bf6db708f7f001dcb43715.zip |
py: Add compiler optimisation for conditions in parenthesis.
Optimises:
if () -> if False
if (x,...) -> if True
if (a and b) -> if a and b
Diffstat (limited to 'py/compile.c')
-rw-r--r-- | py/compile.c | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/py/compile.c b/py/compile.c index 49edc3f15c..267eb2c181 100644 --- a/py/compile.c +++ b/py/compile.c @@ -713,6 +713,23 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) { c_if_cond(comp, pns->nodes[0], !jump_if, label); return; + } else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) { + // cond is something in parenthesis + if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { + // empty tuple, acts as false for the condition + if (jump_if == false) { + EMIT_ARG(jump, label); + } + } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) { + // non-empty tuple, acts as true for the condition + if (jump_if == true) { + EMIT_ARG(jump, label); + } + } else { + // parenthesis around 1 item, is just that item + c_if_cond(comp, pns->nodes[0], jump_if, label); + } + return; } } |