From 635360137c14d785ccbea17d4a93f49418c8ca69 Mon Sep 17 00:00:00 2001
From: geno <gabrigeno@gmail.com>
Date: Wed, 17 Jul 2024 20:12:29 +0200
Subject: fixed: counter not incrementing if the operation is an atom

---
 src/ast/Python3VisitorImpl.java | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

(limited to 'src/ast/Python3VisitorImpl.java')

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 class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> {
         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 class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> {
             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 class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> {
                 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);
-- 
cgit v1.2.3-18-g5258