Generator expressions as direct function arguments
This commit is contained in:
parent
e97e42afc9
commit
0f6b74ff5b
@ -2703,6 +2703,9 @@ static void call(int canAssign) {
|
||||
startEatingWhitespace();
|
||||
size_t argCount = 0, specialArgs = 0, keywordArgs = 0, seenKeywordUnpacking = 0;
|
||||
if (!check(TOKEN_RIGHT_PAREN)) {
|
||||
size_t chunkBefore = currentChunk()->count;
|
||||
KrkScanner scannerBefore = krk_tellScanner();
|
||||
Parser parserBefore = parser;
|
||||
do {
|
||||
if (match(TOKEN_ASTERISK) || check(TOKEN_POW)) {
|
||||
specialArgs++;
|
||||
@ -2755,6 +2758,16 @@ static void call(int canAssign) {
|
||||
continue;
|
||||
}
|
||||
expression();
|
||||
if (argCount == 0 && match(TOKEN_FOR)) {
|
||||
currentChunk()->count = chunkBefore;
|
||||
generatorExpression(scannerBefore, parserBefore);
|
||||
argCount = 1;
|
||||
if (match(TOKEN_COMMA)) {
|
||||
error("Generator expression must be parenthesized");
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
argCount++;
|
||||
} while (match(TOKEN_COMMA));
|
||||
}
|
||||
|
@ -8,3 +8,14 @@ print(_sum([x * x for x in range(10)]))
|
||||
print(_sum((x * x for x in range(10))))
|
||||
|
||||
print('generator object' in str((x * x for x in range(10))))
|
||||
|
||||
# Should be directly usable as function argument
|
||||
print(_sum(x * x for x in range(10)))
|
||||
|
||||
import dis
|
||||
# Should have a compile error:
|
||||
try:
|
||||
dis.build('_sum(x * x for x in range(10), 2, 3)')
|
||||
except SyntaxError as e:
|
||||
print('parenthesized' in str(e))
|
||||
|
||||
|
5
test/testGeneratorExpressions.krk.expect
Normal file
5
test/testGeneratorExpressions.krk.expect
Normal file
@ -0,0 +1,5 @@
|
||||
285
|
||||
285
|
||||
True
|
||||
285
|
||||
True
|
Loading…
Reference in New Issue
Block a user