8000 py: Add compiler optimisation for conditions in parenthesis. · mimoccc/circuitpython@eb4e18f · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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)
0