Skip to content

Commit

Permalink
change syntactical position of lambdef (was an atom, now is a test)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Nov 30, 1993
1 parent ae3b3a3 commit 57531fe
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 102 deletions.
6 changes: 2 additions & 4 deletions Grammar/Grammar
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally'
except_clause: 'except' [test [',' test]]
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

test: and_test ('or' and_test)*
test: and_test ('or' and_test)* | lambdef
and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
Expand All @@ -135,9 +135,7 @@ shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
term: factor (('*'|'/'|'%') factor)*
factor: ('+'|'-'|'~') factor | atom trailer*
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | lambdef | NAME | NUMBER | STRING
# Note ambiguity in grammar: "lambda x: x[1]" could mean "(lambda x: x)[1]"
# but the parser is eager so interprets it as "lambda x: (x[1])"...
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
lambdef: 'lambda' [varargslist] ':' test
trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
subscript: test | [test] ':' [test]
Expand Down
53 changes: 28 additions & 25 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,18 +659,6 @@ com_atom(c, n)
}
com_addoparg(c, LOAD_CONST, i);
break;
case lambdef:
if ((v = (object *) compile(ch, c->c_filename)) == NULL) {
c->c_errors++;
i = 255;
}
else {
i = com_addconst(c, v);
DECREF(v);
}
com_addoparg(c, LOAD_CONST, i);
com_addbyte(c, BUILD_FUNCTION);
break;
case NAME:
com_addopname(c, LOAD_NAME, ch);
break;
Expand Down Expand Up @@ -1106,20 +1094,35 @@ com_test(c, n)
struct compiling *c;
node *n;
{
int i;
int anchor;
REQ(n, test); /* and_test ('and' and_test)* */
anchor = 0;
i = 0;
for (;;) {
com_and_test(c, CHILD(n, i));
if ((i += 2) >= NCH(n))
break;
com_addfwref(c, JUMP_IF_TRUE, &anchor);
com_addbyte(c, POP_TOP);
REQ(n, test); /* and_test ('and' and_test)* | lambdef */
if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
object *v;
int i;
v = (object *) compile(CHILD(n, 0), c->c_filename);
if (v == NULL) {
c->c_errors++;
i = 255;
}
else {
i = com_addconst(c, v);
DECREF(v);
}
com_addoparg(c, LOAD_CONST, i);
com_addbyte(c, BUILD_FUNCTION);
}
else {
int anchor = 0;
int i = 0;
for (;;) {
com_and_test(c, CHILD(n, i));
if ((i += 2) >= NCH(n))
break;
com_addfwref(c, JUMP_IF_TRUE, &anchor);
com_addbyte(c, POP_TOP);
}
if (anchor)
com_backpatch(c, anchor);
}
if (anchor)
com_backpatch(c, anchor);
}

static void
Expand Down
Loading

0 comments on commit 57531fe

Please sign in to comment.