|
From: <fwi...@us...> - 2008-09-02 14:41:12
|
Revision: 5281
http://jython.svn.sourceforge.net/jython/?rev=5281&view=rev
Author: fwierzbicki
Date: 2008-09-02 14:41:10 +0000 (Tue, 02 Sep 2008)
Log Message:
-----------
Coding standards and cosmetic changes.
Modified Paths:
--------------
trunk/jython/src/org/python/compiler/ScopesCompiler.java
Modified: trunk/jython/src/org/python/compiler/ScopesCompiler.java
===================================================================
--- trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-09-01 14:33:42 UTC (rev 5280)
+++ trunk/jython/src/org/python/compiler/ScopesCompiler.java 2008-09-02 14:41:10 UTC (rev 5281)
@@ -18,29 +18,33 @@
private int func_level = 0;
public ScopesCompiler(CompilationContext code_compiler,
- Hashtable nodeScopes)
- {
+ Hashtable nodeScopes) {
this.code_compiler = code_compiler;
this.nodeScopes = nodeScopes;
scopes = new Stack();
}
public void beginScope(String name, int kind, PythonTree node,
- ArgListCompiler ac)
- {
+ ArgListCompiler ac) {
if (cur != null) {
scopes.push(cur);
}
- if (kind == FUNCSCOPE) func_level++;
- cur = new ScopeInfo(name, node, level++, kind,
- func_level, ac);
+ 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--;
+ if (cur.kind == FUNCSCOPE) {
+ func_level--;
+ }
level--;
- ScopeInfo up = (!scopes.empty())?(ScopeInfo)scopes.pop():null;
+ ScopeInfo up = null;
+ if (!scopes.empty()) {
+ up = (ScopeInfo)scopes.pop();
+ }
//Go into the stack to find a non class containing scope to use making the closure
//See PEP 227
int dist = 1;
@@ -49,7 +53,7 @@
referenceable = ((ScopeInfo)scopes.get(i));
}
cur.cook(referenceable, dist, code_compiler);
- cur.dump(); // dbg
+ //cur.dump(); // debug
cur = up;
}
@@ -95,8 +99,7 @@
ac.visitArgs(node.args);
exprType[] defaults = ac.getDefaults();
- int defc = defaults.length;
- for (int i = 0; i < defc; i++) {
+ for (int i = 0; i < defaults.length; i++) {
visit(defaults[i]);
}
@@ -119,18 +122,17 @@
ac.visitArgs(node.args);
PythonTree[] defaults = ac.getDefaults();
- int defc = defaults.length;
- for (int i = 0; i < defc; i++) {
+ for (int i = 0; i < defaults.length; i++) {
visit(defaults[i]);
}
beginScope("<lambda>", FUNCSCOPE, node, ac);
- int n = ac.names.size();
- for (int i = 0; i < n; i++) {
- cur.addParam((String)ac.names.elementAt(i));
+ for (Object o : ac.names) {
+ cur.addParam((String)o);
}
- for (int i = 0; i < ac.init_code.size(); i++)
- visit((stmtType) ac.init_code.elementAt(i));
+ for (Object o : ac.init_code) {
+ visit((stmtType)o);
+ }
cur.markFromParam();
visit(node.body);
endScope();
@@ -138,20 +140,19 @@
}
public void suite(stmtType[] stmts) throws Exception {
- int n = stmts.length;
- for (int i = 0; i < n; i++)
+ for (int i = 0; i < stmts.length; i++)
visit(stmts[i]);
}
public Object visitImport(Import node) throws Exception {
- int n = node.names.length;
- for (int i = 0; i < n; i++) {
- if (node.names[i].asname != null)
+ for (int i = 0; i < node.names.length; i++) {
+ if (node.names[i].asname != null) {
cur.addBound(node.names[i].asname);
- else {
+ } else {
String name = node.names[i].name;
- if (name.indexOf('.') > 0)
+ if (name.indexOf('.') > 0) {
name = name.substring(0, name.indexOf('.'));
+ }
cur.addBound(name);
}
}
@@ -166,10 +167,11 @@
return null;
}
for (int i = 0; i < n; i++) {
- if (node.names[i].asname != null)
+ if (node.names[i].asname != null) {
cur.addBound(node.names[i].asname);
- else
+ } else {
cur.addBound(node.names[i].name);
+ }
}
return null;
}
@@ -180,14 +182,21 @@
String name = node.names[i];
int prev = cur.addGlobal(name);
if (prev >= 0) {
- if ((prev&FROM_PARAM) != 0)
+ if ((prev&FROM_PARAM) != 0) {
code_compiler.error("name '"+name+"' is local and global",
true,node);
- if ((prev&GLOBAL) != 0) continue;
+ }
+ if ((prev&GLOBAL) != 0) {
+ continue;
+ }
String what;
- if ((prev&BOUND) != 0) what = "assignment"; else what = "use";
+ if ((prev&BOUND) != 0) {
+ what = "assignment";
+ } else {
+ what = "use";
+ }
code_compiler.error("name '"+name+"' declared global after "+
- what,false,node);
+ what, false, node);
}
}
return null;
@@ -195,29 +204,19 @@
public Object visitExec(Exec node) throws Exception {
cur.exec = true;
- if (node.globals == null && node.locals == null)
+ if (node.globals == null && node.locals == null) {
cur.unqual_exec = true;
+ }
traverse(node);
return null;
}
-/*
- private static void illassign(PythonTree node) throws Exception {
- String target = "operator";
- if (node.id == PythonGrammarTreeConstants.JJTCALL_OP) {
- target = "function call";
- } else if ((node.id == PythonGrammarTreeConstants.JJTFOR_STMT)) {
- target = "list comprehension";
- }
- throw new ParseException("can't assign to "+target,node);
- }
-*/
-
public Object visitClassDef(ClassDef node) throws Exception {
def(node.name);
int n = node.bases.length;
- for (int i = 0; i < n; i++)
+ for (int i = 0; i < n; i++) {
visit(node.bases[i]);
+ }
beginScope(node.name, CLASSSCOPE, node, null);
suite(node.body);
endScope();
@@ -227,11 +226,13 @@
public Object visitName(Name node) throws Exception {
String name = node.id;
if (node.ctx != expr_contextType.Load) {
- if (name.equals("__debug__"))
+ if (name.equals("__debug__")) {
code_compiler.error("can not assign to __debug__", true,node);
+ }
cur.addBound(name);
+ } else {
+ cur.addUsed(name);
}
- else cur.addUsed(name);
return null;
}
@@ -260,7 +261,6 @@
cur.addParam(bound_exp);
cur.markFromParam();
- //yield stuff
cur.generator = true;
cur.yield_count++;
traverse(node);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|