summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgeno <gabrigeno@gmail.com>2024-07-17 20:12:29 +0200
committergeno <gabrigeno@gmail.com>2024-07-17 20:12:47 +0200
commit635360137c14d785ccbea17d4a93f49418c8ca69 (patch)
tree085fc8531cdfcc9d42ddd61ad4e48174a3e0243b
parent0f6ba6b9ae7781715fc90db8d261bbdaf6a6411b (diff)
fixed: counter not incrementing if the operation is an atom
-rw-r--r--src/ast/Python3VisitorImpl.java10
-rw-r--r--test/2f.py10
2 files changed, 17 insertions, 3 deletions
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);
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)