From: Finn B. <bc...@us...> - 2002-01-06 21:19:15
|
Update of /cvsroot/jython/jython/org/python/compiler In directory usw-pr-cvs1:/tmp/cvs-serv15999/compiler Modified Files: CodeCompiler.java Log Message: Support for the __iter__ protocol. With this change, all loops over sequences will use the __iter__() method to get a iterator object and all builtin sequence objects will ofcourse define the __iter__() method. Index: CodeCompiler.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/compiler/CodeCompiler.java,v retrieving revision 2.23 retrieving revision 2.24 diff -C2 -d -r2.23 -r2.24 *** CodeCompiler.java 2001/11/27 19:07:21 2.23 --- CodeCompiler.java 2002/01/06 21:19:13 2.24 *************** *** 934,938 **** } ! public int safe_getitem=0; public Object for_stmt(SimpleNode node) throws Exception { --- 934,939 ---- } ! public int iter=0; ! public int iternext=0; public Object for_stmt(SimpleNode node) throws Exception { *************** *** 944,948 **** int list_tmp = code.getLocal(); ! int index_tmp = code.getLocal(); int expr_tmp = code.getLocal(); --- 945,949 ---- int list_tmp = code.getLocal(); ! int iter_tmp = code.getLocal(); int expr_tmp = code.getLocal(); *************** *** 953,959 **** code.astore(list_tmp); ! //set up the loop counter ! code.iconst(0); ! code.istore(index_tmp); //do check at end of loop. Saves one opcode ;-) --- 954,967 ---- code.astore(list_tmp); ! //set up the loop iterator ! ! code.aload(list_tmp); ! if (mrefs.iter == 0) { ! mrefs.iter = code.pool.Methodref( ! "org/python/core/PyObject", ! "__iter__", "()" + $pyObj); ! } ! code.invokevirtual(mrefs.iter); ! code.astore(iter_tmp); //do check at end of loop. Saves one opcode ;-) *************** *** 961,965 **** start_loop.setPosition(); ! //set index variable to current entry in list set(node.getChild(0), expr_tmp); --- 969,973 ---- start_loop.setPosition(); ! //set iter variable to current entry in list set(node.getChild(0), expr_tmp); *************** *** 968,985 **** continue_loop.setPosition(); - //increment counter - code.iinc(index_tmp, 1); next_loop.setPosition(); setline(node); //get the next element from the list ! code.aload(list_tmp); ! code.iload(index_tmp); ! if (mrefs.safe_getitem == 0) { ! mrefs.safe_getitem = code.pool.Methodref( "org/python/core/PyObject", ! "__finditem__", "(I)" + $pyObj); } ! code.invokevirtual(mrefs.safe_getitem); code.astore(expr_tmp); code.aload(expr_tmp); --- 976,990 ---- continue_loop.setPosition(); next_loop.setPosition(); setline(node); //get the next element from the list ! code.aload(iter_tmp); ! if (mrefs.iternext == 0) { ! mrefs.iternext = code.pool.Methodref( "org/python/core/PyObject", ! "__iternext__", "()" + $pyObj); } ! code.invokevirtual(mrefs.iternext); code.astore(expr_tmp); code.aload(expr_tmp); *************** *** 997,1001 **** code.freeLocal(list_tmp); ! code.freeLocal(index_tmp); code.freeLocal(expr_tmp); --- 1002,1006 ---- code.freeLocal(list_tmp); ! code.freeLocal(iter_tmp); code.freeLocal(expr_tmp); |