Skip to content

Commit 8817cd8

Browse files
committed
a bit of naming for the environment's scope methods
1 parent 5305f1a commit 8817cd8

File tree

3 files changed

+42
-51
lines changed

3 files changed

+42
-51
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -228,31 +228,31 @@ private SourceSection deriveSourceSection(RuleNode node) {
228228

229229
@Override
230230
public Object visitFile_input(Python3Parser.File_inputContext ctx) {
231-
environment.enterScope(ctx.scope);
231+
environment.pushScope(ctx.scope);
232232
ExpressionNode file = asExpression(super.visitFile_input(ctx));
233233
deriveSourceSection(ctx, file);
234-
environment.leaveScope();
234+
environment.popScope();
235235
return factory.createModuleRoot(name, file, ctx.scope.getFrameDescriptor());
236236
}
237237

238238
@Override
239239
public Object visitEval_input(Python3Parser.Eval_inputContext ctx) {
240-
environment.enterScope(ctx.scope);
240+
environment.pushScope(ctx.scope);
241241
ExpressionNode node = (ExpressionNode) super.visitEval_input(ctx);
242242
deriveSourceSection(ctx, node);
243243
StatementNode evalReturn = factory.createFrameReturn(factory.createWriteLocal(node, environment.getReturnSlot()));
244244
ReturnTargetNode returnTarget = new ReturnTargetNode(evalReturn, factory.createReadLocal(environment.getReturnSlot()));
245245
FunctionRootNode functionRoot = factory.createFunctionRoot(node.getSourceSection(), name, false, ctx.scope.getFrameDescriptor(), returnTarget, environment.getExecutionCellSlots());
246-
environment.leaveScope();
246+
environment.popScope();
247247
return functionRoot;
248248
}
249249

250250
@Override
251251
public Object visitSingle_input(Python3Parser.Single_inputContext ctx) {
252-
environment.enterScope(ctx.scope);
252+
environment.pushScope(ctx.scope);
253253
ExpressionNode body = asExpression(super.visitSingle_input(ctx));
254254
deriveSourceSection(ctx, body);
255-
environment.leaveScope();
255+
environment.popScope();
256256
if (isInlineMode) {
257257
return body;
258258
} else {
@@ -468,7 +468,7 @@ private ExpressionNode createComprehensionExpression(ParserRuleContext ctx, Pyth
468468

469469
private ExpressionNode createComprehensionExpression(ParserRuleContext ctx, Python3Parser.Comp_forContext compctx, Function<ParserRuleContext, ExpressionNode> getBlock) {
470470
try {
471-
environment.enterScope(compctx.scope);
471+
environment.pushScope(compctx.scope);
472472
ExpressionNode block = getBlock.apply(ctx);
473473
ExpressionNode yield = factory.createYield(block, environment.getReturnSlot());
474474
yield.assignSourceSection(block.getSourceSection());
@@ -482,7 +482,7 @@ private ExpressionNode createComprehensionExpression(ParserRuleContext ctx, Pyth
482482
genExprDef.assignSourceSection(srcSection);
483483
return genExprDef;
484484
} finally {
485-
environment.leaveScope();
485+
environment.popScope();
486486
}
487487
}
488488

@@ -1440,7 +1440,7 @@ public Object visitFuncdef(Python3Parser.FuncdefContext ctx) {
14401440
}
14411441
}
14421442

1443-
environment.enterScope(ctx.scope);
1443+
environment.pushScope(ctx.scope);
14441444
environment.setDefaultArgumentNodes(defaultArgs);
14451445

14461446
/**
@@ -1491,7 +1491,7 @@ public Object visitFuncdef(Python3Parser.FuncdefContext ctx) {
14911491
} else {
14921492
funcDef = new FunctionDefinitionNode(funcName, enclosingClassName, doc, arity, defaults, ct, environment.getDefinitionCellSlots(), environment.getExecutionCellSlots());
14931493
}
1494-
environment.leaveScope();
1494+
environment.popScope();
14951495

14961496
ReadNode funcVar = environment.findVariable(funcName);
14971497
return funcVar.makeWriteNode(funcDef);
@@ -1624,7 +1624,7 @@ private PNode createLambda(ParserRuleContext ctx, VarargslistContext varargslist
16241624
}
16251625

16261626
String funcname = "anonymous";
1627-
environment.enterScope(scope);
1627+
environment.pushScope(scope);
16281628
environment.setDefaultArgumentNodes(defaultArgs);
16291629

16301630
/**
@@ -1667,7 +1667,7 @@ private PNode createLambda(ParserRuleContext ctx, VarargslistContext varargslist
16671667
} else {
16681668
funcDef = new FunctionDefinitionNode(funcname, null, null, arity, defaults, ct, environment.getDefinitionCellSlots(), environment.getExecutionCellSlots());
16691669
}
1670-
environment.leaveScope();
1670+
environment.popScope();
16711671

16721672
return funcDef;
16731673
}
@@ -1730,14 +1730,14 @@ public Object visitClassdef(Python3Parser.ClassdefContext ctx) {
17301730
ExpressionNode[] splatArguments = new ExpressionNode[2];
17311731
visitCallArglist(ctx.arglist(), argumentNodes, keywords, splatArguments);
17321732

1733-
environment.enterScope(ctx.scope);
1733+
environment.pushScope(ctx.scope);
17341734

17351735
ExpressionNode body = asClassBody(ctx.suite().accept(this), qualName);
17361736
ClassBodyRootNode classBodyRoot = factory.createClassBodyRoot(deriveSourceSection(ctx), className, environment.getCurrentFrame(), body, environment.getExecutionCellSlots());
17371737
RootCallTarget ct = Truffle.getRuntime().createCallTarget(classBodyRoot);
17381738
FunctionDefinitionNode funcDef = new FunctionDefinitionNode(className, null, null, Arity.createOneArgumentWithVarKwArgs(className),
17391739
factory.createBlock(), ct, environment.getDefinitionCellSlots(), environment.getExecutionCellSlots());
1740-
environment.leaveScope();
1740+
environment.popScope();
17411741

17421742
argumentNodes.add(0, factory.createStringLiteral(className));
17431743
argumentNodes.add(0, funcDef);
@@ -1855,11 +1855,11 @@ private StatementNode createGeneratorExpression(Python3Parser.Comp_forContext co
18551855
// TODO: async
18561856
ScopeInfo old = null;
18571857
if (iteratorInParentScope) {
1858-
old = environment.leaveScope();
1858+
old = environment.popScope();
18591859
}
18601860
ExpressionNode iterator = (ExpressionNode) comp_for.or_test().accept(this);
18611861
if (iteratorInParentScope) {
1862-
environment.enterScope(old);
1862+
environment.pushScope(old);
18631863
}
18641864

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

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,37 @@ public ScopeTranslator(ParserErrorCallback errors, TranslationEnvironment enviro
6666

6767
@Override
6868
public T visitFile_input(Python3Parser.File_inputContext ctx) {
69-
ctx.scope = environment.createScope(ctx, ScopeInfo.ScopeKind.Module);
69+
ctx.scope = environment.pushScope(ctx, ScopeInfo.ScopeKind.Module);
7070
try {
7171
return super.visitFile_input(ctx);
7272
} finally {
73-
environment.leaveScope();
73+
environment.popScope();
7474
}
7575
}
7676

7777
@Override
7878
public T visitSingle_input(Single_inputContext ctx) {
7979
if (interactive) {
80-
ctx.scope = environment.createScope(ctx, ScopeInfo.ScopeKind.Module);
80+
ctx.scope = environment.pushScope(ctx, ScopeInfo.ScopeKind.Module);
8181
} else if (curInlineLocals != null) {
82-
ctx.scope = environment.createScope(ctx, ScopeInfo.ScopeKind.Function, curInlineLocals);
82+
ctx.scope = environment.pushScope(ctx, ScopeInfo.ScopeKind.Function, curInlineLocals);
8383
}
8484
try {
8585
return super.visitSingle_input(ctx);
8686
} finally {
8787
if (interactive || curInlineLocals != null) {
88-
environment.leaveScope();
88+
environment.popScope();
8989
}
9090
}
9191
}
9292

9393
@Override
9494
public T visitEval_input(Python3Parser.Eval_inputContext ctx) {
95-
ctx.scope = environment.createScope(ctx, ScopeInfo.ScopeKind.Module);
95+
ctx.scope = environment.pushScope(ctx, ScopeInfo.ScopeKind.Module);
9696
try {
9797
return super.visitEval_input(ctx);
9898
} finally {
99-
environment.leaveScope();
99+
environment.popScope();
100100
}
101101
}
102102

@@ -110,7 +110,7 @@ public T visitFuncdef(Python3Parser.FuncdefContext ctx) {
110110
argListCompilers.add(argListCompiler);
111111
ctx.parameters().accept(argListCompiler);
112112
ctx.parameters().accept(this);
113-
ctx.scope = environment.createScope(ctx, ScopeKind.Function);
113+
ctx.scope = environment.pushScope(ctx, ScopeKind.Function);
114114
try {
115115
ArgListCompiler<T> argListCompiler2 = argListCompilers.remove(argListCompilers.size() - 1);
116116
assert argListCompiler2 == argListCompiler;
@@ -119,7 +119,7 @@ public T visitFuncdef(Python3Parser.FuncdefContext ctx) {
119119
}
120120
return ctx.suite().accept(this);
121121
} finally {
122-
environment.leaveScope();
122+
environment.popScope();
123123
}
124124
}
125125

@@ -128,11 +128,11 @@ public T visitLambdef_nocond(Python3Parser.Lambdef_nocondContext ctx) {
128128
ArgListCompiler<T> argListCompiler = new ArgListCompiler<>(errors);
129129
argListCompilers.add(argListCompiler);
130130
ctx.accept(argListCompiler);
131-
ctx.scope = environment.createScope(ctx, ScopeKind.Function);
131+
ctx.scope = environment.pushScope(ctx, ScopeKind.Function);
132132
try {
133133
return super.visitLambdef_nocond(ctx);
134134
} finally {
135-
environment.leaveScope();
135+
environment.popScope();
136136
}
137137
}
138138

@@ -153,11 +153,11 @@ public T visitLambdef(Python3Parser.LambdefContext ctx) {
153153
if (ctx.varargslist() != null) {
154154
ctx.accept(argListCompiler);
155155
}
156-
ctx.scope = environment.createScope(ctx, ScopeKind.Function);
156+
ctx.scope = environment.pushScope(ctx, ScopeKind.Function);
157157
try {
158158
return super.visitLambdef(ctx);
159159
} finally {
160-
environment.leaveScope();
160+
environment.popScope();
161161
}
162162
}
163163

@@ -224,11 +224,11 @@ public T visitNonlocal_stmt(Python3Parser.Nonlocal_stmtContext ctx) {
224224
@Override
225225
public T visitClassdef(Python3Parser.ClassdefContext ctx) {
226226
environment.createLocal(ctx.NAME().getText());
227-
ctx.scope = environment.createScope(ctx, ScopeKind.Class);
227+
ctx.scope = environment.pushScope(ctx, ScopeKind.Class);
228228
try {
229229
return super.visitClassdef(ctx);
230230
} finally {
231-
environment.leaveScope();
231+
environment.popScope();
232232
}
233233
}
234234

@@ -349,9 +349,9 @@ public T visitComp_for(Python3Parser.Comp_forContext ctx) {
349349
if (!(ctx.getParent() instanceof Python3Parser.Comp_iterContext)) {
350350
// the first iterator is eagerly evaluated in the outside scope, but any iterator under
351351
// the comp_iter is not
352-
ScopeInfo currentGeneratorScope = environment.leaveScope();
352+
ScopeInfo currentGeneratorScope = environment.popScope();
353353
visitOr_test(ctx.or_test());
354-
environment.enterScope(currentGeneratorScope);
354+
environment.pushScope(currentGeneratorScope);
355355
} else {
356356
visitOr_test(ctx.or_test());
357357
}
@@ -363,11 +363,11 @@ public T visitComp_for(Python3Parser.Comp_forContext ctx) {
363363
}
364364

365365
private T visitGenerator(ParserRuleContext ctx, Python3Parser.Comp_forContext compctx, Function<ParserRuleContext, T> block) {
366-
compctx.scope = environment.createScope(ctx, ScopeKind.Generator);
366+
compctx.scope = environment.pushScope(ctx, ScopeKind.Generator);
367367
try {
368368
return block.apply(ctx);
369369
} finally {
370-
environment.leaveScope();
370+
environment.popScope();
371371
}
372372
}
373373

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,30 @@ public static TranslationEnvironment createFromScope(ScopeInfo scope) {
7272
return environment;
7373
}
7474

75-
public ScopeInfo createScope(ParserRuleContext ctx, ScopeInfo.ScopeKind kind) {
76-
return createScope(ctx, kind, null);
75+
public ScopeInfo pushScope(ParserRuleContext ctx, ScopeInfo.ScopeKind kind) {
76+
return pushScope(ctx, kind, null);
7777
}
7878

79-
public ScopeInfo createScope(ParserRuleContext ctx, ScopeInfo.ScopeKind kind, FrameDescriptor frameDescriptor) {
80-
currentScope = new ScopeInfo(TranslationUtil.getScopeId(ctx, kind), kind, frameDescriptor, currentScope);
79+
public ScopeInfo pushScope(ParserRuleContext ctx, ScopeInfo.ScopeKind kind, FrameDescriptor frameDescriptor) {
80+
ScopeInfo newScope = new ScopeInfo(TranslationUtil.getScopeId(ctx, kind), kind, frameDescriptor, currentScope);
81+
pushScope(newScope);
8182
if (globalScope == null) {
8283
globalScope = currentScope;
8384
}
8485
return currentScope;
8586
}
8687

87-
public void enterScope(ScopeInfo scope) {
88+
public void pushScope(ScopeInfo scope) {
8889
assert scope != null;
8990
currentScope = scope;
9091
}
9192

92-
public ScopeInfo leaveScope() {
93+
public ScopeInfo popScope() {
9394
ScopeInfo old = currentScope;
9495
currentScope = currentScope.getParent();
9596
return old;
9697
}
9798

98-
public ScopeInfo pushCurentScope() {
99-
if (currentScope.getParent() != null) {
100-
ScopeInfo old = currentScope;
101-
currentScope = currentScope.getParent();
102-
return old;
103-
}
104-
assert false;
105-
return null;
106-
}
107-
10899
public boolean atModuleLevel() {
109100
assert currentScope != null;
110101
return currentScope == globalScope;

0 commit comments

Comments
 (0)