Skip to content

Commit eb4e18f

Browse files
committed
py: Add compiler optimisation for conditions in parenthesis.
Optimises: if () -> if False if (x,...) -> if True if (a and b) -> if a and b
1 parent 15d2fe8 commit eb4e18f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

py/compile.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,23 @@ STATIC void c_if_cond(compiler_t *comp, mp_parse_node_t pn, bool jump_if, int la
713713
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_not_test_2) {
714714
c_if_cond(comp, pns->nodes[0], !jump_if, label);
715715
return;
716+
} else if (MP_PARSE_NODE_STRUCT_KIND(pns) == PN_atom_paren) {
717+
// cond is something in parenthesis
718+
if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) {
719+
// empty tuple, acts as false for the condition
720+
if (jump_if == false) {
721+
EMIT_ARG(jump, label);
722+
}
723+
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_testlist_comp)) {
724+
// non-empty tuple, acts as true for the condition
725+
if (jump_if == true) {
726+
EMIT_ARG(jump, label);
727+
}
728+
} else {
729+
// parenthesis around 1 item, is just that item
730+
c_if_cond(comp, pns->nodes[0], jump_if, label);
731+
}
732+
return;
716733
}
717734
}
718735

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy