Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commiteb4e18f

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 parent15d2fe8 commiteb4e18f

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
}elseif (MP_PARSE_NODE_STRUCT_KIND(pns)==PN_not_test_2) {
714714
c_if_cond(comp,pns->nodes[0], !jump_if,label);
715715
return;
716+
}elseif (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+
}elseif (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)

[8]ページ先頭

©2009-2025 Movatter.jp