From: <fwi...@us...> - 2008-11-17 21:48:55
|
Revision: 5586 http://jython.svn.sourceforge.net/jython/?rev=5586&view=rev Author: fwierzbicki Date: 2008-11-17 21:48:50 +0000 (Mon, 17 Nov 2008) Log Message: ----------- First pass at converting all arrays in ast/* to Lists. Checking in without running asdl_antlr.py for clarity of changes. Modified Paths: -------------- branches/astwrite/ast/asdl_antlr.py branches/astwrite/grammar/Python.g branches/astwrite/src/org/python/antlr/GrammarActions.java branches/astwrite/src/org/python/compiler/ArgListCompiler.java branches/astwrite/src/org/python/compiler/CodeCompiler.java branches/astwrite/src/org/python/compiler/Future.java branches/astwrite/src/org/python/compiler/Module.java branches/astwrite/src/org/python/compiler/ProxyMaker.java branches/astwrite/src/org/python/compiler/ScopesCompiler.java Modified: branches/astwrite/ast/asdl_antlr.py =================================================================== --- branches/astwrite/ast/asdl_antlr.py 2008-11-17 21:20:25 UTC (rev 5585) +++ branches/astwrite/ast/asdl_antlr.py 2008-11-17 21:48:50 UTC (rev 5586) @@ -292,16 +292,16 @@ for f in fields: self.emit("this.%s = %s;" % (f.name, f.name), depth+1) fparg = self.fieldDef(f) - #if field.typedef and field.typedef.simple: + not_simple = True if f.typedef is not None and f.typedef.simple: not_simple = False #For now ignoring String -- will want to revisit - if not_simple and not fparg.startswith("String"): + if not_simple and fparg.find("String") == -1: if f.seq: self.emit("if (%s != null) {" % f.name, depth+1); - self.emit("for(int i%(name)s=0;i%(name)s<%(name)s.length;i%(name)s++) {" % {"name":f.name}, depth+2) - self.emit("addChild(%s[i%s]);" % (f.name, f.name), depth+3) + self.emit("for(PythonTree t : %(name)s) {" % {"name":f.name}, depth+2) + self.emit("addChild(t);", depth+3) self.emit("}", depth+2) self.emit("}", depth+1) elif str(f.type) == "expr": @@ -386,10 +386,10 @@ continue if f.seq: self.emit('if (%s != null) {' % f.name, depth+1) - self.emit('for (int i = 0; i < %s.length; i++) {' % f.name, + self.emit('for (PythonTree t : %s) {' % f.name, depth+2) - self.emit('if (%s[i] != null)' % f.name, depth+3) - self.emit('%s[i].accept(visitor);' % f.name, depth+4) + self.emit('if (t != null)', depth+3) + self.emit('t.accept(visitor);', depth+4) self.emit('}', depth+2) self.emit('}', depth+1) else: @@ -415,8 +415,9 @@ jtype = str(field.type) jtype = self.bltinnames.get(jtype, jtype + 'Type') name = field.name - seq = field.seq and "[]" or "" - return "%(jtype)s%(seq)s %(name)s" % locals() + if field.seq: + return "java.util.List<%(jtype)s> %(name)s" % locals() + return "%(jtype)s %(name)s" % locals() class VisitorVisitor(EmitVisitor): def __init__(self, dir): Modified: branches/astwrite/grammar/Python.g =================================================================== --- branches/astwrite/grammar/Python.g 2008-11-17 21:20:25 UTC (rev 5585) +++ branches/astwrite/grammar/Python.g 2008-11-17 21:48:50 UTC (rev 5586) @@ -277,7 +277,7 @@ $single_input.tree = mtype; } : NEWLINE* EOF { - mtype = new Interactive($single_input.start, new stmtType[0]); + mtype = new Interactive($single_input.start, new ArrayList<stmtType>()); } | simple_stmt NEWLINE* EOF { mtype = new Interactive($single_input.start, actions.castStmts($simple_stmt.stypes)); @@ -424,7 +424,7 @@ parameters returns [argumentsType args] : LPAREN (varargslist {$args = $varargslist.args;} - | { $args = new argumentsType($parameters.start, new exprType[0], null, null, new exprType[0]); + | { $args = new argumentsType($parameters.start, new ArrayList<exprType>(), null, null, new ArrayList<exprType>()); } ) RPAREN @@ -597,7 +597,7 @@ | RIGHTSHIFT t2=printlist2 -> ^(PRINT<Print>[$PRINT, actions.castExpr($t2.elts.get(0)), actions.castExprs($t2.elts, 1), $t2.newline]) | - -> ^(PRINT<Print>[$PRINT, null, new exprType[0\], false]) + -> ^(PRINT<Print>[$PRINT, null, new ArrayList<exprType>(), false]) ) ; @@ -730,10 +730,10 @@ ; //import_as_names: import_as_name (',' import_as_name)* [','] -import_as_names returns [aliasType[\] atypes] +import_as_names returns [List<aliasType> atypes] : n+=import_as_name (COMMA! n+=import_as_name)* { - $atypes = (aliasType[])$n.toArray(new aliasType[$n.size()]); + $atypes = $n; } ; @@ -762,10 +762,10 @@ ; //dotted_as_names: dotted_as_name (',' dotted_as_name)* -dotted_as_names returns [aliasType[\] atypes] +dotted_as_names returns [List<aliasType> atypes] : d+=dotted_as_name (COMMA! d+=dotted_as_name)* { - $atypes = (aliasType[])$d.toArray(new aliasType[$d.size()]); + $atypes = $d; } ; @@ -828,7 +828,7 @@ (e2=elif_clause[$iftest] -> ^(ELIF<If>[$iftest, actions.castExpr($test.tree), actions.castStmts($suite.stypes), actions.makeElse($e2.stypes, $e2.tree)]) | - -> ^(ELIF<If>[$iftest, actions.castExpr($test.tree), actions.castStmts($suite.stypes), new stmtType[0\]]) + -> ^(ELIF<If>[$iftest, actions.castExpr($test.tree), actions.castStmts($suite.stypes), new ArrayList<stmtType>()]) ) ; @@ -1223,14 +1223,14 @@ | testlist_gexp -> testlist_gexp | - -> ^(LPAREN<Tuple>[$LPAREN, new exprType[0\], $expr::ctype]) + -> ^(LPAREN<Tuple>[$LPAREN, new ArrayList<exprType>(), $expr::ctype]) ) RPAREN | LBRACK (listmaker[$LBRACK] -> listmaker | - -> ^(LBRACK<org.python.antlr.ast.List>[$LBRACK, new exprType[0\], $expr::ctype]) + -> ^(LBRACK<org.python.antlr.ast.List>[$LBRACK, new ArrayList<exprType>(), $expr::ctype]) ) RBRACK | LCURLY @@ -1238,7 +1238,7 @@ -> ^(LCURLY<Dict>[$LCURLY, actions.castExprs($dictmaker.keys), actions.castExprs($dictmaker.values)]) | - -> ^(LCURLY<Dict>[$LCURLY, new exprType[0\], new exprType[0\]]) + -> ^(LCURLY<Dict>[$LCURLY, new ArrayList<exprType>(), new ArrayList<exprType>()]) ) RCURLY | lb=BACKQUOTE testlist[expr_contextType.Load] rb=BACKQUOTE @@ -1270,8 +1270,7 @@ (list_for[gens] { Collections.reverse(gens); - comprehensionType[] c = - (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); + List<comprehensionType> c = gens; etype = new ListComp($listmaker.start, actions.castExpr($t.get(0)), c); } | (options {greedy=true;}:COMMA t+=test[$expr::ctype])* @@ -1301,7 +1300,7 @@ | (gen_for[gens] { Collections.reverse(gens); - comprehensionType[] c = (comprehensionType[])gens.toArray(new comprehensionType[gens.size()]); + List<comprehensionType> c = gens; exprType e = actions.castExpr($t.get(0)); if (e instanceof Context) { ((Context)e).setContext(expr_contextType.Load); @@ -1324,7 +1323,7 @@ { argumentsType a = $varargslist.args; if (a == null) { - a = new argumentsType($LAMBDA, new exprType[0], null, null, new exprType[0]); + a = new argumentsType($LAMBDA, new ArrayList<exprType>(), null, null, new ArrayList<exprType>()); } etype = new Lambda($LAMBDA, a, actions.castExpr($test.tree)); } @@ -1337,7 +1336,7 @@ -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), actions.castExprs($arglist.args), actions.makeKeywords($arglist.keywords), $arglist.starargs, $arglist.kwargs]) | - -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), new exprType[0\], new keywordType[0\], null, null]) + -> ^(LPAREN<Call>[$begin, actions.castExpr($tree), new ArrayList<exprType>(), new ArrayList<keywordType>(), null, null]) ) RPAREN | LBRACK subscriptlist[$begin] RBRACK @@ -1405,7 +1404,7 @@ //not in CPython's Grammar file //Needed as an exprlist that does not produce tuples for del_stmt. -del_list returns [exprType[\] etypes] +del_list returns [List<exprType> etypes] : e+=expr[expr_contextType.Del] (options {k=2;}: COMMA e+=expr[expr_contextType.Del])* (COMMA)? { $etypes = actions.makeDeleteList($e); @@ -1490,7 +1489,10 @@ : t1=test[expr_contextType.Load] ((ASSIGN t2=test[expr_contextType.Load]) { - $kws.add(new exprType[]{actions.castExpr($t1.tree), actions.castExpr($t2.tree)}); + List<exprType> exprs = new ArrayList<exprType>(); + exprs.add(actions.castExpr($t1.tree)); + exprs.add(actions.castExpr($t2.tree)); + $kws.add(exprs); } | gen_for[$gens] { @@ -1499,7 +1501,7 @@ } $genarg = true; Collections.reverse($gens); - comprehensionType[] c = (comprehensionType[])$gens.toArray(new comprehensionType[$gens.size()]); + List<comprehensionType> c = $gens; arguments.add(new GeneratorExp($t1.start, actions.castExpr($t1.tree), c)); } | { @@ -1523,11 +1525,9 @@ list_for [List gens] : FOR exprlist[expr_contextType.Store] IN testlist[expr_contextType.Load] (list_iter[gens])? { - exprType[] e; + List<exprType> e = new ArrayList<exprType>(); if ($list_iter.etype != null) { - e = new exprType[]{$list_iter.etype}; - } else { - e = new exprType[0]; + e.add($list_iter.etype); } gens.add(new comprehensionType($FOR, $exprlist.etype, actions.castExpr($testlist.tree), e)); } @@ -1554,11 +1554,9 @@ gen_for [List gens] : FOR exprlist[expr_contextType.Store] IN or_test[expr_contextType.Load] gen_iter[gens]? { - exprType[] e; + List<exprType> e = new ArrayList<exprType>(); if ($gen_iter.etype != null) { - e = new exprType[]{$gen_iter.etype}; - } else { - e = new exprType[0]; + e.add($gen_iter.etype); } gens.add(new comprehensionType($FOR, $exprlist.etype, actions.castExpr($or_test.tree), e)); } Modified: branches/astwrite/src/org/python/antlr/GrammarActions.java =================================================================== --- branches/astwrite/src/org/python/antlr/GrammarActions.java 2008-11-17 21:20:25 UTC (rev 5585) +++ branches/astwrite/src/org/python/antlr/GrammarActions.java 2008-11-17 21:48:50 UTC (rev 5586) @@ -118,33 +118,36 @@ return lev.size(); } - aliasType[] makeStarAlias(Token t) { - return new aliasType[]{new aliasType(t, "*", null)}; + List<aliasType> makeStarAlias(Token t) { + List<aliasType> result = new ArrayList<aliasType>(); + result.add(new aliasType(t, "*", null)); + return result; } - aliasType[] makeAliases(aliasType[] atypes) { + List<aliasType> makeAliases(List<aliasType> atypes) { if (atypes == null) { - return new aliasType[0]; + return new ArrayList<aliasType>(); } return atypes; } - exprType[] makeBases(exprType etype) { + List<exprType> makeBases(exprType etype) { + List<exprType> result = new ArrayList<exprType>(); if (etype != null) { if (etype instanceof Tuple) { return ((Tuple)etype).elts; } - return new exprType[]{etype}; + result.add(etype); } - return new exprType[0]; + return result; } - String[] makeNames(List names) { + List<String> makeNames(List names) { List<String> s = new ArrayList<String>(); for(int i=0;i<names.size();i++) { s.add(((Token)names.get(i)).getText()); } - return s.toArray(new String[s.size()]); + return s; } void errorGenExpNotSoleArg(PythonTree t) { @@ -162,13 +165,13 @@ } - exprType[] castExprs(List exprs) { + List<exprType> castExprs(List exprs) { return castExprs(exprs, 0); } - exprType[] castExprs(List exprs, int start) { + List<exprType> castExprs(List exprs, int start) { + List<exprType> result = new ArrayList<exprType>(); if (exprs != null) { - List<exprType> result = new ArrayList<exprType>(); for (int i=start; i<exprs.size(); i++) { Object o = exprs.get(i); if (o instanceof exprType) { @@ -177,18 +180,19 @@ result.add((exprType)((PythonParser.test_return)o).tree); } } - return result.toArray(new exprType[result.size()]); } - return new exprType[0]; + return result; } - stmtType[] makeElse(List elseSuite, PythonTree elif) { + List<stmtType> makeElse(List elseSuite, PythonTree elif) { if (elseSuite != null) { return castStmts(elseSuite); } else if (elif == null) { - return new stmtType[0]; + return new ArrayList<stmtType>(); } - return new stmtType[]{(stmtType)elif}; + List <stmtType> s = new ArrayList<stmtType>(); + s.add((stmtType)elif); + return s; } stmtType castStmt(Object o) { @@ -202,19 +206,22 @@ return null; } - stmtType[] castStmts(PythonTree t) { - return new stmtType[]{(stmtType)t}; + List<stmtType> castStmts(PythonTree t) { + stmtType s = (stmtType)t; + List<stmtType> stmts = new ArrayList<stmtType>(); + stmts.add(s); + return stmts; } - stmtType[] castStmts(List stmts) { + List<stmtType> castStmts(List stmts) { if (stmts != null) { List<stmtType> result = new ArrayList<stmtType>(); for (Object o:stmts) { result.add(castStmt(o)); } - return (stmtType[])result.toArray(new stmtType[result.size()]); + return result; } - return new stmtType[0]; + return new ArrayList<stmtType>(); } exprType makeDottedAttr(Token nameToken, List attrs) { @@ -231,8 +238,8 @@ if (test == null) { return errorHandler.errorStmt(new PythonTree(t)); } - stmtType[] o = castStmts(orelse); - stmtType[] b = castStmts(body); + List<stmtType> o = castStmts(orelse); + List<stmtType> b = castStmts(body); return new While(t, test, b, o); } @@ -242,27 +249,28 @@ } cantBeNone(target); - stmtType[] o = castStmts(orelse); - stmtType[] b = castStmts(body); + List<stmtType> o = castStmts(orelse); + List<stmtType> b = castStmts(body); return new For(t, target, iter, b, o); } stmtType makeTryExcept(Token t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = castStmts(body); - excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o = castStmts(orelse); + List<stmtType> b = castStmts(body); + List<excepthandlerType> e = handlers; + List<stmtType> o = castStmts(orelse); stmtType te = new TryExcept(t, b, e, o); if (finBody == null) { return te; } - stmtType[] f = castStmts(finBody); - stmtType[] mainBody = new stmtType[]{te}; + List<stmtType> f = castStmts(finBody); + List<stmtType> mainBody = new ArrayList<stmtType>(); + mainBody.add(te); return new TryFinally(t, mainBody, f); } TryFinally makeTryFinally(Token t, List body, List finBody) { - stmtType[] b = castStmts(body); - stmtType[] f = castStmts(finBody); + List<stmtType> b = castStmts(body); + List<stmtType> f = castStmts(finBody); return new TryFinally(t, b, f); } @@ -275,21 +283,21 @@ if (args != null) { a = args; } else { - a = new argumentsType(t, new exprType[0], null, null, new exprType[0]); + a = new argumentsType(t, new ArrayList<exprType>(), null, null, new ArrayList<exprType>()); } - stmtType[] s = castStmts(funcStatements); - exprType[] d = castExprs(decorators); + List<stmtType> s = castStmts(funcStatements); + List<exprType> d = castExprs(decorators); return new FunctionDef(t, nameToken.getText(), a, s, d); } - exprType[] makeAssignTargets(exprType lhs, List rhs) { - exprType[] e = new exprType[rhs.size()]; + List<exprType> makeAssignTargets(exprType lhs, List rhs) { + List<exprType> e = new ArrayList<exprType>(); checkAssign(lhs); - e[0] = lhs; + e.add(lhs); for(int i=0;i<rhs.size() - 1;i++) { exprType r = castExpr(rhs.get(i)); checkAssign(r); - e[i + 1] = r; + e.add(r); } return e; } @@ -320,8 +328,8 @@ argumentsType makeArgumentsType(Token t, List params, Token snameToken, Token knameToken, List defaults) { - exprType[] p = castExprs(params); - exprType[] d = castExprs(defaults); + List<exprType> p = castExprs(params); + List<exprType> d = castExprs(defaults); String s; String k; if (snameToken == null) { @@ -337,26 +345,25 @@ return new argumentsType(t, p, s, k, d); } - exprType[] extractArgs(List args) { + List<exprType> extractArgs(List args) { return castExprs(args); } - keywordType[] makeKeywords(List args) { + List<keywordType> makeKeywords(List args) { List<keywordType> k = new ArrayList<keywordType>(); if (args != null) { for(int i=0;i<args.size();i++) { - exprType[] e = (exprType[])args.get(i); - checkAssign(e[0]); - if (e[0] instanceof Name) { - Name arg = (Name)e[0]; - k.add(new keywordType(arg, arg.id, e[1])); + List e = (List)args.get(i); + checkAssign(castExpr(e.get(0))); + if (e.get(0) instanceof Name) { + Name arg = (Name)e.get(0); + k.add(new keywordType(arg, arg.id, castExpr(e.get(1)))); } else { - errorHandler.error("keyword must be a name", e[0]); + errorHandler.error("keyword must be a name", (PythonTree)e.get(0)); } } - return k.toArray(new keywordType[k.size()]); } - return new keywordType[0]; + return k; } Object makeFloat(Token t) { @@ -488,7 +495,7 @@ //FROM Walker: modType makeMod(PythonTree t, List stmts) { - stmtType[] s = castStmts(stmts); + List<stmtType> s = castStmts(stmts); return new Module(t, s); } @@ -497,7 +504,7 @@ } modType makeInteractive(PythonTree t, List stmts) { - stmtType[] s = castStmts(stmts); + List<stmtType> s = castStmts(stmts); return new Interactive(t, s); } @@ -506,29 +513,30 @@ return errorHandler.errorStmt(t); } cantBeNone(nameToken); - exprType[] b = castExprs(bases); - stmtType[] s = castStmts(body); - exprType[] d = castExprs(decorators); + List<exprType> b = castExprs(bases); + List<stmtType> s = castStmts(body); + List<exprType> d = castExprs(decorators); return new ClassDef(t, nameToken.getText(), b, s, d); } stmtType makeTryExcept(PythonTree t, List body, List handlers, List orelse, List finBody) { - stmtType[] b = castStmts(body); - excepthandlerType[] e = (excepthandlerType[])handlers.toArray(new excepthandlerType[handlers.size()]); - stmtType[] o = castStmts(orelse); + List<stmtType> b = castStmts(body); + List<excepthandlerType> e = handlers; + List<stmtType> o = castStmts(orelse); stmtType te = new TryExcept(t, b, e, o); if (finBody == null) { return te; } - stmtType[] f = castStmts(finBody); - stmtType[] mainBody = new stmtType[]{te}; + List<stmtType> f = castStmts(finBody); + List<stmtType> mainBody = new ArrayList<stmtType>(); + mainBody.add(te); return new TryFinally(t, mainBody, f); } TryFinally makeTryFinally(PythonTree t, List body, List finBody) { - stmtType[] b = castStmts(body); - stmtType[] f = castStmts(finBody); + List<stmtType> b = castStmts(body); + List<stmtType> f = castStmts(finBody); return new TryFinally(t, b, f); } @@ -536,8 +544,8 @@ if (test == null) { return errorHandler.errorStmt(t); } - stmtType[] o = castStmts(orelse); - stmtType[] b = castStmts(body); + List<stmtType> o = castStmts(orelse); + List<stmtType> b = castStmts(body); return new If(t, test, b, o); } @@ -545,8 +553,8 @@ if (test == null) { return errorHandler.errorStmt(t); } - stmtType[] o = castStmts(orelse); - stmtType[] b = castStmts(body); + List<stmtType> o = castStmts(orelse); + List<stmtType> b = castStmts(body); return new While(t, test, b, o); } @@ -555,8 +563,8 @@ return errorHandler.errorStmt(t); } cantBeNone(target); - stmtType[] o = castStmts(orelse); - stmtType[] b = castStmts(body); + List<stmtType> o = castStmts(orelse); + List<stmtType> b = castStmts(body); return new For(t, target, iter, b, o); } @@ -568,8 +576,8 @@ if (func == null) { return errorHandler.errorExpr(new PythonTree(t)); } - keywordType[] k = makeKeywords(keywords); - exprType[] a = castExprs(args); + List<keywordType> k = makeKeywords(keywords); + List<exprType> a = castExprs(args); return new Call(t, func, a, k, starargs, kwargs); } @@ -645,27 +653,27 @@ errorHandler.error("can't assign to conditional expression", e); } else if (e instanceof Tuple) { //XXX: performance problem? Any way to do this better? - exprType[] elts = ((Tuple)e).elts; - if (elts.length == 0) { + List<exprType> elts = ((Tuple)e).elts; + if (elts.size() == 0) { errorHandler.error("can't assign to ()", e); } - for (int i=0;i<elts.length;i++) { - checkAssign(elts[i]); + for (int i=0;i<elts.size();i++) { + checkAssign(elts.get(i)); } } else if (e instanceof org.python.antlr.ast.List) { //XXX: performance problem? Any way to do this better? - exprType[] elts = ((org.python.antlr.ast.List)e).elts; - for (int i=0;i<elts.length;i++) { - checkAssign(elts[i]); + List<exprType> elts = ((org.python.antlr.ast.List)e).elts; + for (int i=0;i<elts.size();i++) { + checkAssign(elts.get(i)); } } } - exprType[] makeDeleteList(List e) { - exprType[] exprs = castExprs(e); - for(int i=0;i<exprs.length;i++) { - if (exprs[i] instanceof Call) { - errorHandler.error("can't delete function call", exprs[i]); + List<exprType> makeDeleteList(List e) { + List<exprType> exprs = castExprs(e); + for(exprType expr : exprs) { + if (expr instanceof Call) { + errorHandler.error("can't delete function call", expr); } } return exprs; @@ -706,15 +714,14 @@ } } - cmpopType[] makeCmpOps(List cmps) { + List<cmpopType> makeCmpOps(List cmps) { + List<cmpopType> result = new ArrayList<cmpopType>(); if (cmps != null) { - List<cmpopType> result = new ArrayList<cmpopType>(); for (Object o: cmps) { result.add((cmpopType)o); } - return result.toArray(new cmpopType[result.size()]); } - return new cmpopType[0]; + return result; } BoolOp makeBoolOp(PythonTree left, boolopType op, List right) { @@ -743,15 +750,14 @@ return current; } - sliceType[] castSlices(List slices) { + List<sliceType> castSlices(List slices) { + List<sliceType> result = new ArrayList<sliceType>(); if (slices != null) { - List<sliceType> result = new ArrayList<sliceType>(); for (Object o:slices) { result.add(castSlice(o)); } - return (sliceType[])result.toArray(new sliceType[result.size()]); } - return new sliceType[0]; + return result; } sliceType castSlice(Object o) { @@ -770,7 +776,7 @@ boolean extslice = false; if (isTuple) { - sliceType[] st; + List<sliceType> st; List etypes = new ArrayList(); for (Object o : sltypes) { if (o instanceof Index) { @@ -782,7 +788,7 @@ } } if (!extslice) { - exprType[] es = (exprType[])etypes.toArray(new exprType[etypes.size()]); + List<exprType> es = etypes; exprType t = new Tuple(begin, es, expr_contextType.Load); s = new Index(begin, t); } @@ -792,7 +798,7 @@ extslice = true; } if (extslice) { - sliceType[] st = castSlices(sltypes);//.toArray(new sliceType[sltypes.size()]); + List<sliceType> st = castSlices(sltypes);//.toArray(new sliceType[sltypes.size()]); s = new ExtSlice(begin, st); } return s; Modified: branches/astwrite/src/org/python/compiler/ArgListCompiler.java =================================================================== --- branches/astwrite/src/org/python/compiler/ArgListCompiler.java 2008-11-17 21:20:25 UTC (rev 5585) +++ branches/astwrite/src/org/python/compiler/ArgListCompiler.java 2008-11-17 21:48:50 UTC (rev 5586) @@ -2,7 +2,8 @@ package org.python.compiler; -import java.util.Vector; +import java.util.ArrayList; +import java.util.List; import org.python.antlr.ParseException; import org.python.antlr.Visitor; @@ -18,64 +19,66 @@ public class ArgListCompiler extends Visitor { public boolean arglist, keywordlist; - public exprType[] defaults; - public Vector names; - public Vector fpnames; - public Vector init_code; + public List<exprType> defaults; + public List<String> names; + public List<String> fpnames; + public List<stmtType> init_code; public ArgListCompiler() { arglist = keywordlist = false; defaults = null; - names = new Vector(); - fpnames = new Vector(); - init_code = new Vector(); + names = new ArrayList<String>(); + fpnames = new ArrayList<String>(); + init_code = new ArrayList<stmtType>(); } public void reset() { arglist = keywordlist = false; defaults = null; - names.removeAllElements(); - init_code.removeAllElements(); + names.clear(); + init_code.clear(); } 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); + int n = node.body.size(); + List<stmtType> newtree = new ArrayList<stmtType>(); + newtree.addAll(init_code); + newtree.addAll(node.body); node.body = newtree; } - public exprType[] getDefaults() { + public List<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(args.args[i], - new exprType[] { args.args[i] }, - new Name(args.args[i], name, expr_contextType.Load)); - init_code.addElement(ass); + for (int i = 0; i < args.args.size(); i++) { + String name = (String) visit(args.args.get(i)); + names.add(name); + if (args.args.get(i) instanceof Tuple) { + List<exprType> targets = new ArrayList<exprType>(); + targets.add(args.args.get(i)); + Assign ass = new Assign(args.args.get(i), + targets, + new Name(args.args.get(i), name, expr_contextType.Load)); + init_code.add(ass); } } if (args.vararg != null) { arglist = true; - names.addElement(args.vararg); + names.add(args.vararg); } if (args.kwarg != null) { keywordlist = true; - names.addElement(args.kwarg); + names.add(args.kwarg); } defaults = args.defaults; - for (int i = 0; i < defaults.length; i++) { - if (defaults[i] == null) + for (int i = 0; i < defaults.size(); i++) { + if (defaults.get(i) == null) throw new ParseException( "non-default argument follows default argument", - args.args[args.args.length - defaults.length + i]); + args.args.get(args.args.size() - defaults.size() + i)); } } @@ -90,19 +93,19 @@ throw new ParseException("duplicate argument name found: " + node.id, node); } - fpnames.addElement(node.id); + fpnames.add(node.id); return node.id; } @Override public Object visitTuple(Tuple node) throws Exception { StringBuffer name = new StringBuffer("("); - int n = node.elts.length; + int n = node.elts.size(); for (int i = 0; i < n-1; i++) { - name.append(visit(node.elts[i])); + name.append(visit(node.elts.get(i))); name.append(", "); } - name.append(visit(node.elts[n - 1])); + name.append(visit(node.elts.get(n - 1))); name.append(")"); return name.toString(); } Modified: branches/astwrite/src/org/python/compiler/CodeCompiler.java =================================================================== --- branches/astwrite/src/org/python/compiler/CodeCompiler.java 2008-11-17 21:20:25 UTC (rev 5585) +++ branches/astwrite/src/org/python/compiler/CodeCompiler.java 2008-11-17 21:48:50 UTC (rev 5586) @@ -4,6 +4,8 @@ import java.io.IOException; import java.util.Hashtable; +import java.util.ArrayList; +import java.util.Collection; import java.util.Map; import java.util.Stack; import java.util.Vector; @@ -60,6 +62,7 @@ import org.python.antlr.ast.While; import org.python.antlr.ast.With; import org.python.antlr.ast.Yield; +import org.python.antlr.ast.aliasType; import org.python.antlr.ast.cmpopType; import org.python.antlr.ast.comprehensionType; import org.python.antlr.ast.excepthandlerType; @@ -294,13 +297,13 @@ public Object visitModule(org.python.antlr.ast.Module suite) throws Exception { - if (suite.body.length > 0 && - suite.body[0] instanceof Expr && - ((Expr)suite.body[0]).value instanceof Str) + if (suite.body.size() > 0 && + suite.body.get(0) instanceof Expr && + ((Expr)suite.body.get(0)).value instanceof Str) { loadFrame(); code.ldc("__doc__"); - visit(((Expr) suite.body[0]).value); + visit(((Expr) suite.body.get(0)).value); code.invokevirtual("org/python/core/PyFrame", "setglobal", "(" +$str + $pyObj + ")V"); } if (module.setFile) { @@ -322,13 +325,13 @@ return visitReturn(new Return(node, node.body), true); } - public int makeArray(PythonTree[] nodes) throws Exception { + public int makeArray(java.util.List<? extends PythonTree> nodes) throws Exception { int n; if (nodes == null) n = 0; else - n = nodes.length; + n = nodes.size(); int array = code.getLocal("[Lorg/python/core/PyObject;"); if (n == 0) { @@ -340,7 +343,7 @@ code.astore(array); for(int i=0; i<n; i++) { - visit(nodes[i]); + visit(nodes.get(i)); code.aload(array); code.swap(); code.iconst(i); @@ -351,11 +354,11 @@ return array; } - public void getDocString(stmtType[] suite) throws Exception { - if (suite.length > 0 && suite[0] instanceof Expr && - ((Expr) suite[0]).value instanceof Str) + public void getDocString(java.util.List<stmtType> suite) throws Exception { + if (suite.size() > 0 && suite.get(0) instanceof Expr && + ((Expr) suite.get(0)).value instanceof Str) { - visit(((Expr) suite[0]).value); + visit(((Expr) suite.get(0)).value); } else { code.aconst_null(); } @@ -426,11 +429,13 @@ return null; } - private void doDecorators(stmtType node, exprType[] decs, String name) throws Exception { - if (decs.length > 0) { + private void doDecorators(stmtType node, java.util.List<exprType> decs, String name) throws Exception { + if (decs.size() > 0) { exprType currentExpr = new Name(node, name, expr_contextType.Load); - for (int i=decs.length - 1;i > -1;i--) { - currentExpr = new Call(node, decs[i], new exprType[]{currentExpr}, new keywordType[0], null, null); + for (int i=decs.size() - 1;i > -1;i--) { + java.util.List args = new ArrayList(); + args.add(currentExpr); + currentExpr = new Call(node, decs.get(i), args, new ArrayList<keywordType>(), null, null); } visit(currentExpr); set(new Name(node, name, expr_contextType.Store)); @@ -454,8 +459,8 @@ public Object visitAssign(Assign node) throws Exception { setline(node); visit(node.value); - if (node.targets.length == 1) { - set(node.targets[0]); + if (node.targets.size() == 1) { + set(node.targets.get(0)); } else { int tmp = storeTop(); for (exprType target : node.targets) { @@ -475,7 +480,7 @@ visit(node.dest); tmp = storeTop(); } - if (node.values == null || node.values.length == 0) { + if (node.values == null || node.values.size() == 0) { if (node.dest != null) { code.aload(tmp); code.invokestatic("org/python/core/Py", "printlnv", "(" + $pyObj + ")V"); @@ -483,18 +488,18 @@ code.invokestatic("org/python/core/Py", "println", "()V"); } } else { - for (int i = 0; i < node.values.length; i++) { + for (int i = 0; i < node.values.size(); i++) { if (node.dest != null) { code.aload(tmp); - visit(node.values[i]); - if (node.nl && i == node.values.length - 1) { + visit(node.values.get(i)); + if (node.nl && i == node.values.size() - 1) { code.invokestatic("org/python/core/Py", "println", "(" + $pyObj + $pyObj + ")V"); } else { code.invokestatic("org/python/core/Py", "printComma", "(" + $pyObj + $pyObj + ")V"); } } else { - visit(node.values[i]); - if (node.nl && i == node.values.length - 1) { + visit(node.values.get(i)); + if (node.nl && i == node.values.size() - 1) { code.invokestatic("org/python/core/Py", "println", "(" + $pyObj + ")V"); } else { code.invokestatic("org/python/core/Py", "printComma", "(" + $pyObj + ")V"); @@ -786,16 +791,16 @@ @Override public Object visitImport(Import node) throws Exception { setline(node); - for (int i = 0; i < node.names.length; i++) { + for (aliasType alias : node.names) { String asname = null; - if (node.names[i].asname != null) { - String name = node.names[i].name; - asname = node.names[i].asname; + if (alias.asname != null) { + String name = alias.name; + asname = alias.asname; code.ldc(name); loadFrame(); code.invokestatic("org/python/core/imp", "importOneAs", "(" + $str + $pyFrame + ")" + $pyObj); } else { - String name = node.names[i].name; + String name = alias.name; asname = name; if (asname.indexOf('.') > 0) asname = asname.substring(0, asname.indexOf('.')); @@ -803,7 +808,7 @@ loadFrame(); code.invokestatic("org/python/core/imp", "importOne", "(" + $str + $pyFrame + ")" + $pyObj); } - set(new Name(node.names[i], asname, expr_contextType.Store)); + set(new Name(alias, asname, expr_contextType.Store)); } return null; } @@ -814,8 +819,10 @@ Future.checkFromFuture(node); // future stmt support setline(node); code.ldc(node.module); - //Note: parser does not allow node.names.length == 0 - if (node.names.length == 1 && node.names[0].name.equals("*")) { + java.util.List<aliasType> names = node.names; + if (names == null || names.size() == 0) { + throw new ParseException("Internel parser error", node); + } else if (names.size() == 1 && names.get(0).name.equals("*")) { if (node.level > 0) { throw new ParseException("'import *' not allowed with 'from .'", node); } @@ -839,15 +846,15 @@ loadFrame(); code.invokestatic("org/python/core/imp", "importAll", "(" + $str + $pyFrame + ")V"); } else { - String[] fromNames = new String[node.names.length]; - String[] asnames = new String[node.names.length]; - for (int i = 0; i < node.names.length; i++) { - fromNames[i] = node.names[i].name; - asnames[i] = node.names[i].asname; - if (asnames[i] == null) - asnames[i] = fromNames[i]; + java.util.List<String> fromNames = new ArrayList<String>();//[names.size()]; + java.util.List<String> asnames = new ArrayList<String>();//[names.size()]; + for (int i = 0; i < names.size(); i++) { + fromNames.add(names.get(i).name); + asnames.add(names.get(i).asname); + if (asnames.get(i) == null) + asnames.set(i, fromNames.get(i)); } - int strArray = makeStrings(code, fromNames, fromNames.length); + int strArray = makeStrings(code, fromNames); code.aload(strArray); code.freeLocal(strArray); @@ -864,11 +871,11 @@ } code.invokestatic("org/python/core/imp", "importFrom", "(" + $str + $strArr + $pyFrame + "I" + ")" + $pyObjArr); int tmp = storeTop(); - for (int i = 0; i < node.names.length; i++) { + for (int i = 0; i < names.size(); i++) { code.aload(tmp); code.iconst(i); code.aaload(); - set(new Name(node.names[i], asnames[i], expr_contextType.Store)); + set(new Name(names.get(i), asnames.get(i), expr_contextType.Store)); } code.freeLocal(tmp); } @@ -1122,8 +1129,8 @@ TryExcept node, int index) throws Exception { - for (int i = 0; i < node.handlers.length; i++) { - excepthandlerType handler = node.handlers[i]; + for (int i = 0; i < node.handlers.size(); i++) { + excepthandlerType handler = node.handlers.get(i); //setline(name); Label end_of_self = new Label(); @@ -1135,7 +1142,7 @@ code.invokestatic("org/python/core/Py", "matchException", "(" + $pyExc + $pyObj + ")Z"); code.ifeq(end_of_self); } else { - if (i != node.handlers.length-1) { + if (i != node.handlers.size()-1) { throw new ParseException( "default 'except:' must be last", handler); } @@ -1312,10 +1319,9 @@ return suite(node.body); } - public Object suite(stmtType[] stmts) throws Exception { - int n = stmts.length; - for(int i = 0; i < n; i++) { - Object exit = visit(stmts[i]); + public Object suite(java.util.List<stmtType> stmts) throws Exception { + for(stmtType s: stmts) { + Object exit = visit(s); if (exit != null) return Exit; } @@ -1325,8 +1331,8 @@ @Override public Object visitBoolOp(BoolOp node) throws Exception { Label end = new Label(); - visit(node.values[0]); - for (int i = 1; i < node.values.length; i++) { + visit(node.values.get(0)); + for (int i = 1; i < node.values.size(); i++) { code.dup(); code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); switch (node.op) { @@ -1338,7 +1344,7 @@ break; } code.pop(); - visit(node.values[i]); + visit(node.values.get(i)); } code.label(end); return null; @@ -1354,24 +1360,24 @@ visit(node.left); code.astore(last); - int n = node.ops.length; + int n = node.ops.size(); for(int i = 0; i < n - 1; i++) { - visit(node.comparators[i]); + visit(node.comparators.get(i)); code.aload(last); code.swap(); code.dup(); code.astore(last); - visitCmpop(node.ops[i]); + visitCmpop(node.ops.get(i)); code.dup(); code.astore(result); code.invokevirtual("org/python/core/PyObject", "__nonzero__", "()Z"); code.ifeq(end); } - visit(node.comparators[n-1]); + visit(node.comparators.get(n-1)); code.aload(last); code.swap(); - visitCmpop(node.ops[n-1]); + visitCmpop(node.ops.get(n-1)); if (n > 1) { code.astore(result); @@ -1488,23 +1494,32 @@ } - public static int makeStrings(Code c, String[] names, int n) + public static int makeStrings(Code c, Collection<String> names) throws IOException { - c.iconst(n); + int n = 0; + if (names != null) { + c.iconst(names.size()); + } else { + c.iconst_0(); + } c.anewarray("java/lang/String"); int strings = c.getLocal("[Ljava/lang/String;"); c.astore(strings); - for (int i=0; i<n; i++) { - c.aload(strings); - c.iconst(i); - c.ldc(names[i]); - c.aastore(); + if (names != null) { + int i = 0; + for (String name : names) { + c.aload(strings); + c.iconst(i); + c.ldc(name); + c.aastore(); + i++; + } } return strings; } - public Object invokeNoKeywords(Attribute node, PythonTree[] values) + public Object invokeNoKeywords(Attribute node, java.util.List<exprType> values) throws Exception { String name = getName(node.attr); @@ -1512,34 +1527,34 @@ code.ldc(name); code.invokevirtual("org/python/core/PyObject", "__getattr__", "(" + $str + ")" + $pyObj); - switch (values.length) { + switch (values.size()) { case 0: stackConsume(); // target code.invokevirtual("org/python/core/PyObject", "__call__", "()" + $pyObj); break; case 1: - visit(values[0]); + visit(values.get(0)); stackConsume(); // target code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + ")" + $pyObj); break; case 2: - visit(values[0]); stackProduce(); - visit(values[1]); + visit(values.get(0)); stackProduce(); + visit(values.get(1)); stackConsume(2); // target + arguments code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + $pyObj + ")" + $pyObj); break; case 3: - visit(values[0]); stackProduce(); - visit(values[1]); stackProduce(); - visit(values[2]); + visit(values.get(0)); stackProduce(); + visit(values.get(1)); stackProduce(); + visit(values.get(2)); stackConsume(3); // target + arguments code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + $pyObj + $pyObj + ")" + $pyObj); break; case 4: - visit(values[0]); stackProduce(); - visit(values[1]); stackProduce(); - visit(values[2]); stackProduce(); - visit(values[3]); + visit(values.get(0)); stackProduce(); + visit(values.get(1)); stackProduce(); + visit(values.get(2)); stackProduce(); + visit(values.get(3)); stackConsume(4); // target + arguments code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + $pyObj + $pyObj + $pyObj + ")" + $pyObj); break; @@ -1557,17 +1572,17 @@ @Override public Object visitCall(Call node) throws Exception { - String[] keys = new String[node.keywords.length]; - exprType[] values = new exprType[node.args.length + keys.length]; - for (int i = 0; i < node.args.length; i++) { - values[i] = node.args[i]; + java.util.List<String> keys = new ArrayList<String>();//[node.keywords.size()]; + java.util.List<exprType> values = new ArrayList<exprType>();//[node.args.size() + keys.size()]; + for (int i = 0; i < node.args.size(); i++) { + values.add(node.args.get(i)); } - for (int i = 0; i < node.keywords.length; i++) { - keys[i] = node.keywords[i].arg; - values[node.args.length + i] = node.keywords[i].value; + for (int i = 0; i < node.keywords.size(); i++) { + keys.add(node.keywords.get(i).arg); + values.add(node.keywords.get(i).value); } - if ((node.keywords == null || node.keywords.length == 0)&& node.starargs == null && + if ((node.keywords == null || node.keywords.size() == 0)&& node.starargs == null && node.kwargs == null && node.func instanceof Attribute) { return invokeNoKeywords((Attribute) node.func, values); @@ -1577,7 +1592,7 @@ if (node.starargs != null || node.kwargs != null) { int argArray = makeArray(values); - int strArray = makeStrings(code, keys, keys.length); + int strArray = makeStrings(code, keys); if (node.starargs == null) code.aconst_null(); else @@ -1599,9 +1614,9 @@ stackConsume(3); // target + starargs + kwargs code.invokevirtual("org/python/core/PyObject", "_callextra", "(" + $pyObjArr + $strArr + $pyObj + $pyObj + ")" + $pyObj); - } else if (keys.length > 0) { + } else if (keys.size() > 0) { int argArray = makeArray(values); - int strArray = makeStrings(code, keys, keys.length); + int strArray = makeStrings(code, keys); code.aload(argArray); code.aload(strArray); code.freeLocal(argArray); @@ -1609,34 +1624,34 @@ stackConsume(); // target code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObjArr + $strArr + ")" + $pyObj); } else { - switch (values.length) { + switch (values.size()) { case 0: stackConsume(); // target code.invokevirtual("org/python/core/PyObject", "__call__", "()" + $pyObj); break; case 1: - visit(values[0]); + visit(values.get(0)); stackConsume(); // target code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + ")" + $pyObj); break; case 2: - visit(values[0]); stackProduce(); - visit(values[1]); + visit(values.get(0)); stackProduce(); + visit(values.get(1)); stackConsume(2); // target + arguments code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + $pyObj + ")" + $pyObj); break; case 3: - visit(values[0]); stackProduce(); - visit(values[1]); stackProduce(); - visit(values[2]); + visit(values.get(0)); stackProduce(); + visit(values.get(1)); stackProduce(); + visit(values.get(2)); stackConsume(3); // target + arguments code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + $pyObj + $pyObj + ")" + $pyObj); break; case 4: - visit(values[0]); stackProduce(); - visit(values[1]); stackProduce(); - visit(values[2]); stackProduce(); - visit(values[3]); + visit(values.get(0)); stackProduce(); + visit(values.get(1)); stackProduce(); + visit(values.get(2)); stackProduce(); + visit(values.get(3)); stackConsume(4); // target + arguments code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + $pyObj + $pyObj + $pyObj + ")" + $pyObj); break; @@ -1789,28 +1804,28 @@ return null; } - public Object seqSet(exprType[] nodes) throws Exception { + public Object seqSet(java.util.List<exprType> nodes) throws Exception { code.aload(temporary); - code.iconst(nodes.length); + code.iconst(nodes.size()); code.invokestatic("org/python/core/Py", "unpackSequence", "(" + $pyObj + "I)" + $pyObjArr); int tmp = code.getLocal("[org/python/core/PyObject"); code.astore(tmp); - for (int i = 0; i < nodes.length; i++) { + for (int i = 0; i < nodes.size(); i++) { code.aload(tmp); code.iconst(i); code.aaload(); - set(nodes[i]); + set(nodes.get(i)); } code.freeLocal(tmp); return null; } - public Object seqDel(exprType[] nodes) throws Exception { - for (int i = 0; i < nodes.length; i++) { - visit(nodes[i]); + public Object seqDel(java.util.List<exprType> nodes) throws Exception { + for (exprType e: nodes) { + visit(e); } return null; } @@ -1866,29 +1881,37 @@ set(new Name(node, tmp_append, expr_contextType.Store)); + java.util.List<exprType> args = new ArrayList<exprType>(); + args.add(node.elt); stmtType n = new Expr(node, new Call(node, new Name(node, tmp_append, expr_contextType.Load), - new exprType[] { node.elt }, - new keywordType[0], null, null)); + args, + new ArrayList<keywordType>(), null, null)); - for (int i = node.generators.length - 1; i >= 0; i--) { - comprehensionType lc = node.generators[i]; - for (int j = lc.ifs.length - 1; j >= 0; j--) { - n = new If(lc.ifs[j], lc.ifs[j], new stmtType[] { n }, new stmtType[0]); + for (int i = node.generators.size() - 1; i >= 0; i--) { + comprehensionType lc = node.generators.get(i); + for (int j = lc.ifs.size() - 1; j >= 0; j--) { + java.util.List<stmtType> body = new ArrayList<stmtType>(); + body.add(n); + n = new If(lc.ifs.get(j), lc.ifs.get(j), body, new ArrayList<stmtType>()); } - n = new For(lc, lc.target, lc.iter, new stmtType[] { n }, new stmtType[0]); + java.util.List<stmtType> body = new ArrayList<stmtType>(); + body.add(n); + n = new For(lc, lc.target, lc.iter, body, new ArrayList<stmtType>()); } visit(n); - visit(new Delete(n, new exprType[] { new Name(n, tmp_append, expr_contextType.Del) })); + java.util.List<exprType> targets = new ArrayList<exprType>(); + targets.add(new Name(n, tmp_append, expr_contextType.Del)); + visit(new Delete(n, targets)); return null; } @Override public Object visitDict(Dict node) throws Exception { - PythonTree[] elts = new PythonTree[node.keys.length * 2]; - for (int i = 0; i < node.keys.length; i++) { - elts[i * 2] = node.keys[i]; - elts[i * 2 + 1] = node.values[i]; + java.util.List<PythonTree> elts = new ArrayList<PythonTree>(); + for (int i = 0; i < node.keys.size(); i++) { + elts.add(node.keys.get(i)); + elts.add(node.values.get(i)); } int content = makeArray(elts); @@ -1912,8 +1935,9 @@ String name = "<lambda>"; //Add a return node onto the outside of suite; - modType retSuite = new Suite(node, new stmtType[] { - new Return(node, node.body) }); + java.util.List<stmtType> bod = new ArrayList<stmtType>(); + bod.add(new Return(node, node.body)); + modType retSuite = new Suite(node, bod); setline(node); @@ -2172,7 +2196,7 @@ ScopeInfo scope = module.getScopeInfo(node); - int emptyArray = makeArray(new exprType[0]); + int emptyArray = makeArray(new ArrayList<exprType>()); code.aload(emptyArray); code.freeLocal(emptyArray); @@ -2182,20 +2206,26 @@ stmtType n = new Expr(node, new Yield(node, node.elt)); exprType iter = null; - for (int i = node.generators.length - 1; i >= 0; i--) { - comprehensionType comp = node.generators[i]; - for (int j = comp.ifs.length - 1; j >= 0; j--) { - n = new If(comp.ifs[j], comp.ifs[j], new stmtType[] { n }, new stmtType[0]); + for (int i = node.generators.size() - 1; i >= 0; i--) { + comprehensionType comp = node.generators.get(i); + for (int j = comp.ifs.size() - 1; j >= 0; j--) { + java.util.List<stmtType> bod = new ArrayList<stmtType>(); + bod.add(n); + n = new If(comp.ifs.get(j), comp.ifs.get(j), bod, new ArrayList<stmtType>()); } + java.util.List<stmtType> bod = new ArrayList<stmtType>(); + bod.add(n); if (i != 0) { - n = new For(comp, comp.target, comp.iter, new stmtType[] { n }, new stmtType[0]); + n = new For(comp, comp.target, comp.iter, bod, new ArrayList<stmtType>()); } else { - n = new For(comp, comp.target, new Name(node, bound_exp, expr_contextType.Load), new stmtType[] { n }, new stmtType[0]); + n = new For(comp, comp.target, new Name(node, bound_exp, expr_contextType.Load), bod, new ArrayList<stmtType>()); iter = comp.iter; } } - module.PyCode(new Suite(node, new stmtType[]{n}), tmp_append, true, + java.util.List<stmtType> bod = new ArrayList<stmtType>(); + bod.add(n); + module.PyCode(new Suite(node, bod), tmp_append, true, className, false, false, node.getLine(), scope, cflags).get(code); @@ -2214,7 +2244,9 @@ code.invokevirtual("org/python/core/PyObject", "__iter__", "()Lorg/python/core/PyObject;"); code.invokevirtual("org/python/core/PyObject", "__call__", "(" + $pyObj + ")" + $pyObj); - visit(new Delete(n, new exprType[] { new Name(n, tmp_append, expr_contextType.Del) })); + java.util.List<exprType> targets = new ArrayList<exprType>(); + targets.add(new Name(n, tmp_append, expr_contextType.Del)); + visit(new Delete(n, targets)); return null; } Modified: branches/astwrite/src/org/python/compiler/Future.java =================================================================== --- branches/astwrite/src/org/python/compiler/Future.java 2008-11-17 21:20:25 UTC (rev 5585) +++ branches/astwrite/src/org/python/compiler/Future.java 2008-11-17 21:48:50 UTC (rev 5586) @@ -2,10 +2,17 @@ package org.python.compiler; -import org.python.antlr.*; -import org.python.antlr.ast.*; +import org.python.antlr.ParseException; +import org.python.antlr.ast.ImportFrom; +import org.python.antlr.ast.Expr; +import org.python.antlr.ast.Interactive; import org.python.antlr.ast.Module; +import org.python.antlr.ast.Str; +import org.python.antlr.ast.modType; +import org.python.antlr.ast.stmtType; +import java.util.List; + public class Future { private boolean division = false; @@ -17,13 +24,13 @@ private boolean check(ImportFrom cand) throws Exception { if (!cand.module.equals(FUTURE)) return false; - int n = cand.names.length; + int n = cand.names.size(); 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; + String feature = cand.names.get(i).name; // *known* features if (feature.equals("nested_scopes")) { continue; @@ -63,11 +70,11 @@ division = cflags.division; } int beg = 0; - stmtType[] suite = null; + List<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) { + if (suite.size() > 0 && suite.get(0) instanceof Expr && + ((Expr) suite.get(0)).value instanceof Str) { beg++; } } else if (node instanceof Interactive) { @@ -76,8 +83,8 @@ return; } - for (int i = beg; i < suite.length; i++) { - ... [truncated message content] |