Fix value matching and add new test case
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 5m6s

This commit is contained in:
Daniel Holle 2024-11-21 14:31:18 +01:00
parent 484a70c15c
commit 46b378e3a5
8 changed files with 42 additions and 17 deletions

View File

@ -0,0 +1,19 @@
import java.lang.Boolean;
import java.lang.Object;
sealed interface List<T> permits Cons, Empty {}
public record Cons<T>(T a, List<T> l) implements List<T> {}
public record Empty() implements List<Object> {}
public class PatternMatchingListAppend {
public append(Cons(a, b), list2) {
return new Cons<>(a, append(b, list2));
}
public append(Empty(), list2) {
return list2;
}
}

View File

@ -468,7 +468,7 @@ public class StatementGenerator {
case RPatternContext rPattern: case RPatternContext rPattern:
RecordPatternContext recordPattern = rPattern.recordPattern(); RecordPatternContext recordPattern = rPattern.recordPattern();
return convert(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: default:
throw new NotImplementedException(); throw new NotImplementedException();
@ -477,7 +477,8 @@ public class StatementGenerator {
} }
private RecordPattern convert(RecordPatternContext recordPatternCtx) { private RecordPattern convert(RecordPatternContext recordPatternCtx) {
List<PatternContext> subPatternCtx = recordPatternCtx.recordStructurePattern().recordComponentPatternList().pattern(); var cpl = recordPatternCtx.recordStructurePattern().recordComponentPatternList();
List<PatternContext> subPatternCtx = cpl == null ? List.of() : cpl.pattern();
List<Pattern> subPattern = subPatternCtx.stream().map(this::convert).collect(Collectors.toList()); List<Pattern> subPattern = subPatternCtx.stream().map(this::convert).collect(Collectors.toList());
IdentifierContext identifierCtx = recordPatternCtx.identifier(); IdentifierContext identifierCtx = recordPatternCtx.identifier();
var text = (identifierCtx != null) ? identifierCtx.getText() : null; var text = (identifierCtx != null) ? identifierCtx.getText() : null;

View File

@ -12,15 +12,15 @@ import org.antlr.v4.runtime.Token;
public class LiteralPattern extends FormalParameter public class LiteralPattern extends FormalParameter
{ {
public final Object value; public final Expression value;
public LiteralPattern(RefTypeOrTPHOrWildcardOrGeneric type, Object value, Token offset) { public LiteralPattern(RefTypeOrTPHOrWildcardOrGeneric type, Expression value, Token offset) {
super((String) value, type, offset); super(null, type, offset);
this.value = value; this.value = value;
} }
@Override @Override
public FormalParameter withType(RefTypeOrTPHOrWildcardOrGeneric type) { public FormalParameter withType(RefTypeOrTPHOrWildcardOrGeneric type) {
return null; return new LiteralPattern(type, value, getOffset());
} }
@Override @Override
public void accept(ASTVisitor visitor) { public void accept(ASTVisitor visitor) {

View File

@ -34,7 +34,6 @@ public class ASTFactory {
private static final HashMap<java.lang.Class, ClassOrInterface> cache = new HashMap<>(); private static final HashMap<java.lang.Class, ClassOrInterface> cache = new HashMap<>();
public static ClassOrInterface createClass(java.lang.Class jreClass) { public static ClassOrInterface createClass(java.lang.Class jreClass) {
System.out.println(jreClass);
if (cache.containsKey(jreClass)) if (cache.containsKey(jreClass))
return cache.get(jreClass); return cache.get(jreClass);
@ -174,7 +173,6 @@ public class ASTFactory {
superClass = (RefType) createType(java.lang.Object.class); superClass = (RefType) createType(java.lang.Object.class);
} }
List<RefType> implementedInterfaces = new ArrayList<>(); List<RefType> implementedInterfaces = new ArrayList<>();
System.out.println(jreClass);
for (Type jreInterface : jreClass.getGenericInterfaces()) { for (Type jreInterface : jreClass.getGenericInterfaces()) {
implementedInterfaces.add((RefType) createType(jreInterface)); implementedInterfaces.add((RefType) createType(jreInterface));
} }

View File

@ -59,7 +59,7 @@ public class OutputGenerator implements ASTVisitor {
@Override @Override
public void visit(LiteralPattern literalPattern) { public void visit(LiteralPattern literalPattern) {
out.append(literalPattern.value); literalPattern.value.accept(this);
} }
@Override @Override
@ -513,12 +513,11 @@ public class OutputGenerator implements ASTVisitor {
aRecordPattern.getType().accept(this); aRecordPattern.getType().accept(this);
out.append("("); out.append("(");
List<Pattern> subPatterns = aRecordPattern.getSubPattern(); List<Pattern> subPatterns = aRecordPattern.getSubPattern();
int i; for (var i = 0; i < subPatterns.size(); i++) {
for (i = 0; i < subPatterns.size() - 1; i++) {
subPatterns.get(i).accept(this); subPatterns.get(i).accept(this);
out.append(", "); if (i < subPatterns.size() - 1)
out.append(", ");
} }
subPatterns.get(i).accept(this);
String name; String name;
if ((name = aRecordPattern.getName()) != null) if ((name = aRecordPattern.getName()) != null)
out.append(name); out.append(name);

View File

@ -570,7 +570,7 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(LiteralPattern literalPattern) { 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 @Override

View File

@ -841,7 +841,7 @@ public class TYPEStmt implements StatementVisitor {
for (var child : switchStmt.getBlocks()) { for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) { for (var label : child.getLabels()) {
if (label.getPattern() == null) { if (label.getPattern() == null) {
System.out.println("DefaultCase"); //System.out.println("DefaultCase");
} else { } else {
constraintsSet.addUndConstraint( constraintsSet.addUndConstraint(
new Pair( new Pair(
@ -910,10 +910,11 @@ public class TYPEStmt implements StatementVisitor {
for (var subPattern : pattern.getSubPattern()) { for (var subPattern : pattern.getSubPattern()) {
for (Constructor con : constructors) { 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()))); 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); if (subPattern instanceof RecordPattern) recursivelyAddRecordConstraints((RecordPattern) subPattern);
else if (subPattern instanceof LiteralPattern lp) lp.value.accept(this);
counter++; counter++;
} }
} }

View File

@ -904,7 +904,7 @@ public class TestComplete {
//@Ignore("Not implemented") //@Ignore("Not implemented")
@Test @Test
public void testOverloadPatternMatching() throws Exception { public void testPatternMatchingHaskellStyle() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingHaskellStyle.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingHaskellStyle.jav");
var clazz = classFiles.get("PatternMatchingHaskellStyle"); var clazz = classFiles.get("PatternMatchingHaskellStyle");
var R2 = classFiles.get("Elem"); 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 @Test
public void testOverloadSwitch() throws Exception { public void testOverloadSwitch() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchOverload.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchOverload.jav");