Skip to content

Commit f8161d4

Browse files
committed
first hack to get the example to work
1 parent 5d8ad34 commit f8161d4

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/ScopeTranslator.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.ArrayList;
2929
import java.util.HashSet;
3030
import java.util.List;
31+
import java.util.Stack;
3132
import java.util.function.Function;
3233

3334
import org.antlr.v4.runtime.ParserRuleContext;
@@ -55,7 +56,7 @@ public final class ScopeTranslator<T> extends Python3BaseVisitor<T> {
5556
private final ArrayList<String> possibleCellIdentifiers = new ArrayList<>();
5657
private final ArrayList<ScopeInfo> possibleCellScopes = new ArrayList<>();
5758

58-
private ScopeInfo currentGeneratorScope = null;
59+
private Stack<ScopeInfo> currentGeneratorScope = new Stack<>();
5960

6061
public ScopeTranslator(ParserErrorCallback errors, TranslationEnvironment environment, boolean interactive, FrameDescriptor curInlineLocals) {
6162
this.errors = errors;
@@ -354,32 +355,37 @@ public T visitComp_for(Python3Parser.Comp_forContext ctx) {
354355
public T visitOr_test(Python3Parser.Or_testContext ctx) {
355356
boolean pushedCurrentGeneratorScope = false;
356357
if (ctx.getParent() instanceof Python3Parser.Comp_forContext) {
357-
if (currentGeneratorScope == null && environment.getCurrentScopeLoopCount() == 1) {
358+
if (currentGeneratorScope.peek() == null && environment.getCurrentScopeLoopCount() == 1) {
358359
// the generator iterator needs to be early evaluated in the parent scope
359-
currentGeneratorScope = environment.pushCurentScope();
360+
currentGeneratorScope.pop();
361+
currentGeneratorScope.push(environment.pushCurentScope());
360362
pushedCurrentGeneratorScope = true;
361363
}
362364
}
363365
try {
364366
return super.visitOr_test(ctx);
365367
} finally {
366368
if (ctx.getParent() instanceof Python3Parser.Comp_forContext) {
367-
if (pushedCurrentGeneratorScope && currentGeneratorScope.getLoopCount() == 1) {
369+
if (pushedCurrentGeneratorScope) {
370+
ScopeInfo scopeInfo = currentGeneratorScope.pop();
368371
// restore the current scope
369-
environment.popCurrentScope(currentGeneratorScope);
370-
currentGeneratorScope = null;
372+
environment.popCurrentScope(scopeInfo);
373+
currentGeneratorScope.push(null);
371374
}
372375
}
373376
}
374377
}
375378

376379
private T visitGenerator(ParserRuleContext ctx, Python3Parser.Comp_forContext compctx, Function<ParserRuleContext, T> block) {
377380
compctx.scope = environment.createScope(ctx, ScopeKind.Generator);
381+
currentGeneratorScope.push(null);
378382
try {
379383
return block.apply(ctx);
380384
} finally {
381-
if (currentGeneratorScope == null) {
385+
if (currentGeneratorScope.pop() == null) {
382386
environment.leaveScope();
387+
} else {
388+
throw new IllegalStateException("why did the currentGeneratorScope leak?");
383389
}
384390
}
385391
}

0 commit comments

Comments
 (0)