bpo-32439: Clean up the code for compiling comparison expressions. (#5029)

This commit is contained in:
Serhiy Storchaka 2017-12-30 09:47:42 +02:00 committed by GitHub
parent f111b3dcb4
commit 02b9ef2775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 23 deletions

View File

@ -3420,35 +3420,32 @@ static int
compiler_compare(struct compiler *c, expr_ty e) compiler_compare(struct compiler *c, expr_ty e)
{ {
Py_ssize_t i, n; Py_ssize_t i, n;
basicblock *cleanup = NULL;
/* XXX the logic can be cleaned up for 1 or multiple comparisons */
VISIT(c, expr, e->v.Compare.left); VISIT(c, expr, e->v.Compare.left);
n = asdl_seq_LEN(e->v.Compare.ops); assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
assert(n > 0); n = asdl_seq_LEN(e->v.Compare.ops) - 1;
if (n > 1) { if (n == 0) {
cleanup = compiler_new_block(c); VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
ADDOP_I(c, COMPARE_OP,
cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));
}
else {
basicblock *cleanup = compiler_new_block(c);
if (cleanup == NULL) if (cleanup == NULL)
return 0; return 0;
VISIT(c, expr, for (i = 0; i < n; i++) {
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
}
for (i = 1; i < n; i++) {
ADDOP(c, DUP_TOP);
ADDOP(c, ROT_THREE);
ADDOP_I(c, COMPARE_OP,
cmpop((cmpop_ty)(asdl_seq_GET(
e->v.Compare.ops, i - 1))));
ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
NEXT_BLOCK(c);
if (i < (n - 1))
VISIT(c, expr, VISIT(c, expr,
(expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
} ADDOP(c, DUP_TOP);
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1)); ADDOP(c, ROT_THREE);
ADDOP_I(c, COMPARE_OP, ADDOP_I(c, COMPARE_OP,
cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1)))); cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));
if (n > 1) { ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
NEXT_BLOCK(c);
}
VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
ADDOP_I(c, COMPARE_OP,
cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));
basicblock *end = compiler_new_block(c); basicblock *end = compiler_new_block(c);
if (end == NULL) if (end == NULL)
return 0; return 0;