Rename test file and some work on the overloading behavior
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 50s
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 50s
This commit is contained in:
parent
c33e372446
commit
e2bf09548f
@ -5,13 +5,13 @@ import java.lang.Float;
|
|||||||
public record Point(Number x, Number y) {}
|
public record Point(Number x, Number y) {}
|
||||||
|
|
||||||
public class OverloadPattern {
|
public class OverloadPattern {
|
||||||
public m(Point(x, y), Point(z, a)) {
|
public Number m(Point(Integer x, Integer y)) {
|
||||||
return x + y + z + a;
|
return x + y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public m(Point(Float x, Float y)) {
|
public Number m(Point(Float x, Float y)) {
|
||||||
return x * y;
|
return x * y;
|
||||||
}*/
|
}
|
||||||
|
|
||||||
public m(Integer x) {
|
public m(Integer x) {
|
||||||
return x;
|
return x;
|
||||||
|
@ -5,7 +5,7 @@ sealed interface List<T> permits LinkedElem, Elem {}
|
|||||||
public record LinkedElem<T>(T a, List<T> l) implements List<T> {}
|
public record LinkedElem<T>(T a, List<T> l) implements List<T> {}
|
||||||
public record Elem<T>(T a) implements List<T> {}
|
public record Elem<T>(T a) implements List<T> {}
|
||||||
|
|
||||||
public class PaternMatchingHaskellStyle {
|
public class PatternMatchingHaskellStyle {
|
||||||
|
|
||||||
public append(LinkedElem(a, b), list2) {
|
public append(LinkedElem(a, b), list2) {
|
||||||
return handleAppend(a, b, list2);
|
return handleAppend(a, b, list2);
|
@ -145,21 +145,44 @@ public class ASTToTargetAST {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is used to serve as a custom equality to signature that performs a weak check without going into record patterns.
|
||||||
|
// The two signatures are considered equal if all the argument types match.
|
||||||
|
record WeakSignature(TargetMethod.Signature signature) {
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (!(o instanceof WeakSignature other)) return false;
|
||||||
|
if (other.signature.parameters().size() != signature.parameters().size()) return false;
|
||||||
|
for (var i = 0; i < signature.parameters().size(); i++) {
|
||||||
|
var p1 = signature.parameters().get(i).pattern().type();
|
||||||
|
var p2 = other.signature.parameters().get(i).pattern().type();
|
||||||
|
if (!p1.equals(p2)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(signature.parameters().stream().map(p -> p.pattern().type()).toArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public List<List<TargetMethod>> groupOverloads(ClassOrInterface input, List<Method> methods) {
|
public List<List<TargetMethod>> groupOverloads(ClassOrInterface input, List<Method> methods) {
|
||||||
var res = new ArrayList<List<TargetMethod>>();
|
var res = new ArrayList<List<TargetMethod>>();
|
||||||
|
var mapOfSignatures = new HashMap<WeakSignature, List<MethodWithTphs>>();
|
||||||
for (var method : methods) {
|
for (var method : methods) {
|
||||||
// Convert all methods
|
// Convert all methods
|
||||||
var methodsWithTphs = convert(input, method);
|
var methodsWithTphs = convert(input, method);
|
||||||
// Then check for methods with the same signature
|
// Then check for methods with the same signature
|
||||||
var mapOfSignatures = new HashMap<TargetMethod.Signature, List<MethodWithTphs>>();
|
|
||||||
for (var m : methodsWithTphs) {
|
for (var m : methodsWithTphs) {
|
||||||
var methodsWithSameSignature = mapOfSignatures.getOrDefault(m.method.signature(), new ArrayList<>());
|
var signature = new WeakSignature(m.method.signature());
|
||||||
|
var methodsWithSameSignature = mapOfSignatures.getOrDefault(signature, new ArrayList<>());
|
||||||
methodsWithSameSignature.add(m);
|
methodsWithSameSignature.add(m);
|
||||||
mapOfSignatures.put(m.method.signature(), methodsWithSameSignature);
|
mapOfSignatures.put(signature, methodsWithSameSignature);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var resMethods = new HashSet<TargetMethod>();
|
|
||||||
for (var methodsWithSignature : mapOfSignatures.values()) {
|
for (var methodsWithSignature : mapOfSignatures.values()) {
|
||||||
|
var resMethods = new HashSet<TargetMethod>();
|
||||||
outer: for (var m1 : methodsWithSignature) {
|
outer: for (var m1 : methodsWithSignature) {
|
||||||
for (var m2 : methodsWithSignature) {
|
for (var m2 : methodsWithSignature) {
|
||||||
for (var i = 0; i < m1.args.size(); i++) {
|
for (var i = 0; i < m1.args.size(); i++) {
|
||||||
@ -173,9 +196,9 @@ public class ASTToTargetAST {
|
|||||||
}
|
}
|
||||||
resMethods.add(m1.method);
|
resMethods.add(m1.method);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
res.add(resMethods.stream().toList());
|
res.add(resMethods.stream().toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,7 +332,6 @@ public class ASTToTargetAST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate dispatch method
|
// Generate dispatch method
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -857,8 +857,8 @@ public class TestComplete {
|
|||||||
//@Ignore("Not implemented")
|
//@Ignore("Not implemented")
|
||||||
@Test
|
@Test
|
||||||
public void testOverloadPatternMatching() throws Exception {
|
public void testOverloadPatternMatching() throws Exception {
|
||||||
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PaternMatchingHaskellStyle.jav");
|
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingHaskellStyle.jav");
|
||||||
var clazz = classFiles.get("PaternMatchingHaskellStyle");
|
var clazz = classFiles.get("PatternMatchingHaskellStyle");
|
||||||
var R2 = classFiles.get("Elem");
|
var R2 = classFiles.get("Elem");
|
||||||
var R = classFiles.get("LinkedElem");
|
var R = classFiles.get("LinkedElem");
|
||||||
var I = classFiles.get("List");
|
var I = classFiles.get("List");
|
||||||
|
Loading…
Reference in New Issue
Block a user