diff options
author | geno <gabriele.genovese2@studio.unibo.it> | 2024-07-18 12:16:40 +0200 |
---|---|---|
committer | geno <gabriele.genovese2@studio.unibo.it> | 2024-07-18 12:16:40 +0200 |
commit | 31ee062ad12adcf6dae6716cb566fcbcde9f6959 (patch) | |
tree | 2fdc2b1b8ee39269bb1308cbff12f9e60630ee02 | |
parent | 635360137c14d785ccbea17d4a93f49418c8ca69 (diff) |
codegen while implemented, test added
-rw-r--r-- | src/ast/Python3VisitorImpl.java | 2 | ||||
-rw-r--r-- | src/ast/nodes/WhileStmtNode.java | 18 | ||||
-rw-r--r-- | src/codegen/Label.java | 2 | ||||
-rw-r--r-- | test/2f.py | 4 | ||||
-rw-r--r-- | test/2g-fibonacci.py | 13 |
5 files changed, 30 insertions, 9 deletions
diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index e6728d5..7e8881f 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -504,7 +504,6 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> { } private void optimizeWithThird(BlockNode block, int lineStart, int lineStop, int index) { - int counter = 0; ArrayList<Node> stms = block.getChilds(); for (var e : stms) { if (e instanceof SimpleStmtsNode) { @@ -543,7 +542,6 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> { rewriter.replace(firstToken, lastToken, newVar); } } - counter++; } } } diff --git a/src/ast/nodes/WhileStmtNode.java b/src/ast/nodes/WhileStmtNode.java index 43bf5ff..64b0b71 100644 --- a/src/ast/nodes/WhileStmtNode.java +++ b/src/ast/nodes/WhileStmtNode.java @@ -4,6 +4,7 @@ import ast.types.*; import java.util.ArrayList; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; +import codegen.Label; /** * Node for the `while_stmt` statement of the grammar. @@ -33,12 +34,21 @@ public class WhileStmtNode implements Node { return new VoidType(); } - /** - * NOTE: It is not a part for this project. - */ @Override public String codeGeneration() { - return ""; + String startLabel = Label.newBasic("start"); + String endLabel = Label.newBasic("end"); + + String exprS = expr.codeGeneration(); + String blockS = block.codeGeneration(); + + return startLabel + ":\n" + + exprS + + "storei T1 0\n" + + "beq A0 T1 " + endLabel + "\n" + + blockS + + "b " + startLabel + "\n" + + endLabel + ":\n"; } @Override diff --git a/src/codegen/Label.java b/src/codegen/Label.java index a301ede..61403d8 100644 --- a/src/codegen/Label.java +++ b/src/codegen/Label.java @@ -41,6 +41,6 @@ public class Label { public static String newVar() { - return "_tmp" + (varDefCount++); + return "tmp" + (varDefCount++); } } @@ -3,8 +3,8 @@ n = 6 counter = 1 result = 1 -while n - 1 == counter: +while n - 1 != counter: result = result * counter - counter = counter - 1 + counter = counter + 1 print(result) diff --git a/test/2g-fibonacci.py b/test/2g-fibonacci.py new file mode 100644 index 0000000..fb1ac9f --- /dev/null +++ b/test/2g-fibonacci.py @@ -0,0 +1,13 @@ +n = 9 + +a = 0 +b = 1 +c = 0 +i = 0 +while i < n - 2: + c = a + b + a = b + b = c + i = i + 1 + +print(c) |