From 2ae098898633803482f9610c3b521aa461759885 Mon Sep 17 00:00:00 2001 From: i22035 Date: Sun, 30 Jun 2024 22:17:52 +0200 Subject: [PATCH] Changed For Loop Structure --- src/main/java/parser/astBuilder/ASTBuilder.java | 11 +++++++++-- src/test/java/parser/AstBuilderTest.java | 14 ++++++++++++++ src/test/resources/input/javaCases/Increment.java | 12 ++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/input/javaCases/Increment.java diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 55750a5..fcabdfa 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -207,10 +207,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { crement = (IStatementNode) visit(ctx.statementExpression(i)); } - BlockNode forBlock = (BlockNode) visit(ctx.blockStatement()); + BlockNode forBlock = new BlockNode(); + + BlockNode forStatements = (BlockNode) visit(ctx.blockStatement()); + if(forStatements != null) { + forBlock.addStatement((IStatementNode) forStatements); + } if(crement != null){ - forBlock.addStatement((crement)); + BlockNode forCrement = new BlockNode(); + forCrement.addStatement((crement)); + forBlock.addStatement(forCrement); } WhileNode While = new WhileNode(condition, forBlock); diff --git a/src/test/java/parser/AstBuilderTest.java b/src/test/java/parser/AstBuilderTest.java index 38a601c..f239a41 100644 --- a/src/test/java/parser/AstBuilderTest.java +++ b/src/test/java/parser/AstBuilderTest.java @@ -200,6 +200,20 @@ class AstBuilderTest { //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } + //Noch nicht speziell Increment nur zum Development Testen per Debug + @Test + @DisplayName("Increment Test") + public void incrementTest(){ + ClassNode classNode = Helper.generateEmptyClass("TestClass"); + classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + + ProgramNode expected = new ProgramNode(); + expected.addClass(classNode); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Increment.java"); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + } + diff --git a/src/test/resources/input/javaCases/Increment.java b/src/test/resources/input/javaCases/Increment.java new file mode 100644 index 0000000..9560a9b --- /dev/null +++ b/src/test/resources/input/javaCases/Increment.java @@ -0,0 +1,12 @@ +public class Increment { + + public int test; + + public void increment(int p) { + test = p++; + + for(int i = 1; i<=10, i++) { + int a = 5; + } + } +}