From: Finn B. <bc...@us...> - 2002-05-30 16:13:33
|
Update of /cvsroot/jython/jython/org/python/compiler In directory usw-pr-cvs1:/tmp/cvs-serv22958 Modified Files: ArgListCompiler.java CodeCompiler.java CompilationContext.java Future.java Module.java ScopeInfo.java ScopesCompiler.java SymInfo.java Log Message: Implementation of the new AST tree. Index: ArgListCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ArgListCompiler.java,v retrieving revision 2.8 retrieving revision 2.9 diff -C2 -d -r2.8 -r2.9 *** ArgListCompiler.java 27 Nov 2001 19:07:21 -0000 2.8 --- ArgListCompiler.java 30 May 2002 16:13:28 -0000 2.9 *************** *** 4,107 **** import org.python.parser.*; import java.io.IOException; import java.util.Vector; import java.util.Enumeration; ! public class ArgListCompiler extends org.python.parser.Visitor implements PythonGrammarTreeConstants { public boolean arglist, keywordlist; ! public Vector defaults; public Vector names; ! public SimpleNode init_code; public ArgListCompiler() { arglist = keywordlist = false; ! defaults = new Vector(); names = new Vector(); ! init_code = new SimpleNode(JJTSUITE); } public void reset() { arglist = keywordlist = false; ! defaults.removeAllElements(); names.removeAllElements(); ! //init_code.removeAllElements(); ! } ! ! public SimpleNode[] getDefaults() { ! SimpleNode[] children = new SimpleNode[defaults.size()]; ! for (int i=0; i<children.length; i++) { ! children[i] = (SimpleNode)defaults.elementAt(i); ! } ! return children; ! } ! ! public Object varargslist(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for (int i=0; i<n; i++) { ! node.getChild(i).visit(this); ! } ! return null; } ! public Object ExtraArgList(SimpleNode node) throws Exception { ! arglist = true; ! names.addElement(node.getChild(0).visit(this)); ! return null; } ! public Object ExtraKeywordList(SimpleNode node) throws Exception { ! keywordlist = true; ! names.addElement(node.getChild(0).visit(this)); ! return null; } ! public Object defaultarg(SimpleNode node) throws Exception { ! Object name = node.getChild(0).visit(this); ! // make sure the named argument isn't already in the list of arguments ! for (Enumeration e=names.elements(); e.hasMoreElements();) { ! String objname = (String)e.nextElement(); ! if (objname.equals(name)) ! throw new ParseException("duplicate argument name found: " + ! name, node); } ! names.addElement(name); ! ! //Handle tuple arguments properly ! if (node.getChild(0).id == JJTFPLIST) { ! SimpleNode expr = new SimpleNode(JJTEXPR_STMT); ! // Set the right line number for this expr ! expr.beginLine = node.beginLine; ! expr.jjtAddChild(node.getChild(0), 0); ! SimpleNode nm = new SimpleNode(JJTNAME); ! nm.setInfo(name); ! expr.jjtAddChild(nm, 1); ! init_code.jjtAddChild(expr, init_code.getNumChildren()); } ! ! // Handle default args if specified ! if (node.getNumChildren() > 1) { ! defaults.addElement(node.getChild(1)); ! } else { ! if (defaults.size() > 0) throw new ParseException( ! "non-default argument follows default argument"); } - return null; } ! public Object fplist(SimpleNode node) throws Exception { ! String name = "("; ! int n = node.getNumChildren(); ! for (int i=0; i<n-1; i++) { ! name = name+node.getChild(i).visit(this)+", "; } ! name = name+node.getChild(n-1).visit(this)+")"; ! return name; } ! public Object Name(SimpleNode node) throws ParseException, IOException { ! return node.getInfo(); } } --- 4,99 ---- import org.python.parser.*; + import org.python.parser.ast.*; import java.io.IOException; import java.util.Vector; import java.util.Enumeration; ! public class ArgListCompiler extends Visitor implements PythonGrammarTreeConstants { public boolean arglist, keywordlist; ! public exprType[] defaults; public Vector names; ! public Vector fpnames; ! public Vector init_code; public ArgListCompiler() { arglist = keywordlist = false; ! defaults = null; names = new Vector(); ! fpnames = new Vector(); ! init_code = new Vector(); } public void reset() { arglist = keywordlist = false; ! defaults = null; names.removeAllElements(); ! init_code.removeAllElements(); } ! public void appendInitCode(Suite node) { ! int n = node.body.length; ! stmtType[] newtree = new stmtType[init_code.size() + n]; ! init_code.copyInto(newtree); ! System.arraycopy(node.body, 0, newtree, init_code.size(), n); ! node.body = newtree; } ! public exprType[] getDefaults() { ! return defaults; } ! public void visitArgs(argumentsType args) throws Exception { ! for (int i = 0; i < args.args.length; i++) { ! String name = (String) visit(args.args[i]); ! names.addElement(name); ! if (args.args[i] instanceof Tuple) { ! Assign ass = new Assign( ! new exprType[] { args.args[i] }, ! new Name(name, Name.Load, args.args[i]), args.args[i]); ! init_code.addElement(ass); ! } } ! if (args.vararg != null) { ! arglist = true; ! names.addElement(args.vararg); } ! if (args.kwarg != null) { ! keywordlist = true; ! names.addElement(args.kwarg); ! } ! ! defaults = args.defaults; ! for (int i = 0; i < defaults.length; i++) { ! if (defaults[i] == null) throw new ParseException( ! "non-default argument follows default argument", ! args.args[args.args.length - defaults.length + i]); } } ! public Object visitName(Name node) throws Exception { ! if (node.ctx != Name.Store) ! return null; ! ! if (fpnames.contains(node.id)) { ! throw new ParseException("duplicate argument name found: " + ! node.id, node); } ! fpnames.addElement(node.id); ! return node.id; } ! public Object visitTuple(Tuple node) throws Exception { ! StringBuffer name = new StringBuffer("("); ! int n = node.elts.length; ! for (int i = 0; i < n-1; i++) { ! name.append(visit(node.elts[i])); ! name.append(", "); ! } ! name.append(visit(node.elts[n - 1])); ! name.append(")"); ! return name.toString(); } } Index: CodeCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/CodeCompiler.java,v retrieving revision 2.25 retrieving revision 2.26 diff -C2 -d -r2.25 -r2.26 *** CodeCompiler.java 13 Jan 2002 18:31:55 -0000 2.25 --- CodeCompiler.java 30 May 2002 16:13:29 -0000 2.26 *************** *** 4,9 **** --- 4,15 ---- import org.python.parser.*; + import org.python.parser.ast.*; + import org.python.parser.ast.Attribute; import org.python.core.Py; import org.python.core.PyObject; + import org.python.core.PyInteger; + import org.python.core.PyFloat; + import org.python.core.PyLong; [...3017 lines suppressed...] ! public Object String(SimpleNode node) throws Exception { ! String s = (String)node.getInfo(); if (s.length() > 32767) { throw new ParseException( --- 2127,2132 ---- } ! public Object visitStr(Str node) throws Exception { ! String s = node.s; if (s.length() > 32767) { throw new ParseException( *************** *** 2463,2465 **** --- 2138,2143 ---- } + protected Object unhandled_node(SimpleNode node) throws Exception { + throw new Exception("Unhandled node " + node); + } } Index: CompilationContext.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/CompilationContext.java,v retrieving revision 2.3 retrieving revision 2.4 diff -C2 -d -r2.3 -r2.4 *** CompilationContext.java 27 Nov 2001 19:07:21 -0000 2.3 --- CompilationContext.java 30 May 2002 16:13:29 -0000 2.4 *************** *** 12,14 **** --- 12,16 ---- public String getFilename(); + + public ScopeInfo getScopeInfo(SimpleNode node); } Index: Future.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/Future.java,v retrieving revision 2.6 retrieving revision 2.7 diff -C2 -d -r2.6 -r2.7 *** Future.java 13 Jan 2002 18:31:55 -0000 2.6 --- Future.java 30 May 2002 16:13:29 -0000 2.7 *************** *** 4,40 **** import org.python.parser.*; public class Future extends Object implements PythonGrammarTreeConstants { - private boolean nested_scopes; private boolean division; private static final String FUTURE = "__future__"; ! private boolean check(SimpleNode cand) throws Exception { ! SimpleNode dotted_name = cand.getChild(0); ! if (dotted_name.getNumChildren() != 1 || ! !((String)dotted_name.getChild(0).getInfo()).equals(FUTURE)) return false; ! int n = cand.getNumChildren(); ! if (n == 1) { throw new ParseException( "future statement does not support import *",cand); } ! for (int i = 1; i < n; i++) { ! SimpleNode imp = cand.getChild(i); ! String feature; ! switch(imp.id) { ! default: ! case JJTNAME: ! feature = (String)imp.getInfo(); ! break; ! case JJTIMPORT_AS_NAME: ! feature = (String)imp.getChild(0).getInfo(); ! break; ! } // *known* features if (feature.equals("nested_scopes")) { - nested_scopes = true; continue; } --- 4,28 ---- import org.python.parser.*; + import org.python.parser.ast.*; + import org.python.parser.ast.Module; public class Future extends Object implements PythonGrammarTreeConstants { private boolean division; private static final String FUTURE = "__future__"; ! private boolean check(ImportFrom cand) throws Exception { ! if (!cand.module.equals(FUTURE)) return false; ! int n = cand.names.length; ! if (n == 0) { throw new ParseException( "future statement does not support import *",cand); } ! for (int i = 0; i < n; i++) { ! String feature = cand.names[i].name; // *known* features if (feature.equals("nested_scopes")) { continue; } *************** *** 49,79 **** } ! public void preprocessFutures(SimpleNode node, org.python.core.CompilerFlags cflags) throws Exception { if (cflags != null) { - nested_scopes = cflags.nested_scopes; division = cflags.division; } - if ( node.id != JJTFILE_INPUT && node.id != JJTSINGLE_INPUT) - return; - int n = node.getNumChildren(); - if (n == 0) return; - int beg = 0; ! if (node.id == JJTFILE_INPUT && ! node.getChild(0).id == JJTEXPR_STMT && ! node.getChild(0).getChild(0).id == JJTSTRING) beg++; ! for (int i = beg; i < n; i++) { ! SimpleNode stmt = node.getChild(i); ! if (stmt.id != JJTIMPORTFROM) break; stmt.from_future_checked = true; ! if (!check(stmt)) break; } if (cflags != null) { - cflags.nested_scopes = cflags.nested_scopes || nested_scopes; cflags.division = cflags.division || division; } --- 37,71 ---- } ! public void preprocessFutures(modType node, org.python.core.CompilerFlags cflags) throws Exception { if (cflags != null) { division = cflags.division; } int beg = 0; ! stmtType[] suite = null; ! if (node instanceof Module) { ! suite = ((Module) node).body; ! if (suite.length > 0 && suite[0] instanceof Expr && ! ((Expr) suite[0]).value instanceof Str) { ! beg++; ! } ! } else if (node instanceof Interactive) { ! suite = new stmtType[] { ((Interactive) node).body }; ! } else { ! return; ! } ! for (int i = beg; i < suite.length; i++) { ! stmtType stmt = suite[i]; ! if (!(stmt instanceof ImportFrom)) ! break; stmt.from_future_checked = true; ! if (!check((ImportFrom) stmt)) ! break; } if (cflags != null) { cflags.division = cflags.division || division; } *************** *** 81,97 **** ! public static void checkFromFuture(SimpleNode node) throws Exception { ! if (node.from_future_checked) return; ! SimpleNode dotted_name = node.getChild(0); ! if (dotted_name.getNumChildren() == 1 && ! ((String)dotted_name.getChild(0).getInfo()).equals(FUTURE)) { throw new ParseException("from __future__ imports must occur " + "at the beginning of the file",node); } node.from_future_checked = true; - } - - public boolean areNestedScopesOn() { - return nested_scopes; } --- 73,84 ---- ! public static void checkFromFuture(ImportFrom node) throws Exception { ! if (node.from_future_checked) ! return; ! if (node.module.equals(FUTURE)) { throw new ParseException("from __future__ imports must occur " + "at the beginning of the file",node); } node.from_future_checked = true; } Index: Module.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/Module.java,v retrieving revision 2.11 retrieving revision 2.12 diff -C2 -d -r2.11 -r2.12 *** Module.java 13 Jan 2002 18:31:55 -0000 2.11 --- Module.java 30 May 2002 16:13:29 -0000 2.12 *************** *** 6,9 **** --- 6,12 ---- import java.util.*; import org.python.parser.*; + import org.python.parser.ast.*; + import org.python.core.Py; + import org.python.core.PyException; class PyIntegerConstant extends Constant implements ClassConstants *************** *** 255,259 **** } ! public class Module implements ClassConstants { ClassFile classfile; --- 258,262 ---- } ! public class Module implements ClassConstants, CompilationContext { ClassFile classfile; *************** *** 264,267 **** --- 267,271 ---- public boolean setFile=true; Future futures; + Hashtable scopes; public Module(String name, String filename, boolean linenumbers) { *************** *** 277,280 **** --- 281,285 ---- codes = new Vector(); futures = new Future(); + scopes = new Hashtable(); } *************** *** 367,371 **** private int to_cell; ! public PyCodeConstant PyCode(SimpleNode tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, --- 372,376 ---- private int to_cell; ! public PyCodeConstant PyCode(modType tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, *************** *** 378,385 **** ! public PyCodeConstant PyCode(SimpleNode tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, ! int firstlineno, ScopeInfo scope, org.python.core.CompilerFlags cflags) throws Exception --- 383,391 ---- ! public PyCodeConstant PyCode(modType tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, ! int firstlineno, ! ScopeInfo scope, org.python.core.CompilerFlags cflags) throws Exception *************** *** 414,420 **** CodeCompiler compiler = new CodeCompiler(this, printResults); ! if (ac != null && ac.init_code.getNumChildren() > 0) { ! ac.init_code.jjtAddChild(tree, ac.init_code.getNumChildren()); ! tree = ac.init_code; } --- 420,425 ---- CodeCompiler compiler = new CodeCompiler(this, printResults); ! if (ac != null && ac.init_code.size() > 0) { ! ac.appendInitCode((Suite) tree); } *************** *** 439,443 **** compiler.parse(tree, c, fast_locals, className, classBody, ! scope,cflags); // !classdef only --- 444,448 ---- compiler.parse(tree, c, fast_locals, className, classBody, ! scope, cflags); // !classdef only *************** *** 453,459 **** code.moreflags |= org.python.core.PyTableCode.CO_OPTIMIZED; } - if (compiler.my_scope.nested_scopes) { - code.moreflags |= org.python.core.PyTableCode.CO_NESTED; - } code.module = this; --- 458,461 ---- *************** *** 575,579 **** } ! public static void compile(SimpleNode node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, --- 577,607 ---- } ! // Implementation of CompilationContext ! public Future getFutures() { return futures; } ! ! public String getFilename() { return sfilename; } ! ! public ScopeInfo getScopeInfo(SimpleNode node) { ! return (ScopeInfo) scopes.get(node); ! } ! ! public void error(String msg,boolean err,SimpleNode node) ! throws Exception ! { ! if (!err) { ! try { ! Py.warning(Py.SyntaxWarning, msg, ! (sfilename != null) ? sfilename : "?", ! node.beginLine ,null, Py.None); ! return; ! } catch(PyException e) { ! if (!Py.matchException(e, Py.SyntaxWarning)) ! throw e; ! } ! } ! throw new ParseException(msg,node); ! } ! ! public static void compile(modType node, OutputStream ostream, String name, String filename, boolean linenumbers, boolean printResults, *************** *** 585,593 **** module.setFile = setFile; module.futures.preprocessFutures(node, cflags); //Add __doc__ if it exists //Add __file__ for filename (if it exists?) Constant main = module.PyCode(node, "?", false, null, false, ! printResults, 0, null, cflags); module.mainCode = main; module.write(ostream); --- 613,625 ---- module.setFile = setFile; module.futures.preprocessFutures(node, cflags); + new ScopesCompiler(module, module.scopes).parse(node); + //Add __doc__ if it exists //Add __file__ for filename (if it exists?) Constant main = module.PyCode(node, "?", false, null, false, ! printResults, 0, ! module.getScopeInfo(node), ! cflags); module.mainCode = main; module.write(ostream); Index: ScopeInfo.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ScopeInfo.java,v retrieving revision 2.6 retrieving revision 2.7 diff -C2 -d -r2.6 -r2.7 *** ScopeInfo.java 27 Nov 2001 19:07:21 -0000 2.6 --- ScopeInfo.java 30 May 2002 16:13:30 -0000 2.7 *************** *** 12,15 **** --- 12,16 ---- public int level; public int func_level; + public int list_comprehension_count; public void dump() { // for debugging *************** *** 39,44 **** public ScopeInfo(String name, SimpleNode node, int level, int kind, ! int func_level, ArgListCompiler ac, ! boolean nested_scopes) { scope_name = name; scope_node = node; --- 40,44 ---- public ScopeInfo(String name, SimpleNode node, int level, int kind, ! int func_level, ArgListCompiler ac) { scope_name = name; scope_node = node; *************** *** 47,55 **** this.func_level = func_level; this.ac = ac; - this.nested_scopes = nested_scopes; } public int kind; - public boolean nested_scopes; public boolean unqual_exec; --- 47,53 ---- *************** *** 78,81 **** --- 76,80 ---- public void addParam(String name) { + //System.out.println("addParam " + name); tbl.put(name, new SymInfo(PARAM|BOUND,local++)); names.addElement(name); *************** *** 120,124 **** if (up == null) return; // top level => nop - boolean nested_scopes = this.nested_scopes; boolean func = kind == FUNCSCOPE; Vector purecells = new Vector(); --- 119,122 ---- *************** *** 137,152 **** // not func global and bound ? if ((flags&NGLOBAL) == 0 && (flags&BOUND) != 0) { ! if (nested_scopes) { ! info.flags |= CELL; ! if ((info.flags&PARAM) != 0) ! jy_paramcells.addElement(name); ! cellvars.addElement(name); ! info.env_index = cell++; ! if ((flags&PARAM) == 0) purecells.addElement(name); ! continue; ! } ! ctxt.error("local name '"+name+"' in '"+scope_name+ ! "' shadows use as global in nested scopes", ! false, scope_node); } } else { --- 135,145 ---- // not func global and bound ? if ((flags&NGLOBAL) == 0 && (flags&BOUND) != 0) { ! info.flags |= CELL; ! if ((info.flags&PARAM) != 0) ! jy_paramcells.addElement(name); ! cellvars.addElement(name); ! info.env_index = cell++; ! if ((flags&PARAM) == 0) purecells.addElement(name); ! continue; } } else { *************** *** 204,208 **** else why = " because it contains free variables"; ! ctxt.error(illegal + why, nested_scopes, scope_node); } --- 197,201 ---- else why = " because it contains free variables"; ! ctxt.error(illegal + why, true, scope_node); } *************** *** 210,214 **** public void setup_closure(ScopeInfo up) { - if (!nested_scopes) return; int free = cell; // env = cell...,free... Hashtable up_tbl = up.tbl; --- 203,206 ---- Index: ScopesCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ScopesCompiler.java,v retrieving revision 2.6 retrieving revision 2.7 diff -C2 -d -r2.6 -r2.7 *** ScopesCompiler.java 13 Jan 2002 18:31:55 -0000 2.6 --- ScopesCompiler.java 30 May 2002 16:13:30 -0000 2.7 *************** *** 4,7 **** --- 4,8 ---- import org.python.parser.*; + import org.python.parser.ast.*; import java.util.*; *************** *** 10,17 **** private CompilationContext code_compiler; - private boolean nested_scopes = false; - private Stack scopes; private ScopeInfo cur = null; private int mode; --- 11,17 ---- private CompilationContext code_compiler; private Stack scopes; private ScopeInfo cur = null; + private Hashtable nodeScopes; private int mode; *************** *** 24,53 **** private int func_level = 0; ! public ScopesCompiler(CompilationContext code_compiler) { this.code_compiler = code_compiler; scopes = new Stack(); mode = GET; - nested_scopes = code_compiler.getFutures().areNestedScopesOn(); - // System.err.println("nested-scopes: "+nested_scopes); - } - - public Object set(SimpleNode node) throws Exception { - return modal(node,SET); - } - - public Object del(SimpleNode node) throws Exception { - return modal(node,DEL); - } - - - public Object augset(SimpleNode node) throws Exception { - return modal(node,AUGSET); - } - - public Object modal(SimpleNode node, int newmode)throws Exception { - mode = newmode; - node.visit(this); - mode = GET; - return null; } --- 24,34 ---- private int func_level = 0; ! public ScopesCompiler(CompilationContext code_compiler, ! Hashtable nodeScopes) ! { this.code_compiler = code_compiler; + this.nodeScopes = nodeScopes; scopes = new Stack(); mode = GET; } *************** *** 59,64 **** } if (kind == FUNCSCOPE) func_level++; ! node.scope = cur = new ScopeInfo(name, node, level++, kind, ! func_level, ac, nested_scopes); } --- 40,46 ---- } if (kind == FUNCSCOPE) func_level++; ! cur = new ScopeInfo(name, node, level++, kind, ! func_level, ac); ! nodeScopes.put(node, cur); } *************** *** 74,78 **** public void parse(SimpleNode node) throws Exception { try { ! node.visit(this); } catch(Throwable t) { throw org.python.core.parser.fixParseError(null, t, --- 56,60 ---- public void parse(SimpleNode node) throws Exception { try { ! visit(node); } catch(Throwable t) { throw org.python.core.parser.fixParseError(null, t, *************** *** 81,211 **** } ! public Object single_input(SimpleNode node) throws Exception { ! beginScope("<single-top>",TOPSCOPE,node,null); ! suite(node); endScope(); return null; } ! public Object file_input(SimpleNode node) throws Exception { ! beginScope("<file-top>",TOPSCOPE,node,null); ! suite(node); endScope(); return null; } ! public Object eval_input(SimpleNode node) throws Exception { ! beginScope("<eval-top>",TOPSCOPE,node,null); ! return_stmt(node); endScope(); return null; } ! private String def(SimpleNode node) { ! String name = (String)node.getChild(0).getInfo(); cur.addBound(name); - return name; } ! public Object funcdef(SimpleNode node) throws Exception { ! String my_name = def(node); ArgListCompiler ac = new ArgListCompiler(); ! SimpleNode suite; ! if (node.getNumChildren() == 3) { ! suite = node.getChild(2); ! //Parse arguments ! node.getChild(1).visit(ac); ! } else { ! suite = node.getChild(1); ! } ! SimpleNode[] defaults = ac.getDefaults(); int defc = defaults.length; ! for(int i=0; i<defc; i++) { ! defaults[i].visit(this); } ! beginScope(my_name,FUNCSCOPE,node,ac); int n = ac.names.size(); ! for (int i=0; i<n; i++) { cur.addParam((String)ac.names.elementAt(i)); } ! ac.init_code.visit(this); cur.markFromParam(); ! suite.visit(this); endScope(); return null; } ! public Object expr_stmt(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! node.getChild(n-1).visit(this); ! for (int i=0; i<n-1; i++) { ! set(node.getChild(i)); ! } ! return null; ! } ! ! public Object print_ext(SimpleNode node) throws Exception { ! node.getChild(0).visit(this); ! return null; ! } ! public Object print_stmt(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! if ( n > 0 ) { ! for (int i=0; i<n-1; i++) { ! node.getChild(i).visit(this); ! } ! if(node.getChild(n-1).id != PythonGrammarTreeConstants.JJTCOMMA) ! node.getChild(n-1).visit(this); } - return null; - } ! public Object del_stmt(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for (int i=0; i<n; i++) { ! del(node.getChild(i)); } return null; } ! public Object pass_stmt(SimpleNode n) throws Exception { ! return null; ! } ! ! public Object break_stmt(SimpleNode n) throws Exception { ! return null; ! } ! ! public Object continue_stmt(SimpleNode n) throws Exception { ! return null; ! } ! ! public Object return_stmt(SimpleNode node) throws Exception { ! if (node.getNumChildren() == 1) node.getChild(0).visit(this); ! return null; ! } ! ! public void stmt(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for (int i=0; i<n; i++) node.getChild(i).visit(this); ! } ! ! public Object raise_stmt(SimpleNode node) throws Exception { ! stmt(node); ! return null; } ! public Object Import(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for(int i = 0; i < n; i++) { ! SimpleNode imp = node.getChild(i); ! switch(imp.id) { ! case PythonGrammarTreeConstants.JJTDOTTED_NAME: ! cur.addBound((String)imp.getChild(0).getInfo()); ! break; ! case PythonGrammarTreeConstants.JJTDOTTED_AS_NAME: ! cur.addBound((String)imp.getChild(1).getInfo()); ! break; } } --- 63,157 ---- } ! public Object visitInteractive(Interactive node) throws Exception { ! beginScope("<single-top>", TOPSCOPE, node, null); ! visit(node.body); endScope(); return null; } ! public Object visitModule(org.python.parser.ast.Module node) ! throws Exception ! { ! beginScope("<file-top>", TOPSCOPE, node, null); ! suite(node.body); endScope(); return null; } ! public Object visitExpression(Expression node) throws Exception { ! beginScope("<eval-top>", TOPSCOPE, node, null); ! visit(new Return(node.body)); endScope(); return null; } ! private void def(String name) { cur.addBound(name); } ! public Object visitFunctionDef(FunctionDef node) throws Exception { ! def(node.name); ArgListCompiler ac = new ArgListCompiler(); ! ac.visitArgs(node.args); ! ! exprType[] defaults = ac.getDefaults(); int defc = defaults.length; ! for (int i = 0; i < defc; i++) { ! visit(defaults[i]); } ! ! beginScope(node.name, FUNCSCOPE, node, ac); int n = ac.names.size(); ! for (int i = 0; i < n; i++) { cur.addParam((String)ac.names.elementAt(i)); } ! for (int i = 0; i < ac.init_code.size(); i++) { ! visit((stmtType) ac.init_code.elementAt(i)); ! } cur.markFromParam(); ! suite(node.body); endScope(); return null; } ! public Object visitLambda(Lambda node) throws Exception { ! ArgListCompiler ac = new ArgListCompiler(); ! ac.visitArgs(node.args); ! SimpleNode[] defaults = ac.getDefaults(); ! int defc = defaults.length; ! for (int i = 0; i < defc; 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 (int i = 0; i < ac.init_code.size(); i++) + visit((stmtType) ac.init_code.elementAt(i)); + cur.markFromParam(); + visit(node.body); + endScope(); return null; } ! public void suite(stmtType[] stmts) throws Exception { ! int n = stmts.length; ! for (int i = 0; i < n; 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) ! cur.addBound(node.names[i].asname); ! else { ! String name = node.names[i].name; ! if (name.indexOf('.') > 0) ! name = name.substring(0, name.indexOf('.')); ! cur.addBound(name); } } *************** *** 213,242 **** } ! public Object ImportFrom(SimpleNode node) throws Exception { Future.checkFromFuture(node); // future stmt support ! int n = node.getNumChildren(); ! if (n == 1) { cur.from_import_star = true; return null; } ! for (int i = 1; i < n; i++) { ! SimpleNode imp = node.getChild(i); ! switch(imp.id) { ! case PythonGrammarTreeConstants.JJTNAME: ! cur.addBound((String)imp.getInfo()); ! break; ! case PythonGrammarTreeConstants.JJTIMPORT_AS_NAME: ! cur.addBound((String)imp.getChild(1).getInfo()); ! break; ! } } - return null; } ! public Object global_stmt(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for(int i = 0; i < n; i++) { ! String name = (String)node.getChild(i).getInfo(); int prev = cur.addGlobal(name); if (prev >= 0) { --- 159,182 ---- } ! public Object visitImportFrom(ImportFrom node) throws Exception { Future.checkFromFuture(node); // future stmt support ! int n = node.names.length; ! if (n == 0) { cur.from_import_star = true; return null; } ! for (int i = 0; i < n; i++) { ! if (node.names[i].asname != null) ! cur.addBound(node.names[i].asname); ! else ! cur.addBound(node.names[i].name); } return null; } ! public Object visitGlobal(Global node) throws Exception { ! int n = node.names.length; ! for (int i = 0; i < n; i++) { ! String name = node.names[i]; int prev = cur.addGlobal(name); if (prev >= 0) { *************** *** 254,311 **** } ! public Object exec_stmt(SimpleNode node) throws Exception { cur.exec = true; ! int n = node.getNumChildren(); ! if (n == 1) cur.unqual_exec = true; ! for (int i = 0; i < n; i++) node.getChild(i).visit(this); ! return null; ! } ! ! public Object assert_stmt(SimpleNode node) throws Exception { ! stmt(node); ! return null; ! } ! ! public Object if_stmt(SimpleNode node) throws Exception { ! stmt(node); ! return null; ! } ! ! public Object while_stmt(SimpleNode node) throws Exception { ! stmt(node); ! return null; ! } ! ! public Object for_stmt(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! set(node.getChild(0)); ! node.getChild(1).visit(this); ! node.getChild(2).visit(this); ! if (node.getNumChildren()>3) node.getChild(3).visit(this); ! return null; ! } ! ! public Object try_stmt(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for (int i=0; i<n; i++) { ! if (i%2 == 1 && i != n-1) { ! switch(node.getChild(i).getNumChildren()) { ! case 2: ! set(node.getChild(i).getChild(1)); ! case 1: ! node.getChild(i).getChild(0).visit(this); ! } ! continue; ! } ! node.getChild(i).visit(this); ! } ! return null; ! } ! ! public Object suite(SimpleNode node) throws Exception { ! stmt(node); return null; } private static void illassign(SimpleNode node) throws Exception { String target = "operator"; --- 194,206 ---- } ! public Object visitExec(Exec node) throws Exception { cur.exec = true; ! if (node.globals == null && node.locals == null) ! cur.unqual_exec = true; ! traverse(node); return null; } + /* private static void illassign(SimpleNode node) throws Exception { String target = "operator"; *************** *** 317,600 **** throw new ParseException("can't assign to "+target,node); } ! public void binaryop(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! node.getChild(0).visit(this); ! node.getChild(1).visit(this); ! } ! ! public void unaryop(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! node.getChild(0).visit(this); ! } ! ! ! public Object or_boolean(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object and_boolean(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object not_1op(SimpleNode node) throws Exception { ! unaryop(node); ! return null; ! } ! ! public Object comparision(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! int n = node.getNumChildren(); ! for (int i=0; i<n; i++) { ! if (i%2 == 0) node.getChild(i).visit(this); ! } ! return null; ! } ! ! public Object or_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object xor_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object and_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object lshift_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object rshift_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object add_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object sub_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object mul_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object div_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object floordiv_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object mod_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object pos_1op(SimpleNode node) throws Exception { ! unaryop(node); ! return null; ! } ! ! public Object neg_1op(SimpleNode node) throws Exception { ! unaryop(node); ! return null; ! } ! ! public Object invert_1op(SimpleNode node) throws Exception { ! unaryop(node); ! return null; ! } ! ! public Object pow_2op(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object str_1op(SimpleNode node) throws Exception { ! unaryop(node); ! return null; ! } ! ! public Object strjoin(SimpleNode node) throws Exception { ! binaryop(node); ! return null; ! } ! ! public Object Call_Op(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! node.getChild(0).visit(this); ! if (node.getNumChildren()>1) { ! SimpleNode args=node.getChild(1); ! int n = args.getNumChildren(); ! for (int i=0; i<n; i++) { ! SimpleNode arg = args.getChild(i); ! switch(arg.id) { ! case PythonGrammarTreeConstants.JJTKEYWORD: ! arg.getChild(1).visit(this); ! break; ! case PythonGrammarTreeConstants.JJTEXTRAARGVALUELIST: ! case PythonGrammarTreeConstants.JJTEXTRAKEYWORDVALUELIST: ! arg.getChild(0).visit(this); ! break; ! default: ! arg.visit(this); ! } ! } ! } ! return null; ! } ! ! public Object Index_Op(SimpleNode node) throws Exception { ! int prevmode= mode; ! mode = GET; ! node.getChild(0).visit(this); ! node.getChild(1).visit(this); ! mode = prevmode; ! return null; ! } ! ! public Object Dot_Op(SimpleNode node) throws Exception { ! int prevmode = mode; ! mode = GET; ! node.getChild(0).visit(this); ! mode = prevmode; ! return null; ! } ! ! public Object tuple(SimpleNode node) throws Exception { ! if (mode ==AUGSET) { ! throw new ParseException( ! "augmented assign to tuple not possible", node); ! } ! int n = node.getNumChildren(); ! if (n > 0) { ! for (int i=0; i<n-1; i++) node.getChild(i).visit(this); ! if (node.getChild(n-1).id != PythonGrammarTreeConstants.JJTCOMMA) ! node.getChild(n-1).visit(this); ! } ! return null; ! } ! ! public Object fplist(SimpleNode node) throws Exception { ! return list(node); ! } ! ! public Object list(SimpleNode node) throws Exception { ! if (mode ==AUGSET) { ! throw new ParseException( ! "augmented assign to list not possible", node); ! } ! int n = node.getNumChildren(); ! if (n > 0) { ! for (int i=0; i<n-1; i++) node.getChild(i).visit(this); ! if (node.getChild(n-1).id != PythonGrammarTreeConstants.JJTCOMMA) ! node.getChild(n-1).visit(this); ! } ! return null; ! } ! ! public Object list_iter(SimpleNode node) throws Exception { ! if (node.getNumChildren() == 1) node.getChild(0).visit(this); ! return null; ! } ! ! public Object dictionary(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! stmt(node); ! return null; ! } ! ! public Object lambdef(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! ArgListCompiler ac = new ArgListCompiler(); ! SimpleNode expr; ! if (node.getNumChildren() == 2) { ! expr = node.getChild(1); ! //Parse arguments ! node.getChild(0).visit(ac); ! } else { ! expr = node.getChild(0); ! } ! SimpleNode[] defaults = ac.getDefaults(); ! int defc = defaults.length; ! for(int i=0; i<defc; i++) { ! defaults[i].visit(this); ! } ! beginScope("<lambda>",FUNCSCOPE,node,ac); ! int n = ac.names.size(); ! for (int i=0; i<n; i++) { ! cur.addParam((String)ac.names.elementAt(i)); ! } ! ac.init_code.visit(this); ! cur.markFromParam(); ! expr.visit(this); ! endScope(); ! return null; ! } ! ! public Object Ellipses(SimpleNode n) throws Exception { ! return null; ! } ! ! public Object Slice(SimpleNode node) throws Exception { ! int n = node.getNumChildren(); ! for (int i=0; i<n; i++) { ! SimpleNode snode = node.getChild(i); ! if (snode.id != PythonGrammarTreeConstants.JJTCOLON) { ! snode.visit(this); ! } ! } ! return null; ! } ! ! public Object classdef(SimpleNode node) throws Exception { ! String cl_name = def(node); ! int n = node.getNumChildren(); ! SimpleNode suite = node.getChild(n-1); ! for (int i=1; i<n-1; i++) node.getChild(i).visit(this); ! beginScope(cl_name,CLASSSCOPE,node,null); ! suite.visit(this); endScope(); return null; } ! public Object Int(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! return null; ! } ! ! public Object Float(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! return null; ! } ! ! public Object Complex(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! return null; ! } ! ! public Object Name(SimpleNode node) throws Exception { ! String name = (String)node.getInfo(); ! if ( mode != GET) { if (name.equals("__debug__")) ! code_compiler.error("can not assign to __debug__",false,node); cur.addBound(name); } --- 212,233 ---- 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++) ! visit(node.bases[i]); ! beginScope(node.name, CLASSSCOPE, node, null); ! suite(node.body); endScope(); return null; } ! public Object visitName(Name node) throws Exception { ! String name = node.id; ! if (node.ctx != expr_contextType.Load) { if (name.equals("__debug__")) ! code_compiler.error("can not assign to __debug__", true,node); cur.addBound(name); } *************** *** 603,675 **** } ! public Object String(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); ! return null; ! } ! ! public void aug_assign(SimpleNode node) throws Exception { ! augset(node.getChild(0)); ! node.getChild(1).visit(this); ! } ! ! public Object aug_plus(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_minus(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_multiply(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_divide(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_floordivide(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_modulo(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_and(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_or(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_xor(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_lshift(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_rshift(SimpleNode node) throws Exception { ! aug_assign(node); ! return null; ! } ! ! public Object aug_power(SimpleNode node) throws Exception { ! aug_assign(node); return null; } - } --- 236,244 ---- } ! public Object visitListComp(ListComp node) throws Exception { ! String tmp = "_[" + (++cur.list_comprehension_count) + "]"; ! cur.addBound(tmp); ! traverse(node); return null; } } Index: SymInfo.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/SymInfo.java,v retrieving revision 2.2 retrieving revision 2.3 diff -C2 -d -r2.2 -r2.3 *** SymInfo.java 27 Nov 2001 13:51:37 -0000 2.2 --- SymInfo.java 30 May 2002 16:13:30 -0000 2.3 *************** *** 17,19 **** --- 17,24 ---- public int env_index; + + public String toString() { + return "SymInfo[" + flags + " " + locals_index + " " + + env_index + "]"; + } } |