-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathScopesCompiler.java
More file actions
384 lines (343 loc) · 12.1 KB
/
ScopesCompiler.java
File metadata and controls
384 lines (343 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// (C) Copyright 2001 Samuele Pedroni
package org.python.compiler;
import org.python.antlr.Visitor;
import org.python.antlr.PythonTree;
import org.python.antlr.ast.ClassDef;
import org.python.antlr.ast.DictComp;
import org.python.antlr.ast.Exec;
import org.python.antlr.ast.Expression;
import org.python.antlr.ast.FunctionDef;
import org.python.antlr.ast.GeneratorExp;
import org.python.antlr.ast.Global;
import org.python.antlr.ast.Import;
import org.python.antlr.ast.ImportFrom;
import org.python.antlr.ast.Interactive;
import org.python.antlr.ast.Lambda;
import org.python.antlr.ast.ListComp;
import org.python.antlr.ast.Name;
import org.python.antlr.ast.Return;
import org.python.antlr.ast.SetComp;
import org.python.antlr.ast.Tuple;
import org.python.antlr.ast.With;
import org.python.antlr.ast.Yield;
import org.python.antlr.ast.arguments;
import org.python.antlr.ast.comprehension;
import org.python.antlr.ast.expr_contextType;
import org.python.antlr.base.expr;
import org.python.antlr.base.stmt;
import org.python.core.ParserFacade;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Stack;
import java.util.List;
public class ScopesCompiler extends Visitor implements ScopeConstants {
private CompilationContext code_compiler;
private Stack<ScopeInfo> scopes;
private ScopeInfo cur = null;
private Hashtable<PythonTree,ScopeInfo> nodeScopes;
private int level = 0;
private int func_level = 0;
public ScopesCompiler(CompilationContext code_compiler, Hashtable<PythonTree,ScopeInfo> nodeScopes) {
this.code_compiler = code_compiler;
this.nodeScopes = nodeScopes;
scopes = new Stack<ScopeInfo>();
}
public void beginScope(String name, int kind, PythonTree node,
ArgListCompiler ac) {
if (cur != null) {
scopes.push(cur);
}
if (kind == FUNCSCOPE) {
func_level++;
}
cur = new ScopeInfo(name, node, level++, kind, func_level, ac);
nodeScopes.put(node, cur);
}
public void endScope() throws Exception {
if (cur.kind == FUNCSCOPE) {
func_level--;
}
level--;
ScopeInfo up = null;
if (!scopes.empty()) {
up = scopes.pop();
}
//Go into the stack to find a non class containing scope to use making the closure
//See PEP 227
int dist = 1;
ScopeInfo referenceable = up;
for (int i = scopes.size() - 1; i >= 0
&& referenceable.kind == CLASSSCOPE; i--, dist++) {
referenceable = (scopes.get(i));
}
cur.cook(referenceable, dist, code_compiler);
cur.dump(); // debug
cur = up;
}
public void parse(PythonTree node) throws Exception {
try {
visit(node);
} catch (Throwable t) {
throw ParserFacade.fixParseError(null, t, code_compiler.getFilename());
}
}
@Override
public Object visitInteractive(Interactive node) throws Exception {
beginScope("<single-top>", TOPSCOPE, node, null);
suite(node.getInternalBody());
endScope();
return null;
}
@Override
public Object visitModule(org.python.antlr.ast.Module node)
throws Exception {
beginScope("<file-top>", TOPSCOPE, node, null);
suite(node.getInternalBody());
endScope();
return null;
}
@Override
public Object visitExpression(Expression node) throws Exception {
beginScope("<eval-top>", TOPSCOPE, node, null);
visit(new Return(node,node.getInternalBody()));
endScope();
return null;
}
private void def(String name) {
cur.addBound(name);
}
@Override
public Object visitFunctionDef(FunctionDef node) throws Exception {
def(node.getInternalName());
ArgListCompiler ac = new ArgListCompiler();
ac.visitArgs(node.getInternalArgs());
List<expr> defaults = ac.getDefaults();
for (int i = 0; i < defaults.size(); i++) {
visit(defaults.get(i));
}
List<expr> decs = node.getInternalDecorator_list();
for (int i = decs.size() - 1; i >= 0; i--) {
visit(decs.get(i));
}
beginScope(node.getInternalName(), FUNCSCOPE, node, ac);
int n = ac.names.size();
for (int i = 0; i < n; i++) {
cur.addParam(ac.names.get(i));
}
for (int i = 0; i < ac.init_code.size(); i++) {
visit(ac.init_code.get(i));
}
cur.markFromParam();
suite(node.getInternalBody());
endScope();
return null;
}
@Override
public Object visitLambda(Lambda node) throws Exception {
ArgListCompiler ac = new ArgListCompiler();
ac.visitArgs(node.getInternalArgs());
List<? extends PythonTree> defaults = ac.getDefaults();
for (int i = 0; i < defaults.size(); i++) {
visit(defaults.get(i));
}
beginScope("<lambda>", FUNCSCOPE, node, ac);
for (Object o : ac.names) {
cur.addParam((String) o);
}
for (Object o : ac.init_code) {
visit((stmt) o);
}
cur.markFromParam();
visit(node.getInternalBody());
endScope();
return null;
}
public void suite(List<stmt> stmts) throws Exception {
for (int i = 0; i < stmts.size(); i++)
visit(stmts.get(i));
}
@Override
public Object visitImport(Import node) throws Exception {
for (int i = 0; i < node.getInternalNames().size(); i++) {
if (node.getInternalNames().get(i).getInternalAsname() != null) {
cur.addBound(node.getInternalNames().get(i).getInternalAsname());
} else {
String name = node.getInternalNames().get(i).getInternalName();
if (name.indexOf('.') > 0) {
name = name.substring(0, name.indexOf('.'));
}
cur.addBound(name);
}
}
return null;
}
@Override
public Object visitImportFrom(ImportFrom node) throws Exception {
Future.checkFromFuture(node); // future stmt support
int n = node.getInternalNames().size();
if (n == 0) {
cur.from_import_star = true;
return null;
}
for (int i = 0; i < n; i++) {
if (node.getInternalNames().get(i).getInternalAsname() != null) {
cur.addBound(node.getInternalNames().get(i).getInternalAsname());
} else {
cur.addBound(node.getInternalNames().get(i).getInternalName());
}
}
return null;
}
@Override
public Object visitGlobal(Global node) throws Exception {
int n = node.getInternalNames().size();
for (int i = 0; i < n; i++) {
String name = node.getInternalNames().get(i);
int prev = cur.addGlobal(name);
if (prev >= 0) {
if ((prev & FROM_PARAM) != 0) {
code_compiler.error("name '" + name
+ "' is local and global", true, node);
}
if ((prev & GLOBAL) != 0) {
continue;
}
String what;
if ((prev & BOUND) != 0) {
what = "assignment";
} else {
what = "use";
}
code_compiler.error("name '" + name
+ "' declared global after " + what, false, node);
}
}
return null;
}
@Override
public Object visitExec(Exec node) throws Exception {
cur.exec = true;
if (node.getInternalGlobals() == null && node.getInternalLocals() == null) {
cur.unqual_exec = true;
}
traverse(node);
return null;
}
@Override
public Object visitClassDef(ClassDef node) throws Exception {
List<expr> decs = node.getInternalDecorator_list();
for (int i = decs.size() - 1; i >= 0; i--) {
visit(decs.get(i));
}
def(node.getInternalName());
int n = node.getInternalBases().size();
for (int i = 0; i < n; i++) {
visit(node.getInternalBases().get(i));
}
beginScope(node.getInternalName(), CLASSSCOPE, node, null);
suite(node.getInternalBody());
endScope();
return null;
}
@Override
public Object visitName(Name node) throws Exception {
String name = node.getInternalId();
if (node.getInternalCtx() != expr_contextType.Load) {
if (name.equals("__debug__")) {
code_compiler.error("can not assign to __debug__", true, node);
}
cur.addBound(name);
} else {
cur.addUsed(name);
}
return null;
}
@Override
public Object visitListComp(ListComp node) throws Exception {
String tmp = "_[" + node.getLineno() + "_" + node.getCol_offset()
+ "]";
cur.addBound(tmp);
traverse(node);
return null;
}
@Override
public Object visitDictComp(DictComp node) throws Exception {
java.util.List<expr> kv = Arrays.asList(node.getInternalKey(), node.getInternalValue());
return visitInternalGenerators(
node, new Tuple(node, kv, expr_contextType.UNDEFINED), node.getInternalGenerators());
}
@Override
public Object visitSetComp(SetComp node) throws Exception {
return visitInternalGenerators(node, node.getInternalElt(), node.getInternalGenerators());
}
@Override
public Object visitYield(Yield node) throws Exception {
cur.defineAsGenerator(node);
cur.yield_count++;
traverse(node);
return null;
}
@Override
public Object visitReturn(Return node) throws Exception {
if (node.getInternalValue() != null) {
cur.noteReturnValue(node);
}
traverse(node);
return null;
}
private Object visitInternalGenerators(expr node, expr elt, java.util.List<comprehension> generators)
throws Exception {
// The first iterator is evaluated in the outer scope
if (generators != null && generators.size() > 0) {
visit(generators.get(0).getInternalIter());
}
String bound_exp = "_(x)";
String tmp = "_(" + node.getLineno() + "_" + node.getCol_offset()
+ ")";
def(tmp);
ArgListCompiler ac = new ArgListCompiler();
List<expr> args = new ArrayList<expr>();
args.add(new Name(node.getToken(), bound_exp, expr_contextType.Param));
ac.visitArgs(new arguments(node, args, null, null, new ArrayList<expr>()));
beginScope(tmp, FUNCSCOPE, node, ac);
cur.addParam(bound_exp);
cur.markFromParam();
cur.defineAsGenerator(node);
cur.yield_count++;
// The reset of the iterators are evaluated in the inner scope
if (elt != null) {
visit(elt);
}
if (generators != null) {
for (int i = 0; i < generators.size(); i++) {
if (generators.get(i) != null) {
if (i == 0) {
visit(generators.get(i).getInternalTarget());
if (generators.get(i).getInternalIfs() != null) {
for (expr cond : generators.get(i).getInternalIfs()) {
if (cond != null) {
visit(cond);
}
}
}
} else {
visit(generators.get(i));
}
}
}
}
endScope();
return null;
}
@Override
public Object visitGeneratorExp(GeneratorExp node) throws Exception {
return visitInternalGenerators(node, node.getInternalElt(), node.getInternalGenerators());
}
@Override
public Object visitWith(With node) throws Exception {
cur.max_with_count++;
traverse(node);
return null;
}
}