📄 module.java
字号:
ArgListCompiler ac, boolean fast_locals, boolean class_body, int firstlineno) throws Exception { return PyCode(tree, name, ac, fast_locals, class_body, false, firstlineno); } public PyCodeConstant PyCode(SimpleNode tree, String name, ArgListCompiler ac, boolean fast_locals, boolean class_body, boolean printResults) throws Exception { return PyCode(tree, name, ac, fast_locals, class_body, printResults, 0); }*/ Vector codes; private boolean isJavaIdentifier(String s) { char[] chars = s.toCharArray(); if (chars.length == 0) return false; if (!Character.isJavaIdentifierStart(chars[0])) return false; for(int i=1; i<chars.length; i++) { if (!Character.isJavaIdentifierPart(chars[i])) return false; } return true; } private static final String[] emptyStringAr = new String[0]; private String[] toNameAr(Vector names,boolean nullok) { int sz = names.size(); if (sz ==0 && nullok) return null; String[] nameArray = new String[sz]; names.copyInto(nameArray); return nameArray; } private int to_cell; public PyCodeConstant PyCode(modType 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(modType tree, String name, boolean fast_locals, String className, boolean classBody, boolean printResults, int firstlineno, ScopeInfo scope, org.python.core.CompilerFlags cflags) throws Exception { PyCodeConstant code = new PyCodeConstant(); ArgListCompiler ac = (scope != null)?scope.ac:null; if (ac != null) { code.arglist = ac.arglist; code.keywordlist = ac.keywordlist; code.argcount = ac.names.size(); } code.co_name = name; code.co_firstlineno = firstlineno; code.id = codes.size(); //Better names in the future? if (isJavaIdentifier(name)) code.fname = name+"$"+code.id; else code.fname = "f$"+code.id; codes.addElement(code); Code c = classfile.addMethod( code.fname, "(" + $pyFrame + ")" + $pyObj, ClassFile.PUBLIC); CodeCompiler compiler = new CodeCompiler(this, printResults); Label genswitch = c.getLabel(); if (scope.generator) { c.goto_(genswitch); } Label start = c.getLabel(); start.setPosition(); //Do something to add init_code to tree if (ac != null && ac.init_code.size() > 0) { ac.appendInitCode((Suite) tree); } if (scope != null) { int nparamcell = scope.jy_paramcells.size(); if (nparamcell > 0) { if (to_cell == 0) { to_cell = classfile.pool.Methodref("org/python/core/PyFrame", "to_cell","(II)V"); } Hashtable tbl = scope.tbl; Vector paramcells = scope.jy_paramcells; for (int i = 0; i < nparamcell; i++) { c.aload(1); SymInfo syminf = (SymInfo)tbl.get(paramcells.elementAt(i)); c.iconst(syminf.locals_index); c.iconst(syminf.env_index); c.invokevirtual(to_cell); } } } compiler.parse(tree, c, fast_locals, className, classBody, scope, cflags); if (scope.generator) { genswitch.setPosition(); c.aload(1); if (compiler.f_lasti == 0) { compiler.f_lasti = c.pool.Fieldref( "org/python/core/PyFrame", "f_lasti", "I"); } c.getfield(compiler.f_lasti); Label[] yields = new Label[compiler.yields.size()+1]; yields[0] = start; for (int i = 1; i < yields.length; i++) { yields[i] = (Label) compiler.yields.elementAt(i-1); } c.tableswitch(start, 0, yields); // XXX: Generate an error } // !classdef only if (!classBody) code.names = toNameAr(compiler.names,false); if (scope != null) { 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.generator) { code.moreflags |= org.python.core.PyTableCode.CO_GENERATOR; } if (cflags != null && cflags.generator_allowed) { code.moreflags |= org.python.core.PyTableCode.CO_GENERATOR_ALLOWED; } code.module = this; code.name = code.fname; return code; } //This block of code writes out the various standard methods public void addInit() throws IOException { Code c = classfile.addMethod("<init>", "()V", ClassFile.PUBLIC); c.aload(0); c.invokespecial(c.pool.Methodref("org/python/core/PyFunctionTable", "<init>", "()V")); c.return_(); } public void addRunnable() throws IOException { Code c = classfile.addMethod("getMain", "()" + $pyCode, ClassFile.PUBLIC); mainCode.get(c); c.areturn(); } public void addMain() throws IOException { Code c = classfile.addMethod("main", "(" + $str + ")V", ClassFile.PUBLIC | ClassFile.STATIC); int mref_self = c.pool.Fieldref(classfile.name, "self", "L"+classfile.name+";"); c.getstatic(mref_self); c.aload(0); c.invokestatic(c.pool.Methodref( "org/python/core/Py", "do_main", "(" + $pyRunnable + $strArr + ")V")); c.return_(); } public void addConstants() throws IOException { Code c = classfile.addMethod("<clinit>", "()V", ClassFile.STATIC); classfile.addField("self", "L"+classfile.name+";", ClassFile.STATIC|ClassFile.FINAL); c.new_(c.pool.Class(classfile.name)); c.dup(); c.invokespecial(c.pool.Methodref(classfile.name, "<init>", "()V")); c.putstatic(c.pool.Fieldref(classfile.name, "self", "L"+classfile.name+";")); Enumeration e = constants.elements(); while (e.hasMoreElements()) { Constant constant = (Constant)e.nextElement(); constant.put(c); } for(int i=0; i<codes.size(); i++) { PyCodeConstant pyc = (PyCodeConstant)codes.elementAt(i); pyc.put(c); } c.return_(); } public void addFunctions() throws IOException { Code code = classfile.addMethod( "call_function", "(I" + $pyFrame + ")" + $pyObj, ClassFile.PUBLIC); Label def = code.getLabel(); Label[] labels = new Label[codes.size()]; int i; for(i=0; i<labels.length; i++) labels[i] = code.getLabel(); //Get index for function to call code.iload(1); code.tableswitch(def, 0, labels); for(i=0; i<labels.length; i++) { labels[i].setPosition(); code.aload(0); code.aload(2); code.invokevirtual( classfile.name, ((PyCodeConstant)codes.elementAt(i)).fname, "(" + $pyFrame + ")" + $pyObj); code.areturn(); } def.setPosition(); //Should probably throw internal exception here code.aconst_null(); code.areturn(); } public void write(OutputStream stream) throws IOException { addInit(); addRunnable(); //addMain(); addConstants(); addFunctions(); classfile.addInterface("org/python/core/PyRunnable"); if (sfilename != null) { classfile.addAttribute(new SourceFile(sfilename, classfile.pool)); } classfile.addAttribute(new APIVersion(org.python.core.imp.APIVersion, classfile.pool)); classfile.write(stream); } // 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, boolean setFile, org.python.core.CompilerFlags cflags) throws Exception { Module module = new Module(name, filename, linenumbers); 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); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -