From 46b378e3a5650beda01973e687e5983fbf08a3c0 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 21 Nov 2024 14:31:18 +0100 Subject: [PATCH] Fix value matching and add new test case --- .../javFiles/PatternMatchingListAppend.jav | 19 +++++++++++++++++++ .../StatementGenerator.java | 5 +++-- .../syntaxtree/LiteralPattern.java | 8 ++++---- .../syntaxtree/factory/ASTFactory.java | 2 -- .../syntaxtree/visual/OutputGenerator.java | 9 ++++----- .../generate/StatementToTargetExpression.java | 2 +- .../typeinference/typeAlgo/TYPEStmt.java | 5 +++-- src/test/java/TestComplete.java | 9 ++++++++- 8 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 resources/bytecode/javFiles/PatternMatchingListAppend.jav diff --git a/resources/bytecode/javFiles/PatternMatchingListAppend.jav b/resources/bytecode/javFiles/PatternMatchingListAppend.jav new file mode 100644 index 00000000..dfbf3918 --- /dev/null +++ b/resources/bytecode/javFiles/PatternMatchingListAppend.jav @@ -0,0 +1,19 @@ +import java.lang.Boolean; +import java.lang.Object; + +sealed interface List permits Cons, Empty {} + +public record Cons(T a, List l) implements List {} +public record Empty() implements List {} + +public class PatternMatchingListAppend { + + public append(Cons(a, b), list2) { + return new Cons<>(a, append(b, list2)); + } + + public append(Empty(), list2) { + return list2; + } + +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 5a80acc8..688ea471 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -468,7 +468,7 @@ public class StatementGenerator { case RPatternContext rPattern: RecordPatternContext recordPattern = rPattern.recordPattern(); return convert(recordPattern); - case Java17Parser.LPatternContext patternContext: return new LiteralPattern(TypePlaceholder.fresh(patternContext.start), patternContext.literal().get(0).getText(), patternContext.start); + case Java17Parser.LPatternContext patternContext: return new LiteralPattern(TypePlaceholder.fresh(patternContext.start), convert(patternContext.literal().get(0)), patternContext.start); default: throw new NotImplementedException(); @@ -477,7 +477,8 @@ public class StatementGenerator { } private RecordPattern convert(RecordPatternContext recordPatternCtx) { - List subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern(); + var cpl = recordPatternCtx.recordStructurePattern().recordComponentPatternList(); + List subPatternCtx = cpl == null ? List.of() : cpl.pattern(); List subPattern = subPatternCtx.stream().map(this::convert).collect(Collectors.toList()); IdentifierContext identifierCtx = recordPatternCtx.identifier(); var text = (identifierCtx != null) ? identifierCtx.getText() : null; diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/LiteralPattern.java b/src/main/java/de/dhbwstuttgart/syntaxtree/LiteralPattern.java index 936f7339..36dff01e 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/LiteralPattern.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/LiteralPattern.java @@ -12,15 +12,15 @@ import org.antlr.v4.runtime.Token; public class LiteralPattern extends FormalParameter { - public final Object value; - public LiteralPattern(RefTypeOrTPHOrWildcardOrGeneric type, Object value, Token offset) { - super((String) value, type, offset); + public final Expression value; + public LiteralPattern(RefTypeOrTPHOrWildcardOrGeneric type, Expression value, Token offset) { + super(null, type, offset); this.value = value; } @Override public FormalParameter withType(RefTypeOrTPHOrWildcardOrGeneric type) { - return null; + return new LiteralPattern(type, value, getOffset()); } @Override public void accept(ASTVisitor visitor) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 520097d8..63767c4d 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -34,7 +34,6 @@ public class ASTFactory { private static final HashMap cache = new HashMap<>(); public static ClassOrInterface createClass(java.lang.Class jreClass) { - System.out.println(jreClass); if (cache.containsKey(jreClass)) return cache.get(jreClass); @@ -174,7 +173,6 @@ public class ASTFactory { superClass = (RefType) createType(java.lang.Object.class); } List implementedInterfaces = new ArrayList<>(); - System.out.println(jreClass); for (Type jreInterface : jreClass.getGenericInterfaces()) { implementedInterfaces.add((RefType) createType(jreInterface)); } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 053cea3a..a3711e38 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -59,7 +59,7 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(LiteralPattern literalPattern) { - out.append(literalPattern.value); + literalPattern.value.accept(this); } @Override @@ -513,12 +513,11 @@ public class OutputGenerator implements ASTVisitor { aRecordPattern.getType().accept(this); out.append("("); List subPatterns = aRecordPattern.getSubPattern(); - int i; - for (i = 0; i < subPatterns.size() - 1; i++) { + for (var i = 0; i < subPatterns.size(); i++) { subPatterns.get(i).accept(this); - out.append(", "); + if (i < subPatterns.size() - 1) + out.append(", "); } - subPatterns.get(i).accept(this); String name; if ((name = aRecordPattern.getName()) != null) out.append(name); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index a7eea728..071c2c57 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -570,7 +570,7 @@ public class StatementToTargetExpression implements ASTVisitor { @Override public void visit(LiteralPattern literalPattern) { - result = new TargetExpressionPattern(converter.convert(new Literal(literalPattern.getType(), literalPattern.value, literalPattern.getOffset()))); + result = new TargetExpressionPattern(converter.convert(literalPattern.value)); } @Override diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 878a7414..bb87e2a1 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -841,7 +841,7 @@ public class TYPEStmt implements StatementVisitor { for (var child : switchStmt.getBlocks()) { for (var label : child.getLabels()) { if (label.getPattern() == null) { - System.out.println("DefaultCase"); + //System.out.println("DefaultCase"); } else { constraintsSet.addUndConstraint( new Pair( @@ -910,10 +910,11 @@ public class TYPEStmt implements StatementVisitor { for (var subPattern : pattern.getSubPattern()) { for (Constructor con : constructors) { - System.out.println("----------------------\n" + subPattern.getType() + " | " + con.getParameterList().getParameterAt(counter).getType() + "\n----------------------\n"); + //System.out.println("----------------------\n" + subPattern.getType() + " | " + con.getParameterList().getParameterAt(counter).getType() + "\n----------------------\n"); constraintsSet.addUndConstraint(new Pair(subPattern.getType(), con.getParameterList().getParameterAt(counter).getType(), PairOperator.SMALLERDOT, loc(con.getParameterList().getParameterAt(counter).getOffset()))); } if (subPattern instanceof RecordPattern) recursivelyAddRecordConstraints((RecordPattern) subPattern); + else if (subPattern instanceof LiteralPattern lp) lp.value.accept(this); counter++; } } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 2e40cdea..7441a159 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -904,7 +904,7 @@ public class TestComplete { //@Ignore("Not implemented") @Test - public void testOverloadPatternMatching() throws Exception { + public void testPatternMatchingHaskellStyle() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingHaskellStyle.jav"); var clazz = classFiles.get("PatternMatchingHaskellStyle"); var R2 = classFiles.get("Elem"); @@ -925,6 +925,13 @@ public class TestComplete { } + @Test + public void testPatternMatchingListAppend() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingListAppend.jav"); + var clazz = classFiles.get("PatternMatchingListAppend"); + var instance = clazz.getDeclaredConstructor().newInstance(); + } + @Test public void testOverloadSwitch() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchOverload.jav");