diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 6b3d7d0..e6728d5 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -348,9 +348,10 @@ public Node visitWhile_stmt(While_stmtContext ctx) { optimizeWithSecond(block, lineStart, lineStop, index); // optimize while's guard - int counter = 0; + int counter = -1; var exprs = expr.getExprs(); for (var e : exprs) { + counter++; if (e instanceof ExprNode) { ExprNode exprNode = (ExprNode) e; if (exprNode.typeCheck() instanceof AtomType) { @@ -361,6 +362,10 @@ public Node visitWhile_stmt(While_stmtContext ctx) { if (!al.isEmpty()) { boolean constant = true; for (String a : al) { + if(R.get(a) == null) { + constant = false; + break; + } int n = R.get(a); if (n > lineStart && n <= lineStop) { constant = false; @@ -370,12 +375,11 @@ public Node visitWhile_stmt(While_stmtContext ctx) { if (constant) { String newVar = Label.newVar(); rewriter.insertBefore(index, newVar + "=" + e.toPrint("") + "\n"); - int lastToken = ctx.expr().expr(counter).getStop().getTokenIndex(); int firstToken = ctx.expr().expr(counter).getStart().getTokenIndex(); + int lastToken = ctx.expr().expr(counter).getStop().getTokenIndex(); rewriter.replace(firstToken, lastToken, newVar); } } - counter++; } optimizeWithThird(block, lineStart, lineStop, index); diff --git a/test/2f.py b/test/2f.py new file mode 100644 index 0000000..d83457b --- /dev/null +++ b/test/2f.py @@ -0,0 +1,10 @@ + +n = 6 +counter = 1 +result = 1 + +while n - 1 == counter: + result = result * counter + counter = counter - 1 + +print(result)