Merge branch 'unify' of ssh://gohorb.ba-horb.de/bahome/projekt/git/JavaCompilerCore into refactoring

This commit is contained in:
JanUlrich 2016-04-07 14:39:15 +02:00
commit e537a76cd9
5 changed files with 51 additions and 14 deletions

View File

@ -376,8 +376,8 @@ public class RuleSet implements IRuleSet{
if(!(typeDs instanceof ReferenceType))
return Optional.empty();
if(typeD.getTypeParams().size() == 0 || typeDs.getTypeParams().size() == 0)
return Optional.empty();
/*if(typeD.getTypeParams().size() == 0 || typeDs.getTypeParams().size() == 0)
return Optional.empty();*/
if(typeD.getName().equals(typeDs.getName()))
return Optional.empty();
@ -402,8 +402,8 @@ public class RuleSet implements IRuleSet{
TypeParams typeDParams = typeD.getTypeParams();
TypeParams typeDgenParams = typeDgen.getTypeParams();
Unifier unif = new Unifier((PlaceholderType) typeDgenParams.get(0), typeDParams.get(0));
for(int i = 1; i < typeDParams.size(); i++)
Unifier unif = Unifier.Identity();
for(int i = 0; i < typeDParams.size(); i++)
unif.Add((PlaceholderType) typeDgenParams.get(i), typeDParams.get(i));
return Optional.of(new UnifyPair(unif.apply(newLhs), typeDs, PairOperator.SMALLERDOT));

View File

@ -6,17 +6,28 @@ import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
public class FunNType extends UnifyType {
public FunNType(TypeParams p) {
protected FunNType(TypeParams p) {
super("FuN", p);
if(p.size() == 0)
throw new IllegalArgumentException("Function types need at least one type parameter");
}
public static FunNType getFunNType(TypeParams tp) {
if(!validateTypeParams(tp))
throw new IllegalArgumentException("Invalid TypeParams for a FunNType: " + tp);
return new FunNType(tp);
}
private static boolean validateTypeParams(TypeParams tp) {
if(tp.size() == 0)
return false;
for(UnifyType t : tp)
if(t instanceof WildcardType)
return false;
return true;
}
@Override
public UnifyType setTypeParams(TypeParams newTp) {
if(newTp.size() == 0)
throw new IllegalArgumentException("Function types need at least one type parameter");
return new FunNType(newTp);
return getFunNType(newTp);
}
public int getN() {

View File

@ -4,7 +4,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
public class Node<T> {
class Node<T> {
private T content;
private HashSet<Node<T>> predecessors = new HashSet<>();

View File

@ -45,7 +45,7 @@ public class Unifier implements Function<UnifyType, UnifyType> /*, Set<MPair>*/
return substitutions.containsKey(t);
}
public UnifyType getSubstitute(UnifyType t) {
public UnifyType getSubstitute(PlaceholderType t) {
return substitutions.get(t);
}

View File

@ -380,13 +380,39 @@ public class UnifyTest extends Unify {
//System.out.println(actual);
//Assert.assertEquals(actual, expected);
}
@Test
public void unifyTestComplex() {
/*
* INIT
*/
TypeFactory tf = new TypeFactory();
FiniteClosureBuilder fcb = new FiniteClosureBuilder();
UnifyType tphT = tf.getPlaceholderType("T");
UnifyType vector = tf.getSimpleType("Vector", tphT);
UnifyType number = tf.getSimpleType("Number");
UnifyType integer = tf.getSimpleType("Integer");
UnifyType object = tf.getSimpleType("Object");
UnifyType matrix = tf.getSimpleType("Matrix");
UnifyType vectorvectorint = tf.getSimpleType("Vector", tf.getSimpleType("Vector", integer));
fcb.add(integer, number);
fcb.add(matrix, vectorvectorint);
fcb.add(vector, object);
IFiniteClosure fc = fcb.getFiniteClosure();
Set<UnifyPair> eq = new HashSet<UnifyPair>();
eq.add(new UnifyPair(tf.getSimpleType("Matrix"), tf.getSimpleType("Vector", tf.getPlaceholderType("a")), PairOperator.SMALLERDOT));
Set<Set<UnifyPair>> expected = new HashSet<>();
Set<Set<UnifyPair>> actual = unify(eq, fc);
System.out.println("Test Matrix:");
System.out.println(actual);
}
@Test