Skip to content

Commit

Permalink
fixed: counter not incrementing if the operation is an atom
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielegenovese committed Jul 17, 2024
1 parent 0f6ba6b commit 6353601
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/ast/Python3VisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/2f.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

n = 6
counter = 1
result = 1

while n - 1 == counter:
result = result * counter
counter = counter - 1

print(result)

0 comments on commit 6353601

Please # to comment.