Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57531fe

Browse files
committed
change syntactical position of lambdef (was an atom, now is a test)
1 parent ae3b3a3 commit 57531fe

3 files changed

Lines changed: 111 additions & 102 deletions

File tree

Grammar/Grammar

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally'
123123
except_clause: 'except' [test [',' test]]
124124
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
125125

126-
test: and_test ('or' and_test)*
126+
test: and_test ('or' and_test)* | lambdef
127127
and_test: not_test ('and' not_test)*
128128
not_test: 'not' not_test | comparison
129129
comparison: expr (comp_op expr)*
@@ -135,9 +135,7 @@ shift_expr: arith_expr (('<<'|'>>') arith_expr)*
135135
arith_expr: term (('+'|'-') term)*
136136
term: factor (('*'|'/'|'%') factor)*
137137
factor: ('+'|'-'|'~') factor | atom trailer*
138-
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | lambdef | NAME | NUMBER | STRING
139-
# Note ambiguity in grammar: "lambda x: x[1]" could mean "(lambda x: x)[1]"
140-
# but the parser is eager so interprets it as "lambda x: (x[1])"...
138+
atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
141139
lambdef: 'lambda' [varargslist] ':' test
142140
trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
143141
subscript: test | [test] ':' [test]

Python/compile.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -659,18 +659,6 @@ com_atom(c, n)
659659
}
660660
com_addoparg(c, LOAD_CONST, i);
661661
break;
662-
case lambdef:
663-
if ((v = (object *) compile(ch, c->c_filename)) == NULL) {
664-
c->c_errors++;
665-
i = 255;
666-
}
667-
else {
668-
i = com_addconst(c, v);
669-
DECREF(v);
670-
}
671-
com_addoparg(c, LOAD_CONST, i);
672-
com_addbyte(c, BUILD_FUNCTION);
673-
break;
674662
case NAME:
675663
com_addopname(c, LOAD_NAME, ch);
676664
break;
@@ -1106,20 +1094,35 @@ com_test(c, n)
11061094
struct compiling *c;
11071095
node *n;
11081096
{
1109-
int i;
1110-
int anchor;
1111-
REQ(n, test); /* and_test ('and' and_test)* */
1112-
anchor = 0;
1113-
i = 0;
1114-
for (;;) {
1115-
com_and_test(c, CHILD(n, i));
1116-
if ((i += 2) >= NCH(n))
1117-
break;
1118-
com_addfwref(c, JUMP_IF_TRUE, &anchor);
1119-
com_addbyte(c, POP_TOP);
1097+
REQ(n, test); /* and_test ('and' and_test)* | lambdef */
1098+
if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
1099+
object *v;
1100+
int i;
1101+
v = (object *) compile(CHILD(n, 0), c->c_filename);
1102+
if (v == NULL) {
1103+
c->c_errors++;
1104+
i = 255;
1105+
}
1106+
else {
1107+
i = com_addconst(c, v);
1108+
DECREF(v);
1109+
}
1110+
com_addoparg(c, LOAD_CONST, i);
1111+
com_addbyte(c, BUILD_FUNCTION);
1112+
}
1113+
else {
1114+
int anchor = 0;
1115+
int i = 0;
1116+
for (;;) {
1117+
com_and_test(c, CHILD(n, i));
1118+
if ((i += 2) >= NCH(n))
1119+
break;
1120+
com_addfwref(c, JUMP_IF_TRUE, &anchor);
1121+
com_addbyte(c, POP_TOP);
1122+
}
1123+
if (anchor)
1124+
com_backpatch(c, anchor);
11201125
}
1121-
if (anchor)
1122-
com_backpatch(c, anchor);
11231126
}
11241127

11251128
static void

0 commit comments

Comments
 (0)