PEP 308 implementation, including minor refdocs and some testcases. It

breaks the parser module, because it adds the if/else construct as well as
two new grammar rules for backward compatibility. If no one else fixes
parsermodule, I guess I'll go ahead and fix it later this week.

The TeX code was checked with texcheck.py, but not rendered. There is
actually a slight incompatibility:

>>> (x for x in lambda:0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: iteration over non-sequence

changes into

>>> (x for x in lambda: 0)
  File "<stdin>", line 1
    (x for x in lambda: 0)
                     ^
SyntaxError: invalid syntax

Since there's no way the former version can be useful, it's probably a
bugfix ;)
This commit is contained in:
Thomas Wouters 2006-02-27 00:24:13 +00:00
parent d3a5f53a27
commit dca3b9c797
11 changed files with 878 additions and 642 deletions

View file

@ -2009,6 +2009,30 @@ compiler_class(struct compiler *c, stmt_ty s)
return 1;
}
static int
compiler_ifexp(struct compiler *c, expr_ty e)
{
basicblock *end, *next;
assert(e->kind == IfExp_kind);
end = compiler_new_block(c);
if (end == NULL)
return 0;
next = compiler_new_block(c);
if (next == NULL)
return 0;
VISIT(c, expr, e->v.IfExp.test);
ADDOP_JREL(c, JUMP_IF_FALSE, next);
ADDOP(c, POP_TOP);
VISIT(c, expr, e->v.IfExp.body);
ADDOP_JREL(c, JUMP_FORWARD, end);
compiler_use_next_block(c, next);
ADDOP(c, POP_TOP);
VISIT(c, expr, e->v.IfExp.orelse);
compiler_use_next_block(c, end);
return 1;
}
static int
compiler_lambda(struct compiler *c, expr_ty e)
{
@ -3290,6 +3314,8 @@ compiler_visit_expr(struct compiler *c, expr_ty e)
break;
case Lambda_kind:
return compiler_lambda(c, e);
case IfExp_kind:
return compiler_ifexp(c, e);
case Dict_kind:
/* XXX get rid of arg? */
ADDOP_I(c, BUILD_MAP, 0);