From f08744479a43635c1e38b7c6e7af145ce19cc67e Mon Sep 17 00:00:00 2001 From: luca9913 Date: Wed, 7 Jun 2023 22:24:16 +0200 Subject: [PATCH] Started test driven bug fixing for SyntaxTreeGenerator --- .../parser/antlr/Java17Parser.g4 | 2 +- .../StatementGenerator.java | 20 +++++++++++-------- .../syntaxtreegenerator/TestComplete.java | 3 +++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 index 00b35fbb..6c658a7c 100644 --- a/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 +++ b/src/main/antlr4/de/dhbwstuttgart/parser/antlr/Java17Parser.g4 @@ -475,7 +475,7 @@ blockStatement ; localVariableDeclaration - : variableModifier* (VAR identifier '=' expression | typeType? variableDeclarators) + : variableModifier* (VAR identifier '=' expression | typeType variableDeclarators) ; identifier diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index bdfea53b..25ac46d1 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -381,8 +381,12 @@ public class StatementGenerator { type = TypeGenerator.convert(declaration.typeType(), reg, generics); } if (Objects.isNull(declaration.variableDeclarators())) { - Token offset = declaration.identifier().getStart(); - ret.add(new Assign(new AssignToLocal(new LocalVar(declaration.identifier().getText(), type, offset)), convert(declaration.expression()), offset)); + IdentifierContext identifier = declaration.identifier(); + Token offset = identifier.getStart(); + String name = identifier.getText(); + ret.add(new LocalVarDecl(name, type, offset)); + this.localVars.put(name, type); + ret.add(new Assign(new AssignToLocal(new LocalVar(name, type, offset)), convert(declaration.expression()), offset)); } else { ret.addAll(generateLocalVariableAssignments(declaration.variableDeclarators().variableDeclarator(), type)); } @@ -545,9 +549,9 @@ public class StatementGenerator { } ArgumentList argumentList = convertArguments(expr.expressionList()); - ArrayList argTypes = argumentList.getArguments().stream().map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)).collect(Collectors.toCollection(ArrayList::new)); - - MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), argTypes, offset); + ArrayList signature = argumentList.getArguments().stream().map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(offset)); // return type + MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset); ret.setStatement(); return ret; } @@ -561,9 +565,9 @@ public class StatementGenerator { name = expr.SUPER().getText(); } ArgumentList argumentList = convertArguments(expr.expressionList()); - ArrayList argTypes = argumentList.getArguments().stream().map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)).collect(Collectors.toCollection(ArrayList::new)); - - MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), argTypes, offset); + ArrayList signature = argumentList.getArguments().stream().map(x -> (RefTypeOrTPHOrWildcardOrGeneric) TypePlaceholder.fresh(offset)).collect(Collectors.toCollection(ArrayList::new)); + signature.add(TypePlaceholder.fresh(offset)); // return type + MethodCall ret = new MethodCall(TypePlaceholder.fresh(offset), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(offset), signature, offset); ret.setStatement(); return ret; } diff --git a/src/test/java/syntaxtreegenerator/TestComplete.java b/src/test/java/syntaxtreegenerator/TestComplete.java index 87a40d7a..15a7770d 100644 --- a/src/test/java/syntaxtreegenerator/TestComplete.java +++ b/src/test/java/syntaxtreegenerator/TestComplete.java @@ -14,6 +14,9 @@ import org.junit.Test; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; +/** + * Unit-Tests für den 'SyntaxTreeGenerator' aus dem Package 'parser' nach Vorbild der Klasse 'TestComplete' aus dem Test-Package 'targetast' + */ public class TestComplete { private static HashMap javFiles = new HashMap<>();