From: Samuele P. <ped...@us...> - 2001-05-27 18:49:18
|
Update of /cvsroot/jython/jython/org/python/compiler In directory usw-pr-cvs1:/tmp/cvs-serv24200/compiler Modified Files: CodeCompiler.java Future.java Module.java ScopeInfo.java ScopesCompiler.java Removed Files: CompilerFlags.java Log Message: ** nested scopes (2) ** completes nested scopes support in interpreter: now nested scopes semantic settings are inherited trough exec, execfile, etc ... plus: assert now correctly get __debug__ as a global Index: CodeCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/CodeCompiler.java,v retrieving revision 2.14 retrieving revision 2.15 diff -C2 -r2.14 -r2.15 *** CodeCompiler.java 2001/05/15 18:53:34 2.14 --- CodeCompiler.java 2001/05/27 18:49:15 2.15 *************** *** 7,10 **** --- 7,11 ---- import org.python.core.PyObject; import org.python.core.PyException; + import org.python.core.CompilerFlags; import java.io.IOException; import java.util.Stack; *************** *** 186,190 **** public void parse(SimpleNode node, Code code, boolean fast_locals, String className, ! boolean classBody, ScopeInfo scope) throws Exception { --- 187,191 ---- public void parse(SimpleNode node, Code code, boolean fast_locals, String className, ! boolean classBody, ScopeInfo scope,CompilerFlags cflags) throws Exception { *************** *** 195,199 **** if (scope == null) { futures = new Future(); ! futures.preprocessFutures(node,null); new ScopesCompiler(this).parse(node); scope = node.scope; --- 196,200 ---- if (scope == null) { futures = new Future(); ! futures.preprocessFutures(node,cflags); new ScopesCompiler(this).parse(node); scope = node.scope; *************** *** 782,794 **** public int assert1, assert2; ! public Object assert_stmt(SimpleNode node) throws Exception { // ?? pending __debug__ should be treated as temp global setline(node); Label end_of_assert = code.getLabel(); /* First do an if __debug__: */ ! SimpleNode debugName = new SimpleNode(PythonGrammarTreeConstants.JJTNAME); ! debugName.setInfo("__debug__"); ! debugName.visit(this); ! if (mrefs.nonzero == 0) { mrefs.nonzero = code.pool.Methodref("org/python/core/PyObject", --- 783,794 ---- public int assert1, assert2; ! public Object assert_stmt(SimpleNode node) throws Exception { setline(node); Label end_of_assert = code.getLabel(); /* First do an if __debug__: */ ! loadFrame(); ! emitGetGlobal("__debug__"); ! if (mrefs.nonzero == 0) { mrefs.nonzero = code.pool.Methodref("org/python/core/PyObject", *************** *** 2253,2256 **** --- 2253,2266 ---- int getderef,setderef; + void emitGetGlobal(String name) throws Exception { + code.ldc(name); + if (mrefs.getglobal == 0) { + mrefs.getglobal = code.pool.Methodref( + "org/python/core/PyFrame", "getglobal", + "(Ljava/lang/String;)Lorg/python/core/PyObject;"); + } + code.invokevirtual(mrefs.getglobal); + } + public Object Name(SimpleNode node) throws Exception { String name; *************** *** 2275,2285 **** if ((flags&ScopeInfo.GLOBAL) !=0 || optimizeGlobals&&(flags&(ScopeInfo.BOUND|ScopeInfo.CELL|ScopeInfo.FREE))==0) { ! code.ldc(name); ! if (mrefs.getglobal == 0) { ! mrefs.getglobal = code.pool.Methodref( ! "org/python/core/PyFrame", "getglobal", ! "(Ljava/lang/String;)Lorg/python/core/PyObject;"); ! } ! code.invokevirtual(mrefs.getglobal); return null; } --- 2285,2289 ---- if ((flags&ScopeInfo.GLOBAL) !=0 || optimizeGlobals&&(flags&(ScopeInfo.BOUND|ScopeInfo.CELL|ScopeInfo.FREE))==0) { ! emitGetGlobal(name); return null; } Index: Future.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/Future.java,v retrieving revision 2.2 retrieving revision 2.3 diff -C2 -r2.2 -r2.3 *** Future.java 2001/03/10 02:13:48 2.2 --- Future.java 2001/05/27 18:49:15 2.3 *************** *** 41,45 **** } ! public void preprocessFutures(SimpleNode node,CompilerFlags cflags) throws Exception { if (cflags != null) { nested_scopes = cflags.nested_scopes; --- 41,45 ---- } ! public void preprocessFutures(SimpleNode node,org.python.core.CompilerFlags cflags) throws Exception { if (cflags != null) { nested_scopes = cflags.nested_scopes; Index: Module.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/Module.java,v retrieving revision 2.6 retrieving revision 2.7 diff -C2 -r2.6 -r2.7 *** Module.java 2001/05/15 18:53:34 2.6 --- Module.java 2001/05/27 18:49:15 2.7 *************** *** 197,201 **** public String[] cellvars; public String[] freevars; ! public int xxx_npurecell; public int moreflags; --- 197,201 ---- public String[] cellvars; public String[] freevars; ! public int jy_npurecell; public int moreflags; *************** *** 237,241 **** if (freevars != null) CodeCompiler.makeStrings(c, freevars, freevars.length); else c.aconst_null(); ! c.iconst(xxx_npurecell); c.iconst(moreflags); --- 237,241 ---- if (freevars != null) CodeCompiler.makeStrings(c, freevars, freevars.length); else c.aconst_null(); ! c.iconst(jy_npurecell); c.iconst(moreflags); *************** *** 360,368 **** private int to_cell; public PyCodeConstant PyCode(SimpleNode tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, ! int firstlineno, ScopeInfo scope) throws Exception { --- 360,378 ---- private int to_cell; + + public PyCodeConstant PyCode(SimpleNode tree, String name, + boolean fast_locals, String className, + boolean classBody, boolean printResults, + int firstlineno, ScopeInfo scope) + throws Exception + { + return PyCode(tree,name,fast_locals,className,classBody,printResults,firstlineno,scope,null); + } + 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 { *************** *** 402,406 **** if (scope != null) { ! int nparamcell = scope.xxx_paramcells.size(); if (nparamcell > 0) { if (to_cell == 0) { --- 412,416 ---- if (scope != null) { ! int nparamcell = scope.jy_paramcells.size(); if (nparamcell > 0) { if (to_cell == 0) { *************** *** 408,412 **** } Hashtable tbl = scope.tbl; ! Vector paramcells = scope.xxx_paramcells; for (int i = 0; i < nparamcell; i++) { c.aload(1); --- 418,422 ---- } Hashtable tbl = scope.tbl; ! Vector paramcells = scope.jy_paramcells; for (int i = 0; i < nparamcell; i++) { c.aload(1); *************** *** 419,423 **** } ! compiler.parse(tree, c, fast_locals, className, classBody, scope); // !classdef only --- 429,433 ---- } ! compiler.parse(tree, c, fast_locals, className, classBody, scope,cflags); // !classdef only *************** *** 427,436 **** code.cellvars = toNameAr(scope.cellvars,true); code.freevars = toNameAr(scope.freevars,true); ! code.xxx_npurecell = scope.xxx_npurecell; ! if (compiler.optimizeGlobals) { ! code.moreflags |= org.python.core.PyTableCode.CO_OPTIMIZED; ! } } ! code.module = this; code.name = code.fname; --- 437,450 ---- code.cellvars = toNameAr(scope.cellvars,true); code.freevars = toNameAr(scope.freevars,true); ! code.jy_npurecell = scope.jy_npurecell; } ! ! if (compiler.optimizeGlobals) { ! 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; code.name = code.fname; *************** *** 554,558 **** String name, String filename, boolean linenumbers, boolean printResults, ! boolean setFile) throws Exception { --- 568,572 ---- String name, String filename, boolean linenumbers, boolean printResults, ! boolean setFile,org.python.core.CompilerFlags cflags) throws Exception { *************** *** 562,566 **** //Add __file__ for filename (if it exists?) ! Constant main = module.PyCode(node, "?",false, null, false, printResults, 0,null); module.mainCode = main; module.write(ostream); --- 576,580 ---- //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); Index: ScopeInfo.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ScopeInfo.java,v retrieving revision 2.3 retrieving revision 2.4 diff -C2 -r2.3 -r2.4 *** ScopeInfo.java 2001/05/15 18:53:34 2.3 --- ScopeInfo.java 2001/05/27 18:49:15 2.4 *************** *** 104,110 **** public Vector cellvars = new Vector(); ! public Vector xxx_paramcells = new Vector(); ! public int xxx_npurecell; // ?? xxx public int cell; --- 104,110 ---- public Vector cellvars = new Vector(); ! public Vector jy_paramcells = new Vector(); ! public int jy_npurecell; public int cell; *************** *** 131,135 **** if (nested_scopes) { info.flags |= CELL; ! if ((info.flags&PARAM) != 0) xxx_paramcells.addElement(name); cellvars.addElement(name); info.env_index = cell++; --- 131,135 ---- if (nested_scopes) { info.flags |= CELL; ! if ((info.flags&PARAM) != 0) jy_paramcells.addElement(name); cellvars.addElement(name); info.env_index = cell++; *************** *** 163,167 **** } } ! if ((xxx_npurecell = purecells.size()) > 0) { int sz = purecells.size(); for (int i = 0; i < sz; i++) { --- 163,167 ---- } } ! if ((jy_npurecell = purecells.size()) > 0) { int sz = purecells.size(); for (int i = 0; i < sz; i++) { Index: ScopesCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/ScopesCompiler.java,v retrieving revision 2.3 retrieving revision 2.4 diff -C2 -r2.3 -r2.4 *** ScopesCompiler.java 2001/05/15 18:53:34 2.3 --- ScopesCompiler.java 2001/05/27 18:49:15 2.4 *************** *** 9,15 **** private CompilationContext code_compiler; ! private boolean nested_scopes = false; ! private Stack scopes; private ScopeInfo cur = null; --- 9,15 ---- private CompilationContext code_compiler; ! private boolean nested_scopes = false; ! private Stack scopes; private ScopeInfo cur = null; *************** *** 24,35 **** private int func_level = 0; ! public ScopesCompiler(CompilationContext code_compiler) { // ?? polish this.code_compiler = code_compiler; scopes = new Stack(); mode = GET; ! String nested_scopes_opt = org.python.core.PySystemState.registry.getProperty("python.xfutures.nested_scopes"); ! if (nested_scopes_opt != null && nested_scopes_opt.equals("on")) nested_scopes = true; else nested_scopes = code_compiler.getFutures().areNestedScopesOn(); ! // System.err.println("nested-scopes: "+nested_scopes); } --- 24,35 ---- private int func_level = 0; ! public ScopesCompiler(CompilationContext code_compiler) { this.code_compiler = code_compiler; scopes = new Stack(); mode = GET; ! /*String nested_scopes_opt = org.python.core.PySystemState.registry.getProperty("python.xfutures.nested_scopes"); ! if (nested_scopes_opt != null && nested_scopes_opt.equals("on")) nested_scopes = true; else*/ nested_scopes = code_compiler.getFutures().areNestedScopesOn(); ! // System.err.println("nested-scopes: "+nested_scopes); } *************** *** 272,276 **** public Object for_stmt(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); set(node.getChild(0)); node.getChild(1).visit(this); --- 272,276 ---- public Object for_stmt(SimpleNode node) throws Exception { ! if (mode != GET) illassign(node); set(node.getChild(0)); node.getChild(1).visit(this); *************** *** 581,585 **** public Object Name(SimpleNode node) throws Exception { String name = (String)node.getInfo(); ! if ( mode != GET) cur.addBound(name); else cur.addUsed(name); return null; --- 581,588 ---- 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); ! } else cur.addUsed(name); return null; --- CompilerFlags.java DELETED --- |