TYPE-Algo für LambdaExpression implementieren

This commit is contained in:
JanUlrich 2017-06-12 18:57:12 +02:00
parent 2129299eed
commit 5f31150dc8
7 changed files with 62 additions and 6 deletions

View File

@ -63,6 +63,7 @@ public class JavaTXCompiler {
System.out.println("RESULT: " + result); System.out.println("RESULT: " + result);
results.addAll(result); results.addAll(result);
} }
//TODO: Hier läuft irgendwas gewaltig schief:
return new ResultSet(UnifyTypeFactory.convert(results, generateTPHMap(cons))); return new ResultSet(UnifyTypeFactory.convert(results, generateTPHMap(cons)));
} }

View File

@ -233,6 +233,8 @@ public class SyntaxTreeGenerator{
block = stmtGen.convert(body.block()); block = stmtGen.convert(body.block());
} }
if(parentClass.equals(new JavaClassName(name))){ if(parentClass.equals(new JavaClassName(name))){
//TODO: Constructor darf nicht Rückgabetyp void bekommen: Hier als Rückgabetyp die Klasse inklusive generische Variablen
//retType = TypeGenerator.convertTypeName(name, gtvDeclarations, header.getStart(), reg, localGenerics);
return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart()); return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart());
}else{ }else{
return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart());

View File

@ -11,6 +11,11 @@ import de.dhbwstuttgart.typeinference.ResultSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* TODO:
* Falls in Feldern Generics entstehen, dann werden diese als Klassenparameter eingesetzt
* Für die Instanzierung von Klassen kann man dann beispielsweise nur noch den Diamond-Operator verwenden
*/
public class TypeInsertFactory { public class TypeInsertFactory {
public static List<TypeInsertPoint> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){ public static List<TypeInsertPoint> createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){
List<TypeInsertPoint> ret = new ArrayList<>(); List<TypeInsertPoint> ret = new ArrayList<>();

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.typeinference.typeAlgo; package de.dhbwstuttgart.typeinference.typeAlgo;
import com.sun.org.apache.xpath.internal.Arg;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.exceptions.TypeinferenceException;
import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.*;
@ -34,6 +35,19 @@ public class TYPE implements StatementVisitor{
@Override @Override
public void visit(LambdaExpression lambdaExpression) { public void visit(LambdaExpression lambdaExpression) {
List<MethodAssumption> methodAssumptionss = getMethods("apply", lambdaExpression.params.getFormalparalist().size(), info);
//TODO: Nur FunN-Interface als mögliche Typen verwenden
//methodAssumptionss.stream().filter((methodAssumption -> methodAssumption.getReceiverType().getName().toString()))
Set<Constraint> possibleLambdaTypes = new HashSet<>();
for(MethodAssumption mAss : methodAssumptionss){
Constraint cons = new Constraint();
cons.add(
ConstraintsFactory.createPair(lambdaExpression.methodBody.getType(),mAss.getReturnType(),info));
cons.add(
ConstraintsFactory.createPair(lambdaExpression.getType(),mAss.getReceiverType(),PairOperator.EQUALSDOT, info));
}
constraintsSet.addOderConstraint(possibleLambdaTypes);
//Constraints des Bodys generieren:
TYPE lambdaScope = new TYPE(new TypeInferenceBlockInformation(info, lambdaExpression)); TYPE lambdaScope = new TYPE(new TypeInferenceBlockInformation(info, lambdaExpression));
lambdaExpression.methodBody.accept(lambdaScope); lambdaExpression.methodBody.accept(lambdaScope);
constraintsSet.addAll(lambdaScope.getConstraints()); constraintsSet.addAll(lambdaScope.getConstraints());
@ -224,12 +238,12 @@ public class TYPE implements StatementVisitor{
} }
public static List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { public static List<MethodAssumption> getMethods(String name, int numArgs, TypeInferenceBlockInformation info) {
List<MethodAssumption> ret = new ArrayList<>(); List<MethodAssumption> ret = new ArrayList<>();
for(ClassOrInterface cl : info.getAvailableClasses()){ for(ClassOrInterface cl : info.getAvailableClasses()){
for(Method m : cl.getMethods()){ for(Method m : cl.getMethods()){
if(m.getName().equals(name) && if(m.getName().equals(name) &&
m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){ m.getParameterList().getFormalparalist().size() == numArgs){
RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType());
ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info)));
@ -239,6 +253,10 @@ public class TYPE implements StatementVisitor{
return ret; return ret;
} }
public static List<MethodAssumption> getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) {
return getMethods(name, arglist.getArguments().size(), info);
}
protected static List<RefTypeOrTPHOrWildcardOrGeneric> convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ protected static List<RefTypeOrTPHOrWildcardOrGeneric> convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){
List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> params = new ArrayList<>();
for(FormalParameter fp : parameterList.getFormalparalist()){ for(FormalParameter fp : parameterList.getFormalparalist()){

View File

@ -4,8 +4,14 @@ public class Lambda2
public static void main(List<String> args){ public static void main(List<String> args){
auto listOfStrings = new List<String>(); auto listOfStrings = new List<String>();
auto listOfObjects; auto listOfObjects;
//listOfObjects = map(listOfStrings, (a) -> a); listOfObjects = map(listOfStrings, (a) -> a);
} }
public map(a , b){
b.apply(a);
return a;
}
/* /*
public static <I,O> List<O> map(List<I> input, Function<I,O> func) { public static <I,O> List<O> map(List<I> input, Function<I,O> func) {
List<O> output; List<O> output;

23
test/javFiles/Lambda3.jav Normal file
View File

@ -0,0 +1,23 @@
public class Lambda2
{
/*
public static <A> List<A> map(List<? extends A> input,
Function<? super A, ? extends A> func){
input.add(func.apply(input.get()));
}
*/
public map(input,func){
input.add(func.apply(input.get()));
return map(new List<String>(), func);
}
}
class List<A>{
A get();
void add(A);
}
class Function<A,B>{
B apply(A a);
}

View File

@ -25,11 +25,12 @@ public class JavaTXCompilerTest {
@Test @Test
public void test() throws IOException, ClassNotFoundException { public void test() throws IOException, ClassNotFoundException {
filesToTest.add(new File(rootDirectory+"Lambda2.jav")); filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda2.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda3.jav"));
//filesToTest.add(new File(rootDirectory+"Vector.jav")); //filesToTest.add(new File(rootDirectory+"Vector.jav"));
//filesToTest.add(new File(rootDirectory+"Generics.jav")); //filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Lambda.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(); JavaTXCompiler compiler = new JavaTXCompiler();
for(File f : filesToTest){ for(File f : filesToTest){