Skip to content

Commit 5305f1a

Browse files
committed
further simplify the scope translation
1 parent 49fc9b6 commit 5305f1a

File tree

4 files changed

+11
-32
lines changed

4 files changed

+11
-32
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,11 +1855,11 @@ private StatementNode createGeneratorExpression(Python3Parser.Comp_forContext co
18551855
// TODO: async
18561856
ScopeInfo old = null;
18571857
if (iteratorInParentScope) {
1858-
old = environment.pushCurentScope();
1858+
old = environment.leaveScope();
18591859
}
18601860
ExpressionNode iterator = (ExpressionNode) comp_for.or_test().accept(this);
18611861
if (iteratorInParentScope) {
1862-
environment.popCurrentScope(old);
1862+
environment.enterScope(old);
18631863
}
18641864

18651865
StatementNode targets = assigns.translate(comp_for.exprlist());

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public enum ScopeKind {
8181
private List<ExpressionNode> defaultArgumentNodes;
8282
private ReadDefaultArgumentNode[] defaultArgumentReads;
8383

84-
private int loopCount = 0;
85-
8684
public ScopeInfo(String scopeId, ScopeKind kind, FrameDescriptor frameDescriptor, ScopeInfo parent) {
8785
this.scopeId = scopeId;
8886
this.scopeKind = kind;
@@ -96,18 +94,6 @@ public ScopeInfo(String scopeId, ScopeKind kind, FrameDescriptor frameDescriptor
9694
}
9795
}
9896

99-
public void incLoopCount() {
100-
loopCount++;
101-
}
102-
103-
public int getLoopCount() {
104-
return loopCount;
105-
}
106-
107-
public void resetLoopCount() {
108-
this.loopCount = 0;
109-
}
110-
11197
public ScopeInfo getFirstChildScope() {
11298
return firstChildScope;
11399
}

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

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

3433
import org.antlr.v4.runtime.ParserRuleContext;
@@ -347,13 +346,12 @@ public T visitComp_for(Python3Parser.Comp_forContext ctx) {
347346
declareNames(ctx.exprlist());
348347
visitExprlist(ctx.exprlist());
349348

350-
ScopeInfo currentGeneratorScope = environment.getCurrentScope();
351-
currentGeneratorScope.incLoopCount();
352-
if (currentGeneratorScope.getLoopCount() == 1) {
353-
// the first iterator is eagerly evaluated in the outside scope
354-
environment.pushCurentScope();
349+
if (!(ctx.getParent() instanceof Python3Parser.Comp_iterContext)) {
350+
// the first iterator is eagerly evaluated in the outside scope, but any iterator under
351+
// the comp_iter is not
352+
ScopeInfo currentGeneratorScope = environment.leaveScope();
355353
visitOr_test(ctx.or_test());
356-
environment.popCurrentScope(currentGeneratorScope);
354+
environment.enterScope(currentGeneratorScope);
357355
} else {
358356
visitOr_test(ctx.or_test());
359357
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public TranslationEnvironment(PythonLanguage language) {
6464
public static TranslationEnvironment createFromScope(ScopeInfo scope) {
6565
TranslationEnvironment environment = new TranslationEnvironment(PythonLanguage.getCurrent());
6666
environment.currentScope = scope;
67-
scope.resetLoopCount();
6867
ScopeInfo global = scope;
6968
while (global.getParent() != null) {
7069
global = global.getParent();
@@ -88,11 +87,12 @@ public ScopeInfo createScope(ParserRuleContext ctx, ScopeInfo.ScopeKind kind, Fr
8887
public void enterScope(ScopeInfo scope) {
8988
assert scope != null;
9089
currentScope = scope;
91-
scope.resetLoopCount();
9290
}
9391

94-
public void leaveScope() {
92+
public ScopeInfo leaveScope() {
93+
ScopeInfo old = currentScope;
9594
currentScope = currentScope.getParent();
95+
return old;
9696
}
9797

9898
public ScopeInfo pushCurentScope() {
@@ -101,15 +101,10 @@ public ScopeInfo pushCurentScope() {
101101
currentScope = currentScope.getParent();
102102
return old;
103103
}
104+
assert false;
104105
return null;
105106
}
106107

107-
public void popCurrentScope(ScopeInfo oldScope) {
108-
if (oldScope != null) {
109-
currentScope = oldScope;
110-
}
111-
}
112-
113108
public boolean atModuleLevel() {
114109
assert currentScope != null;
115110
return currentScope == globalScope;

0 commit comments

Comments
 (0)