Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Fix yet another bug in "if+ expr" comprehensions
Browse files Browse the repository at this point in the history
(that is, comprehensions without a "for" clause)

The comprehension would return the result infinitely instead of only
once.

Still part of #3975.
  • Loading branch information
lucaswerkmeister authored and gavinking committed Jan 27, 2014
1 parent 14685db commit 02297d3
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,21 @@ void generateComprehension(Comprehension that) {
tail = "return function(){return " + finished + ";}";
}
} else if (startClause instanceof ExpressionComprehensionClause) {
// return the expression result
gen.out("return function(){return ");
// record exhaustion state: return a function that
// * on first call, returns the expression,
// * on subsequent calls, returns finished.
String exhaustionVarName = names.createTempVariable("exhausted");
gen.out("var ", exhaustionVarName, "=false");
gen.endLine(true);
gen.out("return function()");
gen.beginBlock();
gen.out("if(", exhaustionVarName, ") return ", finished);
gen.endLine(true);
gen.out(exhaustionVarName, "=true");
gen.endLine(true);
gen.out("return ");
((ExpressionComprehensionClause)startClause).getExpression().visit(gen);
gen.out("};");
gen.endBlockNewLine(true);
for (int i = 0; i < initialIfClauses; i++) {
gen.endBlock();
}
Expand Down

0 comments on commit 02297d3

Please sign in to comment.