From 5f31150dc8ef9d211fcccc2ef6504cffb28762cf Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 12 Jun 2017 18:57:12 +0200 Subject: [PATCH] =?UTF-8?q?TYPE-Algo=20f=C3=BCr=20LambdaExpression=20imple?= =?UTF-8?q?mentieren?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 1 + .../SyntaxTreeGenerator.java | 2 ++ .../typedeployment/TypeInsertFactory.java | 5 ++++ .../typeinference/typeAlgo/TYPE.java | 24 ++++++++++++++++--- test/javFiles/Lambda2.jav | 8 ++++++- test/javFiles/Lambda3.jav | 23 ++++++++++++++++++ test/typeinference/JavaTXCompilerTest.java | 5 ++-- 7 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 test/javFiles/Lambda3.jav diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 0fdcbb77..15430da8 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -63,6 +63,7 @@ public class JavaTXCompiler { System.out.println("RESULT: " + result); results.addAll(result); } + //TODO: Hier läuft irgendwas gewaltig schief: return new ResultSet(UnifyTypeFactory.convert(results, generateTPHMap(cons))); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 0f2f8e51..c16dcc8a 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -233,6 +233,8 @@ public class SyntaxTreeGenerator{ block = stmtGen.convert(body.block()); } 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()); }else{ return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 3af59dc1..e07d299a 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -11,6 +11,11 @@ import de.dhbwstuttgart.typeinference.ResultSet; import java.util.ArrayList; 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 static List createTypeInsertPoints(SourceFile forSourcefile, ResultSet withResults){ List ret = new ArrayList<>(); diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 7a0cad92..ae1c6952 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.typeAlgo; +import com.sun.org.apache.xpath.internal.Arg; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.syntaxtree.*; @@ -34,6 +35,19 @@ public class TYPE implements StatementVisitor{ @Override public void visit(LambdaExpression lambdaExpression) { + List 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 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)); lambdaExpression.methodBody.accept(lambdaScope); constraintsSet.addAll(lambdaScope.getConstraints()); @@ -224,12 +238,12 @@ public class TYPE implements StatementVisitor{ } - public static List getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { + public static List getMethods(String name, int numArgs, TypeInferenceBlockInformation info) { List ret = new ArrayList<>(); for(ClassOrInterface cl : info.getAvailableClasses()){ for(Method m : cl.getMethods()){ if(m.getName().equals(name) && - m.getParameterList().getFormalparalist().size() == arglist.getArguments().size()){ + m.getParameterList().getFormalparalist().size() == numArgs){ RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); ret.add(new MethodAssumption(cl.getType(), retType, convertParams(m.getParameterList(),info))); @@ -239,7 +253,11 @@ public class TYPE implements StatementVisitor{ return ret; } - protected static List convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ + public static List getMethods(String name, ArgumentList arglist, TypeInferenceBlockInformation info) { + return getMethods(name, arglist.getArguments().size(), info); + } + + protected static List convertParams(ParameterList parameterList, TypeInferenceBlockInformation info){ List params = new ArrayList<>(); for(FormalParameter fp : parameterList.getFormalparalist()){ params.add(info.checkGTV(fp.getType())); diff --git a/test/javFiles/Lambda2.jav b/test/javFiles/Lambda2.jav index 8326dcd5..b0ef8221 100644 --- a/test/javFiles/Lambda2.jav +++ b/test/javFiles/Lambda2.jav @@ -4,8 +4,14 @@ public class Lambda2 public static void main(List args){ auto listOfStrings = new List(); auto listOfObjects; - //listOfObjects = map(listOfStrings, (a) -> a); + listOfObjects = map(listOfStrings, (a) -> a); } + +public map(a , b){ + b.apply(a); + return a; +} + /* public static List map(List input, Function func) { List output; diff --git a/test/javFiles/Lambda3.jav b/test/javFiles/Lambda3.jav new file mode 100644 index 00000000..9c4e960c --- /dev/null +++ b/test/javFiles/Lambda3.jav @@ -0,0 +1,23 @@ + +public class Lambda2 +{ + /* + public static List map(List input, + Function func){ + input.add(func.apply(input.get())); + } + */ + public map(input,func){ + input.add(func.apply(input.get())); + return map(new List(), func); + } +} + +class List{ + A get(); + void add(A); +} + +class Function{ + B apply(A a); +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index d710174b..4bd02ba6 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -25,11 +25,12 @@ public class JavaTXCompilerTest { @Test 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+"Generics.jav")); //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); - //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav")); JavaTXCompiler compiler = new JavaTXCompiler(); for(File f : filesToTest){