feat: adding Constraints.
This commit is contained in:
parent
be60261795
commit
b18b0a38cf
@ -10,7 +10,7 @@ public record Rec(Object a, Object b) {}
|
||||
public class SwitchInfered {
|
||||
public main(o) {
|
||||
return switch (o) {
|
||||
case Rec(a, b) -> a+b;
|
||||
case Rec(a, b) -> a;
|
||||
case Integer i -> i+1;
|
||||
default -> 0;
|
||||
};
|
||||
|
15
resources/bytecode/javFiles/recordList.jav
Normal file
15
resources/bytecode/javFiles/recordList.jav
Normal file
@ -0,0 +1,15 @@
|
||||
sealed interface List<A> permits Cons, Nil {}
|
||||
|
||||
record Cons(A a, List<A> l) implements List<A> {}
|
||||
|
||||
record Nil()implements List<A> {
|
||||
|
||||
void m(x) {
|
||||
switch(x) {
|
||||
case Cons(a, Cons(b, List<A> l)) -> System.out.println(a);
|
||||
case Cons(a, Nil()) -> System.out.println(a);
|
||||
case Nil() -> System.out.println("Nil");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
//PL 2018-12-19: Merge chekcen
|
||||
package de.dhbwstuttgart.typeinference.typeAlgo;
|
||||
|
||||
import java.lang.Record;
|
||||
import java.sql.Ref;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import de.dhbwstuttgart.core.JavaTXCompiler;
|
||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||
import de.dhbwstuttgart.exceptions.TypeinferenceException;
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
@ -48,7 +50,9 @@ public class TYPEStmt implements StatementVisitor {
|
||||
|
||||
private SourceLoc loc(Token token) {
|
||||
return new SourceLoc(info.getCurrentClass().getFileName(), token.getLine());
|
||||
};
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
/**
|
||||
* Erstellt einen neuen GenericResolver Die Idee dieser Datenstruktur ist es, GTVs einen eindeutigen TPH zuzuweisen. Bei Methodenaufrufen oder anderen Zugriffen, bei denen alle benutzten GTVs jeweils einen einheitlichen TPH bekommen müssen kann diese Klasse eingesetzt werden. Wichtig ist, dass hierfür jeweils eine frische Instanz benutzt wird.
|
||||
@ -834,7 +838,15 @@ public class TYPEStmt implements StatementVisitor {
|
||||
if (caseExpressionType == null) {
|
||||
for (var child : switchStmt.getBlocks()) {
|
||||
for (var label : child.getLabels()) {
|
||||
if (label.getPattern() instanceof FormalParameter) {
|
||||
if (label.getPattern() == null) {
|
||||
System.out.println("DefaultCase");
|
||||
}
|
||||
if (label.getPattern() instanceof RecordPattern) {
|
||||
RecordPattern recordPattern = (RecordPattern) label.getPattern();
|
||||
System.out.println("HALFTAN DER WEI?E ICH CHECK ECHT GART NCIHTS MEHR");
|
||||
System.out.println(recordPattern.getName());
|
||||
//constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset())));
|
||||
} else if (label.getPattern() instanceof FormalParameter) {
|
||||
constraintsSet.addUndConstraint(new Pair(label.getPattern().getType(), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset())));
|
||||
}
|
||||
}
|
||||
@ -844,6 +856,35 @@ public class TYPEStmt implements StatementVisitor {
|
||||
}
|
||||
|
||||
for (var child : switchStmt.getBlocks()) {
|
||||
|
||||
//Hier Constraints hinzufügen das die Parameter vom Parameter des definierten Record sein müssen -> Wie komm ich an den goofy ah Record?
|
||||
|
||||
|
||||
child.getLabels().forEach(el -> {
|
||||
if (el.getType() instanceof RefType) {
|
||||
var recType = el;
|
||||
|
||||
if(el.getPattern() instanceof RecordPattern){
|
||||
var pattern = (RecordPattern) recType.getPattern();
|
||||
|
||||
var allClasses = info.getAvailableClasses();
|
||||
var interestingClasses = allClasses.stream().filter(as -> as.getClassName().equals(((RefType) el.getType()).getName())).toList();
|
||||
|
||||
var test = "";
|
||||
|
||||
for(int i = 0; i < pattern.getSubPattern().size(); i++){
|
||||
var supPattern = pattern.getSubPattern().get(i);
|
||||
for(int j = 0; j < interestingClasses.size(); j++){
|
||||
constraintsSet.addUndConstraint(new Pair(supPattern.getType(), interestingClasses.get(j).getFieldDecl().get(i).getType(), PairOperator.SMALLERDOT, loc(switchStmt.getOffset())));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
child.accept(this);
|
||||
constraintsSet.addUndConstraint(new Pair(child.getType(), switchStmt.getType(), PairOperator.SMALLERDOT, loc(switchStmt.getOffset())));
|
||||
}
|
||||
@ -860,7 +901,7 @@ public class TYPEStmt implements StatementVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(SwitchLabel switchLabel) {
|
||||
// TODO Auto-generated method stub
|
||||
System.out.println("I MOCH WAS");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -690,7 +690,24 @@ public class TestComplete {
|
||||
var r1 = ctor.newInstance(1, 1);
|
||||
|
||||
|
||||
assertEquals(swtch.invoke(instance, r1), 2);
|
||||
assertEquals(swtch.invoke(instance, r1), 1);
|
||||
}
|
||||
|
||||
@Ignore("Not implemented")
|
||||
@Test
|
||||
public void testRecordList() throws Exception {
|
||||
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "recordList.jav");
|
||||
var clazz = classFiles.get("Nil");
|
||||
|
||||
var instance = clazz.getDeclaredConstructor().newInstance();
|
||||
var swtch = clazz.getDeclaredMethod("m", Object.class);
|
||||
|
||||
//var record = classFiles.get("Rec");
|
||||
//var ctor = record.getDeclaredConstructor(Object.class, Object.class);
|
||||
//var r1 = ctor.newInstance(1, 1);
|
||||
|
||||
|
||||
assertEquals(swtch.invoke(instance, null), 2);
|
||||
}
|
||||
|
||||
@Ignore("Not implemented")
|
||||
|
Loading…
Reference in New Issue
Block a user