From 01339ca7ec7ec368cf9e6a3fee5e481fc19459f9 Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Thu, 28 Dec 2017 10:20:28 +0100 Subject: [PATCH 01/17] Descriptor-Erzeugen neu mit Visitor-pattern implementiert --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 16 ++- .../bytecode/BytecodeGenMethod.java | 36 +++--- src/de/dhbwstuttgart/bytecode/Descriptor.java | 112 ------------------ .../bytecode/DescriptorToString.java | 85 +++++++++++++ .../bytecode/DescriptorVisitor.java | 9 ++ src/de/dhbwstuttgart/bytecode/Lambda.java | 25 ++++ .../bytecode/MethodFromMethodCall.java | 18 +++ .../bytecode/NormalConstructor.java | 20 ++++ .../dhbwstuttgart/bytecode/NormalMethod.java | 29 +++++ src/de/dhbwstuttgart/bytecode/SamMethod.java | 31 +++++ .../bytecode/TypeToDescriptor.java | 37 ++++++ test/bytecode/JavaTXCompilerTest.java | 2 +- 12 files changed, 285 insertions(+), 135 deletions(-) delete mode 100644 src/de/dhbwstuttgart/bytecode/Descriptor.java create mode 100644 src/de/dhbwstuttgart/bytecode/DescriptorToString.java create mode 100644 src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java create mode 100644 src/de/dhbwstuttgart/bytecode/Lambda.java create mode 100644 src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java create mode 100644 src/de/dhbwstuttgart/bytecode/NormalConstructor.java create mode 100644 src/de/dhbwstuttgart/bytecode/NormalMethod.java create mode 100644 src/de/dhbwstuttgart/bytecode/SamMethod.java create mode 100644 src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 6f7acd64..1b6ef47b 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -88,11 +88,12 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(Constructor field) { - Descriptor desc = new Descriptor(field, resultSet); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc.getDesc(), null, null); + NormalConstructor constructor = new NormalConstructor(field); + String desc = constructor.accept(new DescriptorToString(resultSet)); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, null, null); mv.visitCode(); System.out.println("-----Constructor-----"); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,desc.getDesc(),cw,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,desc,cw,isInterface); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(0, 0); @@ -104,12 +105,15 @@ public class BytecodeGen implements ASTVisitor { // TODO: check if the method is static => if static then the first param will be stored in pos 0 // else it will be stored in pos 1 and this will be stored in pos 0 method.getParameterList().accept(this); - Descriptor methDesc = new Descriptor(method,resultSet); + + NormalMethod meth = new NormalMethod(method); + String methDesc = meth.accept(new DescriptorToString(resultSet)); + System.out.println("-----Method-----"); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc.getDesc(), null, null); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc, null, null); mv.visitCode(); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,methDesc.getDesc(),cw,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,methDesc,cw,isInterface); mv.visitMaxs(0, 0); mv.visitEnd(); } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index c2acaba0..ffdf1450 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -94,7 +94,8 @@ public class BytecodeGenMethod implements StatementVisitor{ } private String getResolvedType(RefTypeOrTPHOrWildcardOrGeneric type) { - return resultSet.resolveType(type).resolvedType.toString().replace(".", "/"); +// return resultSet.resolveType(type).resolvedType.toString().replace(".", "/"); + return resultSet.resolveType(type).resolvedType.acceptTV(new TypeToDescriptor()); } @@ -164,7 +165,9 @@ public class BytecodeGenMethod implements StatementVisitor{ public void visit(LambdaExpression lambdaExpression) { System.out.println("\n++ In Lambda: "); this.lamCounter++; - Descriptor lamDesc = new Descriptor(lambdaExpression, resultSet); + + Lambda lam = new Lambda(lambdaExpression); + String lamDesc = lam.accept(new DescriptorToString(resultSet)); //Call site, which, when invoked, returns an instance of the functional interface to which //the lambda is being converted MethodType mt = MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, @@ -175,9 +178,9 @@ public class BytecodeGenMethod implements StatementVisitor{ "metafactory", mt.toMethodDescriptorString(), false); String methodName = "lambda$new$" + this.lamCounter; // Type erasure - Type arg1 = Type.getMethodType(lamDesc.getDesc()); + Type arg1 = Type.getMethodType(lamDesc); // real Type - Type arg3 = Type.getMethodType(lamDesc.getDesc()); + Type arg3 = Type.getMethodType(lamDesc); int staticOrSpecial=0; int staticOrInstance=0; @@ -197,12 +200,12 @@ public class BytecodeGenMethod implements StatementVisitor{ Handle arg2 = new Handle(staticOrSpecial, this.className, methodName, arg3.toString(),false); // Descriptor of functional interface methode - Descriptor fiMethodDesc = new Descriptor(kindOfLambda.getArgumentList(), lambdaExpression.getType(),resultSet); - + SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType()); // Desc: (this/nothing)TargetType - mv.visitInvokeDynamicInsn("apply", fiMethodDesc.getDesc(), bootstrap, + String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet)); + mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2,arg3); - + MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ staticOrInstance + Opcodes.ACC_SYNTHETIC, methodName, arg3.toString(), null, null); @@ -274,17 +277,18 @@ public class BytecodeGenMethod implements StatementVisitor{ methodCall.receiver.accept(this); methodCall.arglist.accept(this); - Descriptor mDesc = new Descriptor(methodCall.arglist, methodCall.getType(),resultSet); + MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType()); + String mDesc = method.accept(new DescriptorToString(resultSet)); System.out.println("is Vars empty: "+varsFunInterface.isEmpty()); // is methodCall.receiver functional Interface)? if(varsFunInterface.contains(methodCall.receiver.getType())) { mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, getResolvedType(methodCall.receiver.getType()), - methodCall.name, mDesc.getDesc(), false); + methodCall.name, mDesc, false); }else { mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()), - methodCall.name, mDesc.getDesc(), isInterface); + methodCall.name, mDesc, isInterface); } // test // if(!methodCall.getType().toString().equals("V")) { @@ -303,7 +307,7 @@ public class BytecodeGenMethod implements StatementVisitor{ methodCall.arglist.accept(this); String d = "("; for(Expression e : methodCall.arglist.getArguments()) { - d = d + "L"+e.getType().toString().replace(".", "/") + ";"; + d = d + "L"+getResolvedType(e.getType()) + ";"; } d += ")V"; @@ -339,7 +343,7 @@ public class BytecodeGenMethod implements StatementVisitor{ System.out.println("In StaticClassName: "); // mv.visitMethodInsn(Opcodes.INVOKESTATIC, staticClassName.getType().toString().replace(".", "/"), // staticClassName.toString(), staticClassName.getType().toString(), false); - mv.visitFieldInsn(Opcodes.GETSTATIC, staticClassName.getType().toString().replace(".", "/"), + mv.visitFieldInsn(Opcodes.GETSTATIC, getResolvedType(staticClassName.getType()), fieldName, fieldDesc); } @@ -379,7 +383,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Literal literal) { // value? - mv.visitLdcInsn(literal.getType().toString()); + mv.visitLdcInsn(getResolvedType(literal.getType())); } @Override @@ -399,8 +403,8 @@ public class BytecodeGenMethod implements StatementVisitor{ // array slot onto the top of the operand stack. assignLeftSide.field.receiver.accept(this); this.rightSideTemp.accept(this); - mv.visitFieldInsn(Opcodes.PUTFIELD, assignLeftSide.field.receiver.getType().toString(), - assignLeftSide.field.fieldVarName, assignLeftSide.field.getType().toString()); + mv.visitFieldInsn(Opcodes.PUTFIELD, getResolvedType(assignLeftSide.field.receiver.getType()), + assignLeftSide.field.fieldVarName, getResolvedType(assignLeftSide.field.getType())); } @Override diff --git a/src/de/dhbwstuttgart/bytecode/Descriptor.java b/src/de/dhbwstuttgart/bytecode/Descriptor.java deleted file mode 100644 index 3b024d71..00000000 --- a/src/de/dhbwstuttgart/bytecode/Descriptor.java +++ /dev/null @@ -1,112 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -import java.util.List; -import java.util.Iterator; - -import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.syntaxtree.Constructor; -import de.dhbwstuttgart.syntaxtree.FormalParameter; -import de.dhbwstuttgart.syntaxtree.Method; -import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; -import de.dhbwstuttgart.syntaxtree.statement.Expression; -import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.typeinference.result.ResultSet; - -public class Descriptor { - String desc; - - public Descriptor(Method method, ResultSet resultSet) { - desc = "("; - Iterator itr = method.getParameterList().iterator(); - while(itr.hasNext()) { - FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; - } - desc = addReturnType(desc,method.getReturnType(), resultSet); - - - } - - private class TypeToDescriptor implements TypeVisitor{ - - @Override - public String visit(RefType refType) { - return refType.getName().toString().replace(".", "/"); - } - - @Override - public String visit(SuperWildcardType superWildcardType) { - throw new NotImplementedException(); - } - - @Override - public String visit(TypePlaceholder typePlaceholder) { - return typePlaceholder.toString(); - } - - @Override - public String visit(ExtendsWildcardType extendsWildcardType) { - throw new NotImplementedException(); - } - - @Override - public String visit(GenericRefType genericRefType) { - return genericRefType.getParsedName(); - } - } - - private String addReturnType(String desc2, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) { - System.out.println("DescType = "+returnType.toString()); - if(resultSet.resolveType(returnType).resolvedType.toString().equals("void")){ - desc = desc + ")V"; - }else { - desc = desc + ")" + "L"+resultSet.resolveType(returnType).resolvedType.toString().replace(".", "/")+";"; - } - return desc; - } - - public Descriptor(Constructor constructor, ResultSet resultSet) { - desc = "("; - Iterator itr = constructor.getParameterList().iterator(); - while(itr.hasNext()) { - FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "/") + ";"; - } - desc = desc + ")V"; - } - - public Descriptor(LambdaExpression lambdaExpr, ResultSet resultSet) { - desc = "("; - Iterator itr = lambdaExpr.params.iterator(); - while(itr.hasNext()) { - FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "/") + ";"; - } - desc = addReturnType(desc, lambdaExpr.getReturnType(), resultSet); - } - - public Descriptor(ArgumentList argList, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) { - desc = "("; - for(Expression e : argList.getArguments()) { - desc = desc + "L"+resultSet.resolveType(e.getType()).resolvedType.toString().replace(".", "/") + ";"; - } - desc = addReturnType(desc, returnType, resultSet); - - } - - public Descriptor(List argumentList,RefTypeOrTPHOrWildcardOrGeneric returnType ,ResultSet resultSet) { - desc = "("; - Iterator itr = argumentList.iterator(); - while(itr.hasNext()) { - RefTypeOrTPHOrWildcardOrGeneric rt = itr.next(); - desc = desc + "L"+resultSet.resolveType(rt).resolvedType.toString().replace(".", "/")+";"; - } - desc = desc + ")"+"L"+resultSet.resolveType(returnType).resolvedType.toString().replace(".", "/")+";"; - } - - public String getDesc() { - return this.desc; - } - -} diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java new file mode 100644 index 00000000..044218e4 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java @@ -0,0 +1,85 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.Iterator; + +import de.dhbwstuttgart.syntaxtree.FormalParameter; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class DescriptorToString implements DescriptorVisitor{ + ResultSet resultSet; + + public DescriptorToString(ResultSet resultSet) { + this.resultSet = resultSet; + } + + private String addReturnType(String desc, RefTypeOrTPHOrWildcardOrGeneric returnType, ResultSet resultSet) { + if(resultSet.resolveType(returnType).resolvedType.toString().equals("void")){ + desc = desc + ")V"; + }else { + desc = desc + ")" + "L"+resultSet.resolveType(returnType).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + return desc; + } + + @Override + public String visit(NormalMethod method) { + + String desc = "("; + Iterator itr = method.getParameterList().iterator(); + while(itr.hasNext()) { + FormalParameter fp = itr.next(); + desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + desc = addReturnType(desc,method.getReturnType(), resultSet); + return desc; + } + + @Override + public String visit(NormalConstructor constructor) { + String desc = "("; + Iterator itr = constructor.getParameterList().iterator(); + while(itr.hasNext()) { + FormalParameter fp = itr.next(); + desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + desc = desc + ")V"; + return desc; + } + + @Override + public String visit(Lambda lambdaExpression) { + String desc = "("; + Iterator itr = lambdaExpression.getParams().iterator(); + while(itr.hasNext()) { + FormalParameter fp = itr.next(); + desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()) + ";"; + } + desc = addReturnType(desc, lambdaExpression.getReturnType(), resultSet); + return desc; + } + + @Override + public String visit(SamMethod samMethod) { + String desc = "("; + Iterator itr = samMethod.getArgumentList().iterator(); + while(itr.hasNext()) { + RefTypeOrTPHOrWildcardOrGeneric rt = itr.next(); + desc = desc + "L"+resultSet.resolveType(rt).resolvedType.acceptTV(new TypeToDescriptor())+";"; + } + desc = desc + ")"+"L"+resultSet.resolveType(samMethod.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+";"; + return desc; + } + + @Override + public String visit(MethodFromMethodCall methodFromMethodCall) { + String desc = "("; + for(Expression e : methodFromMethodCall.argList.getArguments()) { + desc = desc + "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + desc = addReturnType(desc, methodFromMethodCall.returnType, resultSet); + return desc; + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java b/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java new file mode 100644 index 00000000..63198828 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java @@ -0,0 +1,9 @@ +package de.dhbwstuttgart.bytecode; + +public interface DescriptorVisitor { + public String visit(NormalMethod method); + public String visit(NormalConstructor constructor); + public String visit(Lambda lambdaExpression); + public String visit(SamMethod samMethod); + public String visit(MethodFromMethodCall methodFromMethodCall); +} diff --git a/src/de/dhbwstuttgart/bytecode/Lambda.java b/src/de/dhbwstuttgart/bytecode/Lambda.java new file mode 100644 index 00000000..283b7393 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/Lambda.java @@ -0,0 +1,25 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class Lambda { + private LambdaExpression lambdaExpression; + + public Lambda(LambdaExpression lambdaExpression) { + this.lambdaExpression = lambdaExpression; + } + + public ParameterList getParams() { + return lambdaExpression.params; + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return lambdaExpression.getReturnType(); + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java new file mode 100644 index 00000000..034e2abf --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java @@ -0,0 +1,18 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class MethodFromMethodCall { + ArgumentList argList; + RefTypeOrTPHOrWildcardOrGeneric returnType; + + public MethodFromMethodCall(ArgumentList argList,RefTypeOrTPHOrWildcardOrGeneric returnType) { + this.argList = argList; + this.returnType = returnType; + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java new file mode 100644 index 00000000..d029d21e --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.ParameterList; + +public class NormalConstructor { + private Constructor constructor; + + public NormalConstructor(Constructor constructor) { + this.constructor = constructor; + } + + public ParameterList getParameterList() { + return constructor.getParameterList(); + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java new file mode 100644 index 00000000..2dd81d78 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -0,0 +1,29 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.ParameterList; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class NormalMethod { + private Method method; + + public NormalMethod(Method method) { + this.method = method; + } + + public Method getMethod() { + return method; + } + + public ParameterList getParameterList() { + return method.getParameterList(); + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return method.getType(); + } + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/SamMethod.java b/src/de/dhbwstuttgart/bytecode/SamMethod.java new file mode 100644 index 00000000..9cf039a2 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/SamMethod.java @@ -0,0 +1,31 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.List; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; + +public class SamMethod { + private List argumentList; + private RefTypeOrTPHOrWildcardOrGeneric returnType; + + public SamMethod(List argumentList, RefTypeOrTPHOrWildcardOrGeneric returnType) { + this.argumentList = argumentList; + this.returnType = returnType; + } + + public List getArgumentList() { + return argumentList; + } + + + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return returnType; + } + + + + public String accept(DescriptorVisitor descVisitor) { + return descVisitor.visit(this); + } +} diff --git a/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java b/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java new file mode 100644 index 00000000..5271e63d --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java @@ -0,0 +1,37 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; + +public class TypeToDescriptor implements TypeVisitor{ + + @Override + public String visit(RefType refType) { + return refType.getName().toString().replace(".", "/"); + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + return typePlaceholder.toString().replace(".", "/"); + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + return genericRefType.getParsedName().replace(".", "/"); + } +} \ No newline at end of file diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index f43a0740..39ff5084 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -29,7 +29,7 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, java.lang.ClassNotFoundException { System.out.println(rootDirectory); - String fileName = "Generics"; + String fileName = "Faculty"; filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); From 3c732346d907ae5b59e7aece1975fa29ff40baeb Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sat, 6 Jan 2018 09:38:53 +0100 Subject: [PATCH 02/17] ASPParameterlist erzeugt auch paramNum --- pom.xml | 5 +++++ src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/pom.xml b/pom.xml index 6b0f2fba..9cce2d99 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,11 @@ antlr4 4.7 + + commons-io + commons-io + 2.6 + com.google.guava guava diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java index f0c06744..46eed10a 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java @@ -11,11 +11,13 @@ import java.util.Set; public class ASPParameterList { private final static String ASP_PARAMLIST_NAME = "param"; + private final static String ASP_PARAMLISTNUMERATION_NAME = "paramNum"; private final static String ASP_PARAMLIST_END_POINTER = "null"; public final String name; private final List types; public ASPParameterList(List types, ASPWriter writer){ + int paramNum = 0; this.types = types; if(types.size() == 0){ name = ASP_PARAMLIST_END_POINTER; @@ -30,6 +32,8 @@ public class ASPParameterList { if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; param += nextPointer; writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); + writer.add(new ASPStatement(ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); + paramNum++; //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); } } From 19a1ef40244e76c117a7f0ad3bab406bb7f54733 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 10:53:07 +0100 Subject: [PATCH 03/17] =?UTF-8?q?Erste=20lauff=C3=A4hige=20aber=20unvollst?= =?UTF-8?q?=C3=A4ndige=20Version=20des=20UnifyWithoutWildcards?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/sat/asp/Clingo.java | 46 +++++++++++++++++++ .../dhbwstuttgart/sat/asp/model/ASPRule.java | 24 ++++++++++ .../dhbwstuttgart/sat/asp/model/ASPType.java | 4 -- .../sat/asp/parser/ASPParser.java | 17 +++++++ .../asp/parser/model/ParsedASPStatement.java | 7 +++ .../sat/asp/{ => writer}/ASPGenerator.java | 8 +--- .../sat/asp/{ => writer}/ASPWriter.java | 6 +-- .../{ => writer}/model/ASPGenericType.java | 7 +-- .../sat/asp/{ => writer}/model/ASPPair.java | 2 +- .../asp/{ => writer}/model/ASPPairEquals.java | 7 +-- .../{ => writer}/model/ASPPairSmaller.java | 11 +++-- .../{ => writer}/model/ASPPairSmallerDot.java | 7 +-- .../{ => writer}/model/ASPParameterList.java | 20 ++++---- .../asp/{ => writer}/model/ASPRefType.java | 7 +-- .../asp/{ => writer}/model/ASPStatement.java | 2 +- .../sat/asp/writer/model/ASPType.java | 4 ++ .../asp/{ => writer}/model/ASPTypeVar.java | 2 +- test/asp/ClingoTest.java | 46 +++++++++++++++++++ test/asp/typeinference/ASPTest.java | 2 +- test/asp/unifywithoutwildcards/ASPTests.java | 10 ++++ test/typeinference/JavaTXCompilerTest.java | 15 ------ 21 files changed, 193 insertions(+), 61 deletions(-) create mode 100644 src/de/dhbwstuttgart/sat/asp/Clingo.java create mode 100644 src/de/dhbwstuttgart/sat/asp/model/ASPRule.java delete mode 100644 src/de/dhbwstuttgart/sat/asp/model/ASPType.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java create mode 100644 src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java rename src/de/dhbwstuttgart/sat/asp/{ => writer}/ASPGenerator.java (94%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/ASPWriter.java (67%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPGenericType.java (53%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPair.java (90%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPairEquals.java (55%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPairSmaller.java (51%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPPairSmallerDot.java (55%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPParameterList.java (59%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPRefType.java (68%) rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPStatement.java (91%) create mode 100644 src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java rename src/de/dhbwstuttgart/sat/asp/{ => writer}/model/ASPTypeVar.java (83%) create mode 100644 test/asp/ClingoTest.java create mode 100644 test/asp/unifywithoutwildcards/ASPTests.java diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java new file mode 100644 index 00000000..85f6dc56 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -0,0 +1,46 @@ +package de.dhbwstuttgart.sat.asp; + + +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Clingo { + private final List input; + private static final List programFiles = new ArrayList<>(); + static{ + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/basis.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/subst.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce1.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce2.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); + } + + public Clingo(List inputFiles){ + this.input = inputFiles; + } + + public String runClingo() throws IOException, InterruptedException { + String pathToClingo = + "/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/clingo-5.2.1-linux-x86_64/clingo"; + List commands = new ArrayList<>(); + commands.add(pathToClingo); + commands.add("--outf=2"); //use JSON-Output + for(File file : input){ + commands.add(file.getPath()); + } + commands.addAll(programFiles.stream().map(f->f.getPath()).collect(Collectors.toList())); + Process clingo = new ProcessBuilder( commands.toArray(new String[0])).start(); + InputStream output = clingo.getInputStream(); + clingo.waitFor(); + String result = IOUtils.toString(output, StandardCharsets.UTF_8); + return result; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java new file mode 100644 index 00000000..41237547 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -0,0 +1,24 @@ +package de.dhbwstuttgart.sat.asp.model; + +public enum ASPRule { + ASP_GENERIC_TYPE_NAME("genericType"), + ASP_PAIR_EQUALS_NAME("equals"), + ASP_PAIR_SMALLER_NAME("smaller"), + ASP_PAIR_SMALLER_DOT_NAME("smallerDot"), + ASP_PARAMLIST_NAME("param"), + ASP_PARAMLISTNUMERATION_NAME("paramNum"), + ASP_PARAMLIST_END_POINTER("null"), + ASP_TYPE("type") + ; + + private final String text; + + private ASPRule(final String text) { + this.text = text; + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPType.java deleted file mode 100644 index e703596c..00000000 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.dhbwstuttgart.sat.asp.model; - -public interface ASPType { -} diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java new file mode 100644 index 00000000..c7be7dbc --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -0,0 +1,17 @@ +package de.dhbwstuttgart.sat.asp.parser; + +import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +import java.util.HashSet; +import java.util.Set; + +public class ASPParser { + ResultSet parse(String result){ + Set ret = new HashSet<>(); + for(String pair : result.split(",")){ + + } + return new ResultSet(ret); + } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java new file mode 100644 index 00000000..614bddb0 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.sat.asp.parser.model; + +public class ParsedASPStatement { + public ParsedASPStatement(String statement){ + + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java similarity index 94% rename from src/de/dhbwstuttgart/sat/asp/ASPGenerator.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index 1b942ad0..002e3c7f 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -1,9 +1,8 @@ -package de.dhbwstuttgart.sat.asp; +package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.model.*; +import de.dhbwstuttgart.sat.asp.writer.model.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; @@ -11,13 +10,10 @@ import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.UnifyType; -import java.sql.Ref; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Optional; public class ASPGenerator { ASPWriter writer = new ASPWriter(); diff --git a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java similarity index 67% rename from src/de/dhbwstuttgart/sat/asp/ASPWriter.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index 4d33cbbf..450950d9 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,8 +1,6 @@ -package de.dhbwstuttgart.sat.asp; +package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.sat.asp.model.ASPRefType; -import de.dhbwstuttgart.sat.asp.model.ASPStatement; -import de.dhbwstuttgart.sat.asp.model.ASPType; +import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; import java.util.HashSet; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java similarity index 53% rename from src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java index 26e6ea02..57619edf 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java @@ -1,7 +1,8 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPGenericType implements ASPType{ - public static final String ASP_GENERIC_TYPE_NAME = "genericType"; private final String name; public ASPGenericType(String name){ @@ -9,6 +10,6 @@ public class ASPGenericType implements ASPType{ } public String toString(){ - return ASP_GENERIC_TYPE_NAME + "(" + name + ")"; + return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java similarity index 90% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPair.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java index bbd3ff3f..662f995c 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public abstract class ASPPair { public final ASPType leftSide; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java similarity index 55% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java index b67920bd..8806f2ce 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java @@ -1,13 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPPairEquals extends ASPPair{ - private final static String ASP_PAIR_EQUALS_NAME = "equals"; public ASPPairEquals(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_EQUALS_NAME; + return ASPRule.ASP_PAIR_EQUALS_NAME.toString(); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java similarity index 51% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java index 882054cc..f57170f6 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java @@ -1,13 +1,16 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; + +import java.util.Map; public class ASPPairSmaller extends ASPPair{ - private final static String ASP_PAIR_SMALLER_NAME = "smaller"; public ASPPairSmaller(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_SMALLER_NAME; + return ASPRule.ASP_PAIR_SMALLER_NAME.toString(); } -} +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java similarity index 55% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java index 0e6598c1..e8482bfc 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java @@ -1,13 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPPairSmallerDot extends ASPPair{ - private final static String ASP_PAIR_SMALLER_NAME = "smallerDot"; public ASPPairSmallerDot(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_SMALLER_NAME; + return ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java similarity index 59% rename from src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java index 46eed10a..3c176e49 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java @@ -1,18 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; -import de.dhbwstuttgart.sat.asp.ASPGenerator; -import de.dhbwstuttgart.sat.asp.ASPWriter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPWriter; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; public class ASPParameterList { - private final static String ASP_PARAMLIST_NAME = "param"; - private final static String ASP_PARAMLISTNUMERATION_NAME = "paramNum"; - private final static String ASP_PARAMLIST_END_POINTER = "null"; public final String name; private final List types; @@ -20,7 +16,7 @@ public class ASPParameterList { int paramNum = 0; this.types = types; if(types.size() == 0){ - name = ASP_PARAMLIST_END_POINTER; + name = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); }else{ name = newName(); String nextPointer = name; @@ -29,10 +25,10 @@ public class ASPParameterList { ASPType t = it.next(); String param = nextPointer + "," + t.toString() + ","; nextPointer = newName(); - if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; + if(! it.hasNext())nextPointer = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); param += nextPointer; - writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); - writer.add(new ASPStatement(ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLIST_NAME + "(" + param + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); paramNum++; //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java similarity index 68% rename from src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java index ba318876..6491ff33 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java @@ -1,7 +1,8 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPRefType implements ASPType { - public static final String ASP_TYPE = "type"; private final ASPParameterList params; private final String name; @@ -15,6 +16,6 @@ public class ASPRefType implements ASPType { } public String toString(){ - return ASP_TYPE + "(" + name +"," + params.name + ")"; + return ASPRule.ASP_TYPE + "(" + name +"," + params.name + ")"; } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java similarity index 91% rename from src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java index 8e4a1d35..76bb63e8 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public class ASPStatement { private final String stmt; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java new file mode 100644 index 00000000..da694218 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.sat.asp.writer.model; + +public interface ASPType { +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java similarity index 83% rename from src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java index e2f87636..4b07d30b 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public class ASPTypeVar implements ASPType{ private final String name; diff --git a/test/asp/ClingoTest.java b/test/asp/ClingoTest.java new file mode 100644 index 00000000..dbfa70d8 --- /dev/null +++ b/test/asp/ClingoTest.java @@ -0,0 +1,46 @@ +package asp; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.io.*; +import java.util.*; + +public class ClingoTest { + public static final String rootDirectory = "~/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards"; + public static final String tempDirectory = "/tmp/"; + + @Test + public void test() throws IOException, InterruptedException { + String content = ""; + content = new ASPGenerator(this.getPairs(), this.getFC()).getASP(); + + PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); + writer.println(content); + writer.close(); + + Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); + System.out.println(clingo.runClingo()); + } + + public Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createObjectClass()); + ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } + + public ConstraintSet getPairs() { + ConstraintSet ret = new ConstraintSet<>(); + ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); + return ret; + } +} diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index 5074e4f3..b41d9405 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -1,7 +1,7 @@ package asp.typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.sat.asp.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; diff --git a/test/asp/unifywithoutwildcards/ASPTests.java b/test/asp/unifywithoutwildcards/ASPTests.java new file mode 100644 index 00000000..80e7bb78 --- /dev/null +++ b/test/asp/unifywithoutwildcards/ASPTests.java @@ -0,0 +1,10 @@ +package asp.unifywithoutwildcards; + +import org.junit.Test; + +public class ASPTests { + @Test + public void test(){ + + } +} diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index fbbed9af..f4622954 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -1,29 +1,15 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.ASPGenerator; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; -import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResultSet; -import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; -import de.dhbwstuttgart.typeinference.unify.TypeUnify; -import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import org.junit.Test; import java.io.File; import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -32,7 +18,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; public class JavaTXCompilerTest { From 268056542b7e3ac368cf9057ceecd679c3a0d62d Mon Sep 17 00:00:00 2001 From: Fayez Abu Alia Date: Wed, 10 Jan 2018 11:36:29 +0100 Subject: [PATCH 04/17] =?UTF-8?q?erzeugt=20bytecode=20f=C3=BCr=20generics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dhbwstuttgart/bytecode/BytecodeGen.java | 122 ++++++++++++-- .../bytecode/BytecodeGenMethod.java | 47 ++++-- .../bytecode/DescriptorToString.java | 73 ++++++++- .../bytecode/MethodFromMethodCall.java | 29 +++- .../bytecode/NormalConstructor.java | 21 ++- .../dhbwstuttgart/bytecode/NormalMethod.java | 28 +++- src/de/dhbwstuttgart/bytecode/Signature.java | 155 ++++++++++++++++++ .../dhbwstuttgart/bytecode/TypeToString.java | 38 +++++ test/bytecode/Generics.jav | 2 + test/bytecode/Generics2.jav | 6 + test/bytecode/Generics2Test.java | 7 + test/bytecode/GenericsTest.java | 7 + test/bytecode/InterfaceTest.java | 7 + test/bytecode/JavaTXCompilerTest.java | 36 ++-- test/bytecode/LamAssign.jav | 4 + test/bytecode/LamAssignTest.java | 7 + 16 files changed, 526 insertions(+), 63 deletions(-) create mode 100644 src/de/dhbwstuttgart/bytecode/Signature.java create mode 100644 src/de/dhbwstuttgart/bytecode/TypeToString.java create mode 100644 test/bytecode/Generics2.jav create mode 100644 test/bytecode/Generics2Test.java create mode 100644 test/bytecode/GenericsTest.java create mode 100644 test/bytecode/InterfaceTest.java create mode 100644 test/bytecode/LamAssignTest.java diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 1b6ef47b..f1994c20 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -8,6 +9,9 @@ import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; +import org.objectweb.asm.signature.SignatureVisitor; +import org.objectweb.asm.signature.SignatureWriter; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; @@ -16,6 +20,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -23,7 +28,6 @@ import de.dhbwstuttgart.typeinference.result.ResultSet; public class BytecodeGen implements ASTVisitor { ClassWriter cw =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); -// String methDesc; String type; @@ -34,6 +38,12 @@ public class BytecodeGen implements ASTVisitor { // stores parameter, local vars and the next index on the local variable table, which use for aload_i, astore_i,... HashMap paramsAndLocals = new HashMap<>(); + // stores generics and their bounds of class + HashMap genericsAndBounds = new HashMap<>(); + // stores generics and their bounds of method + HashMap genericsAndBoundsMethod = new HashMap<>(); + + HashMap methodParamsAndTypes = new HashMap<>(); byte[] bytecode; HashMap classFiles; @@ -45,14 +55,19 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(SourceFile sourceFile) { for(ClassOrInterface cl : sourceFile.getClasses()) { - isInterface = (cl.getModifiers()&512)==512; - System.out.println("IS Interface = "+"modifiers= "+cl.getModifiers()+" ->"+(cl.getModifiers()&512) + isInterface); BytecodeGen classGen = new BytecodeGen(classFiles, resultSet); cl.accept(classGen); + System.out.println("In CLASS: "+(cl.getClassName().toString())); classGen.writeClass(cl.getClassName().toString()); } } - + + /** + * Associates the bytecode of the class that was build with the classWriter {@link #cw} + * with the class name in the map {@link #classFiles} + * + * @param name name of the class with which the the bytecode is to be associated + */ private void writeClass(String name) { bytecode = cw.toByteArray(); classFiles.put(name, bytecode); @@ -62,12 +77,30 @@ public class BytecodeGen implements ASTVisitor { public HashMap getClassFiles() { return classFiles; } + + @Override public void visit(ClassOrInterface classOrInterface) { className = classOrInterface.getClassName().toString(); - // access flages?? - cw.visit(Opcodes.V1_8, classOrInterface.getModifiers()+Opcodes.ACC_SUPER, classOrInterface.getClassName().toString() - , null, classOrInterface.getSuperClass().toString().replace(".", "/"), null); + + isInterface = (classOrInterface.getModifiers()&512)==512; + System.out.println("IS Interface = "+"modifiers= "+classOrInterface.getModifiers()+" ->"+(classOrInterface.getModifiers()&512) + isInterface); + + int acc = isInterface?classOrInterface.getModifiers()+Opcodes.ACC_ABSTRACT:classOrInterface.getModifiers()+Opcodes.ACC_SUPER; + String sig = null; + /* if class has generics then creates signature + * Signature looks like: + * Superclass + */ + if(classOrInterface.getGenerics().iterator().hasNext()) { + Signature signature = new Signature(classOrInterface, genericsAndBounds); + + System.out.println(signature.toString()); + sig = signature.toString(); + } + // needs implemented Interfaces? + cw.visit(Opcodes.V1_8, acc, classOrInterface.getClassName().toString() + , sig, classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()), null); // for each field in the class for(Field f : classOrInterface.getFieldDecl()) { @@ -85,17 +118,34 @@ public class BytecodeGen implements ASTVisitor { } cw.visitSource(classOrInterface.getClassName().toString()+".jav", null); } - + @Override public void visit(Constructor field) { - NormalConstructor constructor = new NormalConstructor(field); - String desc = constructor.accept(new DescriptorToString(resultSet)); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, null, null); + field.getParameterList().accept(this); + + String desc = null; + boolean hasGen = false; + for(String paramName : methodParamsAndTypes.keySet()) { + genericsAndBounds.containsKey(paramName); + hasGen = true; + } + String sig = null; + if(hasGen) { + System.out.println("IM IN CONST HAS Gens"); + Signature signature = new Signature(field, genericsAndBounds,methodParamsAndTypes); + sig = signature.toString(); + System.out.println(sig); + } + NormalConstructor constructor = new NormalConstructor(field,genericsAndBounds,hasGen); + desc = constructor.accept(new DescriptorToString(resultSet)); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", desc, sig, null); mv.visitCode(); System.out.println("-----Constructor-----"); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,desc,cw,isInterface); - - mv.visitInsn(Opcodes.RETURN); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,field, mv,paramsAndLocals,cw, + genericsAndBoundsMethod,genericsAndBounds,isInterface); + if(!field.getParameterList().iterator().hasNext()) { + mv.visitInsn(Opcodes.RETURN); + } mv.visitMaxs(0, 0); mv.visitEnd(); } @@ -106,14 +156,48 @@ public class BytecodeGen implements ASTVisitor { // else it will be stored in pos 1 and this will be stored in pos 0 method.getParameterList().accept(this); - NormalMethod meth = new NormalMethod(method); - String methDesc = meth.accept(new DescriptorToString(resultSet)); + String methDesc = null; + + // Method getModifiers() ? + int acc = isInterface?Opcodes.ACC_ABSTRACT:0; System.out.println("-----Method-----"); - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methDesc, null, null); + + boolean hasGenInParameterList = genericsAndBounds.containsKey(method.getReturnType().acceptTV(new TypeToDescriptor())); + if(!hasGenInParameterList) { + for(String paramName : methodParamsAndTypes.keySet()) { + if(genericsAndBounds.containsKey(paramName)) { + hasGenInParameterList = true; + break; + } + } + } + String sig = null; + boolean hasGen = method.getGenerics().iterator().hasNext() || hasGenInParameterList; + // Wenn ReturnType has Generics?? Fun1<...> wie testet man das generic hat?? + System.out.println(method.getReturnType().acceptTV(new TypeToString())); +// if(method.getReturnType().acceptTV(new TypeToString()).equals("TPH")) { +// Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); +// sig = signature.toString(); +// System.out.println(sig); +// } + /* if method has generics, create signature */ + if(hasGen) { + // resultset hier zum testen + Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); + sig = signature.toString(); + System.out.println(sig); + + } + + NormalMethod meth = new NormalMethod(method,genericsAndBounds,genericsAndBoundsMethod,hasGen); + methDesc = meth.accept(new DescriptorToString(resultSet)); + System.out.println("methDesc" + methDesc); + MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC+acc, method.getName(), methDesc, sig, null); + mv.visitCode(); - BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,methDesc,cw,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,cw,genericsAndBounds,genericsAndBounds,isInterface); mv.visitMaxs(0, 0); mv.visitEnd(); } @@ -121,11 +205,13 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(ParameterList formalParameters) { paramsAndLocals = new HashMap<>(); + methodParamsAndTypes = new HashMap<>(); Iterator itr = formalParameters.iterator(); int i = 1; while(itr.hasNext()) { FormalParameter fp = itr.next(); paramsAndLocals.put(fp.getName(), i); + methodParamsAndTypes.put(fp.getName(), fp.getType()); fp.accept(this); i++; } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index ffdf1450..5368cf93 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -21,6 +21,7 @@ import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.type.FunN; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -29,12 +30,13 @@ public class BytecodeGenMethod implements StatementVisitor{ private Method m; private MethodVisitor mv; private HashMap paramsAndLocals = new HashMap<>(); - private String desc; private String className; private int lamCounter; private ClassWriter cw; private ResultSet resultSet; private boolean isInterface; + HashMap genericsAndBoundsMethod; + private HashMap genericsAndBounds; //for tests ** private String fieldName; @@ -46,8 +48,9 @@ public class BytecodeGenMethod implements StatementVisitor{ private ArrayList varsFunInterface; - public BytecodeGenMethod(String className,ResultSet resultSet, Method m, MethodVisitor mv, HashMap paramsAndLocals, - String desc, ClassWriter cw, boolean isInterface) { + public BytecodeGenMethod(String className,ResultSet resultSet, Method m, MethodVisitor mv, + HashMap paramsAndLocals, ClassWriter cw, + HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface) { this.where = "<<<<<< NORMAL METHOD >>>>>>"; @@ -56,29 +59,32 @@ public class BytecodeGenMethod implements StatementVisitor{ this.m = m; this.mv = mv; this.paramsAndLocals = paramsAndLocals; - this.desc = desc; this.cw = cw; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.genericsAndBounds = genericsAndBounds; this.isInterface = isInterface; this.lamCounter = -1; this.varsFunInterface = new ArrayList<>(); System.out.println("PARAMS = "+this.paramsAndLocals.size()); - this.m.block.accept(this); - System.out.println("PARAMS = "+this.paramsAndLocals.size()); - for(int i = 0; i(); @@ -114,7 +120,7 @@ public class BytecodeGenMethod implements StatementVisitor{ superCall.receiver.accept(this); superCall.arglist.accept(this); // mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", superCall.name, desc,false); - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), superCall.name, desc,isInterface); + mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(Object.class), superCall.name, "()V",isInterface); } // ?? @@ -151,9 +157,6 @@ public class BytecodeGenMethod implements StatementVisitor{ assign.rightSide.accept(this); assign.lefSide.accept(this); } - - - } @Override @@ -165,7 +168,9 @@ public class BytecodeGenMethod implements StatementVisitor{ public void visit(LambdaExpression lambdaExpression) { System.out.println("\n++ In Lambda: "); this.lamCounter++; - + + System.out.println("Lam Hs Gens: " + lambdaExpression.getGenerics().iterator().hasNext()); + System.out.println("Lam Hs Gens: " + lambdaExpression.getReturnType().acceptTV(new TypeToString())); Lambda lam = new Lambda(lambdaExpression); String lamDesc = lam.accept(new DescriptorToString(resultSet)); //Call site, which, when invoked, returns an instance of the functional interface to which @@ -177,7 +182,9 @@ public class BytecodeGenMethod implements StatementVisitor{ Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", mt.toMethodDescriptorString(), false); String methodName = "lambda$new$" + this.lamCounter; +// String typeErasure = "(Ljava/lang/Object;)Ljava/lang/Object;"; // Type erasure +// Type arg1 = Type.getMethodType(typeErasure); Type arg1 = Type.getMethodType(lamDesc); // real Type Type arg3 = Type.getMethodType(lamDesc); @@ -209,7 +216,7 @@ public class BytecodeGenMethod implements StatementVisitor{ MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ staticOrInstance + Opcodes.ACC_SYNTHETIC, methodName, arg3.toString(), null, null); - new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,arg3.toString(),indexOfFirstParamLam,isInterface); + new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,indexOfFirstParamLam,isInterface); mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitEnd(); @@ -277,7 +284,8 @@ public class BytecodeGenMethod implements StatementVisitor{ methodCall.receiver.accept(this); methodCall.arglist.accept(this); - MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType()); + MethodFromMethodCall method = new MethodFromMethodCall(methodCall.arglist, methodCall.getType(), + genericsAndBoundsMethod,genericsAndBounds); String mDesc = method.accept(new DescriptorToString(resultSet)); System.out.println("is Vars empty: "+varsFunInterface.isEmpty()); @@ -287,6 +295,7 @@ public class BytecodeGenMethod implements StatementVisitor{ mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, getResolvedType(methodCall.receiver.getType()), methodCall.name, mDesc, false); }else { + System.out.println("mDesc = " + mDesc); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()), methodCall.name, mDesc, isInterface); } @@ -397,6 +406,8 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(AssignToField assignLeftSide) { +// temporäre Lösung für testen, bis ich weiss wie man funktionale +// interfaces erkennt if(isRightSideALambda) varsFunInterface.add(assignLeftSide.field.getType()); // Loads the an object reference from the local variable diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java index 044218e4..481dbc5d 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/DescriptorToString.java @@ -30,9 +30,37 @@ public class DescriptorToString implements DescriptorVisitor{ Iterator itr = method.getParameterList().iterator(); while(itr.hasNext()) { FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + if(method.hasGen()) { + String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); + if(method.getGenericsAndBoundsMethod().containsKey(fpDesc)) { + desc += "L"+method.getGenericsAndBoundsMethod().get(fpDesc)+ ";"; + }else if(method.getGenericsAndBounds().containsKey(fpDesc)){ + desc += "L"+method.getGenericsAndBounds().get(fpDesc)+ ";"; + }else { + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + }else { + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } } - desc = addReturnType(desc,method.getReturnType(), resultSet); + + if(resultSet.resolveType(method.getReturnType()).resolvedType.toString().equals("void")) { + desc += ")V"; + }else { + if(method.hasGen()) { + String ret = method.getReturnType().acceptTV(new TypeToDescriptor()); + if(method.getGenericsAndBoundsMethod().containsKey(ret)) { + desc += ")L"+method.getGenericsAndBoundsMethod().get(ret)+ ";"; + }else if(method.getGenericsAndBounds().containsKey(ret)){ + desc += ")L"+method.getGenericsAndBounds().get(ret)+ ";"; + }else { + desc += ")" + "L"+resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + }else { + desc += ")" + "L"+resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + } +// desc = addReturnType(desc,method.getReturnType(), resultSet); return desc; } @@ -42,7 +70,19 @@ public class DescriptorToString implements DescriptorVisitor{ Iterator itr = constructor.getParameterList().iterator(); while(itr.hasNext()) { FormalParameter fp = itr.next(); - desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + if(constructor.hasGen()) { + System.out.println("Cons has Gens"); + String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); + System.out.println(fpDesc); + if(constructor.getGenericsAndBounds().containsKey(fpDesc)){ + desc += "L"+constructor.getGenericsAndBounds().get(fpDesc)+ ";"; + }else { + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + }else { + System.out.println("Cons has NOT Gens"); + desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } } desc = desc + ")V"; return desc; @@ -56,6 +96,7 @@ public class DescriptorToString implements DescriptorVisitor{ FormalParameter fp = itr.next(); desc = desc + "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()) + ";"; } + System.out.println("LamReturnType: "+lambdaExpression.getReturnType().acceptTV(new TypeToString())); desc = addReturnType(desc, lambdaExpression.getReturnType(), resultSet); return desc; } @@ -75,10 +116,30 @@ public class DescriptorToString implements DescriptorVisitor{ @Override public String visit(MethodFromMethodCall methodFromMethodCall) { String desc = "("; - for(Expression e : methodFromMethodCall.argList.getArguments()) { - desc = desc + "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + for(Expression e : methodFromMethodCall.getArgList().getArguments()) { + String d = e.getType().acceptTV(new TypeToDescriptor()); + if(methodFromMethodCall.getGenericsAndBoundsMethod().containsKey(d)) { + desc += "L"+methodFromMethodCall.getGenericsAndBoundsMethod().get(d)+ ";"; + }else if(methodFromMethodCall.getGenericsAndBounds().containsKey(d)) { + desc += "L"+methodFromMethodCall.getGenericsAndBounds().get(d)+ ";"; + }else { + desc += "L"+resultSet.resolveType(e.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } } - desc = addReturnType(desc, methodFromMethodCall.returnType, resultSet); + + if(resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.toString().equals("void")) { + desc += ")V"; + }else { + String ret = resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor()); + if(methodFromMethodCall.getGenericsAndBoundsMethod().containsKey(ret)) { + desc += ")L"+methodFromMethodCall.getGenericsAndBoundsMethod().get(ret)+ ";"; + }else if(methodFromMethodCall.getGenericsAndBounds().containsKey(ret)){ + desc += ")L"+methodFromMethodCall.getGenericsAndBounds().get(ret)+ ";"; + }else { + desc += ")" + "L"+resultSet.resolveType(methodFromMethodCall.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; + } + } +// desc = addReturnType(desc, methodFromMethodCall.getReturnType(), resultSet); return desc; } diff --git a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java index 034e2abf..330b0666 100644 --- a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java +++ b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java @@ -1,15 +1,38 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; + import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class MethodFromMethodCall { - ArgumentList argList; - RefTypeOrTPHOrWildcardOrGeneric returnType; + private ArgumentList argList; + private RefTypeOrTPHOrWildcardOrGeneric returnType; + private HashMap genericsAndBoundsMethod; + private HashMap genericsAndBounds; - public MethodFromMethodCall(ArgumentList argList,RefTypeOrTPHOrWildcardOrGeneric returnType) { + public MethodFromMethodCall(ArgumentList argList,RefTypeOrTPHOrWildcardOrGeneric returnType, + HashMap genericsAndBoundsMethod,HashMap genericsAndBounds) { this.argList = argList; this.returnType = returnType; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.genericsAndBounds = genericsAndBounds; + } + + public ArgumentList getArgList() { + return argList; + } + + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + return returnType; + } + + public HashMap getGenericsAndBoundsMethod(){ + return genericsAndBoundsMethod; + } + + public HashMap getGenericsAndBounds(){ + return genericsAndBounds; } public String accept(DescriptorVisitor descVisitor) { diff --git a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java index d029d21e..d2174fd4 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java +++ b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java @@ -1,15 +1,34 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; + import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.ParameterList; public class NormalConstructor { private Constructor constructor; + private HashMap genericsAndBounds; + private boolean hasGenerics; - public NormalConstructor(Constructor constructor) { + public NormalConstructor(Constructor constructor, boolean hasGenerics) { this.constructor = constructor; + this.hasGenerics = hasGenerics; } + public NormalConstructor(Constructor constructor, HashMap genericsAndBounds, boolean hasGenerics) { + this.constructor = constructor; + this.genericsAndBounds = genericsAndBounds; + this.hasGenerics = hasGenerics; + } + + public HashMap getGenericsAndBounds() { + return genericsAndBounds; + } + + public boolean hasGen() { + return hasGenerics; + } + public ParameterList getParameterList() { return constructor.getParameterList(); } diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index 2dd81d78..3f6bf2b4 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -1,14 +1,28 @@ package de.dhbwstuttgart.bytecode; +import java.util.HashMap; + import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class NormalMethod { private Method method; + private HashMap genericsAndBounds; + private HashMap genericsAndBoundsMethod; + private boolean hasGenerics; - public NormalMethod(Method method) { + public NormalMethod(Method method, boolean hasGenerics) { this.method = method; + this.hasGenerics = hasGenerics; + } + + public NormalMethod(Method method, HashMap genericsAndBounds, + HashMap genericsAndBoundsMethod,boolean hasGenerics) { + this.method = method; + this.genericsAndBounds = genericsAndBounds; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.hasGenerics = hasGenerics; } public Method getMethod() { @@ -19,10 +33,22 @@ public class NormalMethod { return method.getParameterList(); } + public HashMap getGenericsAndBounds(){ + return genericsAndBounds; + } + + public HashMap getGenericsAndBoundsMethod(){ + return genericsAndBoundsMethod; + } + public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { return method.getType(); } + public boolean hasGen() { + return this.hasGenerics; + } + public String accept(DescriptorVisitor descVisitor) { return descVisitor.visit(this); } diff --git a/src/de/dhbwstuttgart/bytecode/Signature.java b/src/de/dhbwstuttgart/bytecode/Signature.java new file mode 100644 index 00000000..362429a3 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/Signature.java @@ -0,0 +1,155 @@ +package de.dhbwstuttgart.bytecode; + +import java.util.HashMap; +import java.util.Iterator; + +import org.objectweb.asm.signature.SignatureVisitor; +import org.objectweb.asm.signature.SignatureWriter; + +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.Constructor; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +public class Signature { + private ClassOrInterface classOrInterface; + private HashMap genericsAndBounds; + private HashMap genericsAndBoundsMethod; + private SignatureWriter sw; + private Constructor constructor; + private Method method; + private HashMap methodParamsAndTypes; + private ResultSet resultSet; + + public Signature(ClassOrInterface classOrInterface, HashMap genericsAndBounds) { + this.classOrInterface = classOrInterface; + this.genericsAndBounds = genericsAndBounds; + sw = new SignatureWriter(); + createSignatureForClassOrInterface(); + } + + public Signature(Constructor constructor, HashMap genericsAndBounds, HashMap methodParamsAndTypes) { + this.constructor = constructor; + this.genericsAndBounds = genericsAndBounds; + this.methodParamsAndTypes = methodParamsAndTypes; + sw = new SignatureWriter(); + createSignatureForConsOrMethod(this.constructor,true); + } + + public Signature(Method method, HashMap genericsAndBoundsMethod, + HashMap methodParamsAndTypes, ResultSet resultSet) { + this.method = method; + this.genericsAndBoundsMethod = genericsAndBoundsMethod; + this.methodParamsAndTypes = methodParamsAndTypes; + this.resultSet = resultSet; + sw = new SignatureWriter(); + createSignatureForConsOrMethod(this.method,false); + } + + /** + * Creates signature for a method or constructor with @see {@link SignatureWriter} + * Signature looks like: + * (params L.. OR T.. Or basistape)ReturnType + * + * @param method method or constructor + * @param isConstructor true if constructor + */ + private void createSignatureForConsOrMethod(Method method, boolean isConstructor) { + Iterator itr = method.getGenerics().iterator(); + // visits all formal type parameter and visits their bounds + while(itr.hasNext()) { + GenericTypeVar g = itr.next(); + getBoundsOfTypeVar(g,genericsAndBoundsMethod); + } + // visits each method-parameter to create the signature + for(String paramName : methodParamsAndTypes.keySet()) { + RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName); + // parameter type deswegen ist true + doVisitParamsOrReturn(t,true); + } + if(isConstructor) { + sw.visitReturnType().visitBaseType('V'); + }else { + RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); + // return type deswegen ist false + doVisitParamsOrReturn(returnType, false); + } +// sw.visitEnd(); + } + /** + * Visits parameter type or return type with {@link SignatureVisitor} to create + * the method signature + * @param t type of parameter or return type + * @param isParameterType true if t is type of parameter + */ + private void doVisitParamsOrReturn(RefTypeOrTPHOrWildcardOrGeneric t, boolean isParameterType) { + String type = t.acceptTV(new TypeToString()); + SignatureVisitor sv; + if(isParameterType) { + sv = sw.visitParameterType(); + } + else { + sv = sw.visitReturnType(); + } + switch (type) { + case "RT": + sv.visitClassType(t.acceptTV(new TypeToDescriptor())); + break; + case "GRT": + GenericRefType g = (GenericRefType) t; + sv.visitTypeVariable(g.getParsedName()); + break; + case "TPH": + System.out.println(resultSet.resolveType(t).resolvedType.acceptTV(new TypeToDescriptor())); +// sv.visitInterface().visitClassType(resultSet.resolveType(t).resolvedType.acceptTV(new TypeToDescriptor())+";"); + + break; + default: + if(!isParameterType) + sv.visitBaseType('V'); + break; + } + } + /** + * Creates signature for class or interface with {@link SignatureWriter} + * Signature looks like: + * superclass + */ + private void createSignatureForClassOrInterface() { + Iterator itr = classOrInterface.getGenerics().iterator(); + + while(itr.hasNext()) { + GenericTypeVar g = itr.next(); + getBoundsOfTypeVar(g,genericsAndBounds); + } + + sw.visitSuperclass().visitClassType(classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor()));; + sw.visitEnd(); + } + /** + * Get bounds of type variable + * @param g type variable + * @param genAndBounds + */ + private void getBoundsOfTypeVar(GenericTypeVar g, HashMap genAndBounds) { + sw.visitFormalTypeParameter(g.getParsedName()); + + Iterator bItr = g.getBounds().iterator(); + while(bItr.hasNext()) { + RefTypeOrTPHOrWildcardOrGeneric b =bItr.next(); + String boundDesc = b.acceptTV(new TypeToDescriptor()); + // Ensure that <...> extends java.lang.Object OR ... + sw.visitClassBound().visitClassType(boundDesc); + genAndBounds.put(g.getParsedName(), boundDesc); + } + sw.visitClassBound().visitEnd(); + } + + public String toString() { + return sw.toString(); + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/TypeToString.java b/src/de/dhbwstuttgart/bytecode/TypeToString.java new file mode 100644 index 00000000..86d4124a --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/TypeToString.java @@ -0,0 +1,38 @@ +package de.dhbwstuttgart.bytecode; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; +import de.dhbwstuttgart.syntaxtree.type.GenericRefType; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; + +public class TypeToString implements TypeVisitor{ + + @Override + public String visit(RefType refType) { + return "RT"; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + return "TPH"; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + return "GRT"; + } + +} diff --git a/test/bytecode/Generics.jav b/test/bytecode/Generics.jav index 2c7a67b6..bb7b2af5 100644 --- a/test/bytecode/Generics.jav +++ b/test/bytecode/Generics.jav @@ -1,5 +1,7 @@ class Generics { + Generics(B b){ + } B mt1(B b){ return mt1(b); } diff --git a/test/bytecode/Generics2.jav b/test/bytecode/Generics2.jav new file mode 100644 index 00000000..52d5caa2 --- /dev/null +++ b/test/bytecode/Generics2.jav @@ -0,0 +1,6 @@ +class Generics2{ + B m1(B b){ + return b; + } + +} \ No newline at end of file diff --git a/test/bytecode/Generics2Test.java b/test/bytecode/Generics2Test.java new file mode 100644 index 00000000..26e52665 --- /dev/null +++ b/test/bytecode/Generics2Test.java @@ -0,0 +1,7 @@ +package bytecode; + +public class Generics2Test extends JavaTXCompilerTest{ + public Generics2Test() { + this.fileName = "Generics2"; + } +} diff --git a/test/bytecode/GenericsTest.java b/test/bytecode/GenericsTest.java new file mode 100644 index 00000000..cca16129 --- /dev/null +++ b/test/bytecode/GenericsTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class GenericsTest extends JavaTXCompilerTest { + public GenericsTest() { + this.fileName = "Generics"; + } +} diff --git a/test/bytecode/InterfaceTest.java b/test/bytecode/InterfaceTest.java new file mode 100644 index 00000000..ed378127 --- /dev/null +++ b/test/bytecode/InterfaceTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class InterfaceTest extends JavaTXCompilerTest{ + public InterfaceTest() { + this.fileName = "Interface1"; + } +} diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index 39ff5084..f017e23e 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -25,11 +25,12 @@ public class JavaTXCompilerTest { private static final String rootDirectory = System.getProperty("user.dir")+"/test/bytecode/"; private static final List filesToTest = new ArrayList<>(); - + + protected String fileName = ""; + @Test public void test() throws IOException, java.lang.ClassNotFoundException { System.out.println(rootDirectory); - String fileName = "Faculty"; filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); @@ -48,7 +49,7 @@ public class JavaTXCompilerTest { if(pos != -1) { name = f.getName().substring(0, pos); } - this.writeClassFile(bytecode, name); + this.writeClassFile(bytecode); } } @@ -61,20 +62,23 @@ public class JavaTXCompilerTest { return bytecodeGen.getClassFiles(); } - public void writeClassFile(HashMap classFiles, String name) { + public void writeClassFile(HashMap classFiles) { FileOutputStream output; - byte[] bytecode = classFiles.get(name); - try { - System.out.println("generating .class file"); - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); - output.write(bytecode); - output.close(); - System.out.println(".class file generated"); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } + for(String name : classFiles.keySet()) { + byte[] bytecode = classFiles.get(name); + try { + System.out.println("generating"+name+ ".class file"); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/" +name+".class")); + output.write(bytecode); + output.close(); + System.out.println(name+".class file generated"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } static String readFile(String path, Charset encoding) diff --git a/test/bytecode/LamAssign.jav b/test/bytecode/LamAssign.jav index e522bd3b..3df81780 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -7,3 +7,7 @@ class LamAssign { return lam1; } } + +interface Fun1{ + A apply(B b); +} diff --git a/test/bytecode/LamAssignTest.java b/test/bytecode/LamAssignTest.java new file mode 100644 index 00000000..3442c1d1 --- /dev/null +++ b/test/bytecode/LamAssignTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class LamAssignTest extends JavaTXCompilerTest{ + public LamAssignTest() { + this.fileName = "LamAssign"; + } +} From ab86cc32295c46ad2bae473aeaae4c836405d411 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 10 Jan 2018 12:10:01 +0100 Subject: [PATCH 05/17] RefType.toString(): Ausgabe von Parameterliste implementieren --- src/de/dhbwstuttgart/syntaxtree/type/RefType.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 002e792d..4cf85297 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @@ -28,7 +29,15 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric @Override public String toString(){ - return this.name.toString(); + String params = "<"; + Iterator it = parameter.iterator(); + while(it.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric param = it.next(); + params += param.toString(); + if(it.hasNext())params += ", "; + } + params += ">"; + return this.name.toString() + params; } @Override From 1f5071d4e4d2ac7deaa81fd77e8dd0387db1bb6a Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Fri, 19 Jan 2018 15:24:49 +0100 Subject: [PATCH 06/17] =?UTF-8?q?Methode=20soll=20kein=20Feld=20mehr=20sei?= =?UTF-8?q?n.=20Nicht=20lauff=C3=A4hig!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/syntaxtree/Field.java | 2 +- src/de/dhbwstuttgart/syntaxtree/Method.java | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 421f0ff3..4ee1675e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -5,7 +5,7 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; -public class Field extends SyntaxTreeNode implements TypeScope{ +public class Field extends SyntaxTreeNode { private String name; private RefTypeOrTPHOrWildcardOrGeneric type; diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 3686fb4e..fd4ad180 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -22,7 +22,7 @@ import de.dhbwstuttgart.syntaxtree.statement.Block; * @author janulrich * */ -public class Method extends Field implements IItemWithOffset +public class Method implements IItemWithOffset, TypeScope { public final Block block; private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken()); @@ -54,4 +54,9 @@ public class Method extends Field implements IItemWithOffset public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public Token getOffset() { + return null; + } } From 8d12821c681fde205636d83f75b57c013eaa7f29 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sun, 21 Jan 2018 11:38:55 +0100 Subject: [PATCH 07/17] Methode erbt nicht mehr von Feld --- .../dhbwstuttgart/bytecode/NormalMethod.java | 2 +- .../SyntaxTreeGenerator.java | 7 +++---- .../syntaxtree/AbstractASTWalker.java | 2 +- .../dhbwstuttgart/syntaxtree/Constructor.java | 4 ++-- src/de/dhbwstuttgart/syntaxtree/Field.java | 6 ++++-- src/de/dhbwstuttgart/syntaxtree/Method.java | 18 ++++++++++++++---- .../syntaxtree/factory/ASTFactory.java | 5 ++--- .../syntaxtree/visual/OutputGenerator.java | 2 +- .../typeinference/typeAlgo/TYPEStmt.java | 2 +- 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index 3f6bf2b4..16e84cc5 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -42,7 +42,7 @@ public class NormalMethod { } public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return method.getType(); + return method.getReturnType(); } public boolean hasGen() { diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index c5b135e6..3d9d7aca 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -139,9 +139,9 @@ public class SyntaxTreeGenerator{ block = stmtGen.convert(body.block()); } if(parentClass.equals(new JavaClassName(name))){ - return new Constructor(modifiers, name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); + return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); }else{ - return new Method(modifiers, name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); + return new Method(modifiers, name, retType, parameterList,block, gtvDeclarations, header.getStart()); } } @@ -221,10 +221,9 @@ public class SyntaxTreeGenerator{ */ private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); - int modifiers = 0; ParameterList params = new ParameterList(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset); - return new Constructor(Modifier.PUBLIC, className, classType, modifiers, params, block, classGenerics, offset, fieldInitializations); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset, fieldInitializations); } private RefType convert(Java8Parser.SuperclassContext superclass) { diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index d771bd93..8a66629b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -61,7 +61,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } private void visitMethod(Method method){ - method.getType().accept(this); + method.getReturnType().accept(this); method.getParameterList().accept(this); if(method.block != null) method.block.accept(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index bb48be40..a99692bb 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -14,9 +14,9 @@ public class Constructor extends Method { //TODO: Constructor braucht ein super-Statement - public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, + public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations) { - super(modifier, name, returnType, modifiers, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); + super(modifier, name, returnType, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 4ee1675e..8a4230e3 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -5,8 +5,9 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; -public class Field extends SyntaxTreeNode { - +public class Field extends SyntaxTreeNode implements TypeScope{ + + public final int modifier; private String name; private RefTypeOrTPHOrWildcardOrGeneric type; @@ -14,6 +15,7 @@ public class Field extends SyntaxTreeNode { super(offset); this.name = name; this.type = type; + this.modifier = modifier; } public String getName(){ diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index fd4ad180..99cd3f91 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -22,16 +22,22 @@ import de.dhbwstuttgart.syntaxtree.statement.Block; * @author janulrich * */ -public class Method implements IItemWithOffset, TypeScope +public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope { public final Block block; + public final int modifier; + public final String name; private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken()); private ExceptionList exceptionlist; private GenericDeclarationList generics; + private final RefTypeOrTPHOrWildcardOrGeneric returnType; - public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block, + public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { - super(name, returnType, modifiers, offset); + super(offset); + this.name = name; + this.modifier = modifier; + this.returnType = returnType; this.parameterlist = parameterList; this.block = block; this.generics = gtvDeclarations; @@ -47,7 +53,7 @@ public class Method implements IItemWithOffset, TypeScope @Override public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return this.getType(); + return this.returnType; } @Override @@ -59,4 +65,8 @@ public class Method implements IItemWithOffset, TypeScope public Token getOffset() { return null; } + + public String getName() { + return name; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 046b97b8..59e54385 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -88,7 +88,7 @@ public class ASTFactory { return null; } - return new de.dhbwstuttgart.syntaxtree.Constructor(constructor.getModifiers(), name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); + return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); } //private static RefType createType(Class classType) { @@ -109,9 +109,8 @@ public class ASTFactory { Block block = new Block(new ArrayList(), new NullToken()); GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod.getName()); Token offset = new NullToken(); - int modifier = jreMethod.getModifiers(); - return new Method(jreMethod.getModifiers(), name,returnType, modifier, parameterList, block, gtvDeclarations, offset); + return new Method(jreMethod.getModifiers(), name,returnType, parameterList, block, gtvDeclarations, offset); } public static GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index e760e458..d56c39d8 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -81,7 +81,7 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(Method method) { - method.getType().accept(this); + method.getReturnType().accept(this); out.append(" " + method.getName()); method.getParameterList().accept(this); if(method.block != null) diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 2152fc65..3448e8e3 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -345,7 +345,7 @@ public class TYPEStmt implements StatementVisitor{ for(Method m : cl.getMethods()){ if(m.getName().equals(name) && m.getParameterList().getFormalparalist().size() == numArgs){ - RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); + RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getReturnType()); ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(),info), createTypeScope(cl, m))); From 0610ea8e63c02f2cfda8004e6b5bd95f54527175 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 29 Jan 2018 17:32:25 +0100 Subject: [PATCH 08/17] FC Generator --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 1 + .../SyntaxTreeGenerator/FCGenerator.java | 148 ++++++++++++++++++ .../syntaxtree/factory/UnifyTypeFactory.java | 53 +------ 3 files changed, 151 insertions(+), 51 deletions(-) create mode 100644 src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 2abd8b9d..86666a49 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -89,6 +89,7 @@ public class JavaTXCompiler { final ConstraintSet cons = getConstraints(); FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); + System.out.println(finiteClosure); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java new file mode 100644 index 00000000..cbca13ce --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -0,0 +1,148 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.*; + +import java.util.*; + +public class FCGenerator { + /** + * Baut die FiniteClosure aus availableClasses. + * Klassen welche nicht in availableClasses vorkommen werden im Java Classpath nachgeschlagen. + * + * @param availableClasses - Alle geparsten Klassen + */ + public static Set toFC(Collection availableClasses) throws ClassNotFoundException { + HashSet pairs = new HashSet<>(); + for(ClassOrInterface cly : availableClasses){ + pairs.addAll(getSuperTypes(cly, availableClasses)); + } + System.out.println(pairs); + return pairs; + } + + /** + * Bildet eine Kette vom übergebenen Typ bis hin zum höchsten bekannten Typ + * Als Generics werden TPHs benutzt, welche der Unifikationsalgorithmus korrekt interpretieren muss. + * Die verwendeten TPHs werden in der Kette nach oben gereicht, so erhält der selbe GTV immer den selben TPH + * @param forType + * @return + */ + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { + return getSuperTypes(forType, availableClasses, new HashMap<>()); + } + + //TODO: implements Interface auch als superklassen beachten + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, HashMap gtvs) throws ClassNotFoundException { + List params = new ArrayList<>(); + //Die GTVs, die in forType hinzukommen: + HashMap newGTVs = new HashMap<>(); + //Generics mit gleichem Namen müssen den selben TPH bekommen + for(GenericTypeVar gtv : forType.getGenerics()){ + if(!gtvs.containsKey(gtv.getParsedName())){ + gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + newGTVs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + } + params.add(gtvs.get(gtv.getParsedName())); + } + + Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); + ClassOrInterface superClass; + if(!hasSuperclass.isPresent()) //Wenn es die Klasse in den available Klasses nicht gibt wird sie im Classpath gesucht. Ansonsten Exception + { + superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(forType.getSuperClass().getName().toString())); + }else{ + superClass = hasSuperclass.get(); + } + /* + Die Parameter der superklasse müssen jetzt nach den Angaben in der Subklasse + modifiziert werden + Beispie: Matrix extends Vector> + Den ersten Parameter mit Vector austauschen und dort alle Generics zu den Typplaceholdern in gtvs austauschen + */ + //Hier vermerken, welche Typen im der Superklasse ausgetauscht werden müssen + Iterator itGenParams = superClass.getGenerics().iterator(); + Iterator itSetParams = forType.getSuperClass().getParaList().iterator(); + while(itGenParams.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); + //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: + UnifyType setSetType = setType.acceptTV(new TypeExchanger(gtvs)); + newGTVs.put(itGenParams.next().getParsedName(), setSetType); + } + + UnifyType superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); + + TypeParams paramList = new TypeParams(params); + UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); + UnifyType t2 = superType; + + UnifyPair ret = UnifyTypeFactory.generateSmallerPair(t1, t2); + + List superTypes; + //Rekursiver Aufruf. Abbruchbedingung ist Object als Superklasse: + if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ + superTypes = Arrays.asList(UnifyTypeFactory.generateSmallerPair(UnifyTypeFactory.convert(ASTFactory.createObjectType()), UnifyTypeFactory.convert(ASTFactory.createObjectType()))); + }else{ + superTypes = getSuperTypes(superClass, availableClasses, newGTVs); + } + + List retList = new ArrayList<>(); + retList.add(ret); + retList.addAll(superTypes); + + return retList; + } + + /** + * Tauscht die GTVs in einem Typ gegen die entsprechenden Typen in der übergebenen Map aus. + */ + private static class TypeExchanger implements TypeVisitor{ + + private final HashMap gtvs; + + TypeExchanger(HashMap gtvs){ + this.gtvs = gtvs; + } + + @Override + public UnifyType visit(RefType refType) { + List params = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + params.add(param.acceptTV(this)); + } + TypeParams paramList = new TypeParams(params); + UnifyType ret = new ReferenceType(refType.getName().toString(), paramList); + return ret; + } + + @Override + public UnifyType visit(SuperWildcardType superWildcardType) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(TypePlaceholder typePlaceholder) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(ExtendsWildcardType extendsWildcardType) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(GenericRefType genericRefType) { + if(! gtvs.containsKey(genericRefType.getParsedName())) + throw new DebugException("Dieser Fall darf nicht auftreten"); + return gtvs.get(genericRefType.getParsedName()); + } + + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 6e48c797..aef8068c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -6,6 +6,7 @@ import java.util.stream.Collectors; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; @@ -35,57 +36,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - HashSet pairs = new HashSet<>(); - for(ClassOrInterface cly : fromClasses){ - pairs.addAll(getSuperTypes(cly, fromClasses)); - } - return new FiniteClosure(pairs); - } - - /** - * Bildet eine Kette vom übergebenen Typ bis hin zum höchsten bekannten Typ - * Als Generics werden TPHs benutzt, welche der Unifikationsalgorithmus korrekt interpretieren muss. - * Die verwendeten TPHs werden in der Kette nach oben gereicht, so erhält der selbe GTV immer den selben TPH - * @param forType - * @return - */ - private static List getSuperTypes(ClassOrInterface forType, List availableClasses) throws ClassNotFoundException { - return getSuperTypes(forType, availableClasses, new HashMap<>()); - } - - private static List getSuperTypes(ClassOrInterface forType, List availableClasses, HashMap gtvs) throws ClassNotFoundException { - List params = new ArrayList<>(); - //Generics mit gleichem Namen müssen den selben TPH bekommen - for(GenericTypeVar gtv : forType.getGenerics()){ - if(!gtvs.containsKey(gtv.getParsedName())) - gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); - params.add(gtvs.get(gtv.getParsedName())); - } - Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); - ClassOrInterface superClass; - if(!hasSuperclass.isPresent()) //TODO: Wenn es die Object-Klasse ist, dann ist es in Ordnung, ansonsten Fehler ausgeben: - { - superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(forType.getSuperClass().getName().toString())); - }else{ - superClass = hasSuperclass.get(); - } - List superTypes; - if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ - superTypes = Arrays.asList(generateSmallerPair(convert(ASTFactory.createObjectType()), convert(ASTFactory.createObjectType()))); - }else{ - superTypes = getSuperTypes(superClass, availableClasses, gtvs); - } - - TypeParams paramList = new TypeParams(params); - UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); - UnifyType t2 = superTypes.get(0).getLhsType(); - - UnifyPair ret = generateSmallerPair(t1, t2); - List retList = new ArrayList<>(); - retList.add(ret); - retList.addAll(superTypes); - - return retList; + return new FiniteClosure(FCGenerator.toFC(fromClasses)); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){ From ede55d228e23eeaf7fd054357e1b7b51ada0f6f8 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 29 Jan 2018 17:35:49 +0100 Subject: [PATCH 09/17] Fehler in ASTFactory beheben. GenericRefType statt RefType in den Parameterlisten --- src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 59e54385..6acb4214 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -48,7 +48,7 @@ public class ASTFactory { if(superjreClass != null){ List params = new ArrayList<>(); for(TypeVariable tv : superjreClass.getTypeParameters()){ - params.add(new RefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); + params.add(new GenericRefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); } superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) From c7dea4c86bfa9c22567dd9452537bca5518c2b30 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Mon, 29 Jan 2018 18:26:14 +0100 Subject: [PATCH 10/17] Matrix Test --- .../syntaxtree/factory/UnifyTypeFactory.java | 1 - .../typeinference/unify/model/UnifyType.java | 10 ++++++++++ test/typeinference/MatrixTest.java | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/typeinference/MatrixTest.java diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index aef8068c..a962c2bc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -26,7 +26,6 @@ public class UnifyTypeFactory { public static FiniteClosure generateFC(List fromClasses) throws ClassNotFoundException { /* - TODO: Generics werden zu TPHs Die transitive Hülle muss funktionieren. Man darf schreiben List extends AL und Vector extends List diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java index 56718198..1080f03b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java @@ -96,4 +96,14 @@ public abstract class UnifyType { ret.addAll(typeParams.getInvolvedPlaceholderTypes()); return ret; } + + @Override + public int hashCode() { + return this.toString().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return this.toString().equals(obj.toString()); + } } \ No newline at end of file diff --git a/test/typeinference/MatrixTest.java b/test/typeinference/MatrixTest.java new file mode 100644 index 00000000..8cc587c9 --- /dev/null +++ b/test/typeinference/MatrixTest.java @@ -0,0 +1,9 @@ +package typeinference; + +import java.io.File; + +public class MatrixTest extends JavaTXCompilerTest{ + public MatrixTest() { + this.fileToTest = new File(rootDirectory+"Matrix.jav"); + } +} \ No newline at end of file From 5d94820fed8e2681e1d1848febad2bd57d38d0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Mon, 29 Jan 2018 21:30:45 +0100 Subject: [PATCH 11/17] modified: src/de/dhbwstuttgart/typeinference/unify/RuleSet.java modified: src/de/dhbwstuttgart/typeinference/unify/model/Node.java Genauere Fehlermeldung --- src/de/dhbwstuttgart/typeinference/unify/RuleSet.java | 2 +- src/de/dhbwstuttgart/typeinference/unify/model/Node.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 39ea0eff..8c41660b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -305,7 +305,7 @@ public class RuleSet implements IRuleSet{ if(!rhsSType.getName().equals(lhsSType.getName())) return Optional.empty(); - if(!(lhsSType.getTypeParams().size()==rhsSType.getTypeParams().size()))throw new DebugException("Fehler in Unifizierung"); + if(!(lhsSType.getTypeParams().size()==rhsSType.getTypeParams().size()))throw new DebugException("Fehler in Unifizierung"+ " " + lhsSType.toString() + " " + rhsSType.toString()); //if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) // return Optional.empty(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index d24b6825..c6aa9b3e 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -94,6 +94,6 @@ class Node { @Override public String toString() { - return "Node(" + content.toString() + ")"; + return "Node(" + content.toString() + ")\n"; } } From 39793b461e934819280ef55d8e35deea1c82e3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pl=C3=BCmicke?= Date: Mon, 29 Jan 2018 22:35:48 +0100 Subject: [PATCH 12/17] modified: test/javFiles/Matrix.jav Falsche Typen in methode korrigiert. --- test/javFiles/Matrix.jav | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e6aa84cd..14b58cde 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -2,8 +2,8 @@ import java.util.Vector; class Matrix extends Vector> { - methode(Matrix m) { - Vector> i; + methode(Vector> m) { + Matrix i; methode(i); } } From 027538a0829f02d4d20103075ac735a702314f6a Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 30 Jan 2018 01:08:21 +0100 Subject: [PATCH 13/17] =?UTF-8?q?Unary=20Expression=20und=20while=20statem?= =?UTF-8?q?ent=20anf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/NegativeExpr.html | 2 +- .../syntaxtree/statement/NotExpr.html | 2 +- .../syntaxtree/statement/PositivExpr.html | 2 +- .../syntaxtree/statement/PostDecExpr.html | 2 +- .../syntaxtree/statement/PostIncExpr.html | 2 +- .../syntaxtree/statement/PreDecExpr.html | 2 +- .../syntaxtree/statement/PreIncExpr.html | 2 +- .../statement/class-use/NegativeExpr.html | 6 +- .../statement/class-use/NotExpr.html | 6 +- .../statement/class-use/PositivExpr.html | 6 +- .../statement/class-use/PostDecExpr.html | 6 +- .../statement/class-use/PostIncExpr.html | 6 +- .../statement/class-use/PreDecExpr.html | 6 +- .../statement/class-use/PreIncExpr.html | 6 +- .../dhbwstuttgart/bytecode/BytecodeGen.java | 12 ++-- .../bytecode/BytecodeGenMethod.java | 11 +-- .../dhbwstuttgart/bytecode/KindOfLambda.java | 12 ++-- .../StatementGenerator.java | 27 ++++--- .../SyntaxTreeGenerator/SyntacticSugar.java | 30 +++++--- .../SyntaxTreeGenerator/TypeGenerator.java | 7 +- .../syntaxtree/AbstractASTWalker.java | 11 +-- .../syntaxtree/StatementVisitor.java | 4 +- .../syntaxtree/factory/ASTFactory.java | 2 +- .../syntaxtree/statement/IfStmt.java | 6 +- .../statement/JavaInternalExpression.java | 12 ++++ .../syntaxtree/statement/NegativeExpr.java | 18 ----- .../syntaxtree/statement/NotExpr.java | 14 ---- .../syntaxtree/statement/PositivExpr.java | 15 ---- .../syntaxtree/statement/PostDecExpr.java | 17 ----- .../syntaxtree/statement/PostIncExpr.java | 15 ---- .../syntaxtree/statement/PreDecExpr.java | 17 ----- .../syntaxtree/statement/PreIncExpr.java | 12 ---- .../syntaxtree/statement/UnaryExpr.java | 29 +++++++- .../syntaxtree/statement/UnaryMinus.java | 12 ---- .../syntaxtree/statement/UnaryNot.java | 7 -- .../syntaxtree/statement/UnaryPlus.java | 20 ------ .../syntaxtree/visual/OutputGenerator.java | 10 +-- .../visual/TypeOutputGenerator.java | 5 -- .../typedeployment/TypeInsertPlacer.java | 2 + .../typeinference/typeAlgo/TYPEStmt.java | 28 ++++++-- test/javFiles/Matrix.jav | 70 ++++++------------- 41 files changed, 187 insertions(+), 294 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html index 274fb17d..7ddcc746 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
    • -
    • de.dhbwstuttgart.syntaxtree.statement.NegativeExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html index d59ab82b..1eb88e1d 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.NotExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.NotExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html index 6dfdb37b..e02874de 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PositivExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PositivExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html index 86c5a8fa..9c293c66 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PostDecExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostDecExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html index 94b06b12..4d52f399 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PostIncExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostIncExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html index 47989bf2..3027535e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PreDecExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreDecExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html index 437a143f..e2caa4bd 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.PreIncExpr
    • +
    • de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreIncExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html index beb55c1f..6e30f09e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.NegativeExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr @@ -70,9 +70,9 @@
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NegativeExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NegativeExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.NegativeExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NotExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.NotExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.NotExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.NotExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PositivExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PositivExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PositivExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PositivExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostDecExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostDecExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PostDecExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostDecExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostIncExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PostIncExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PostIncExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostIncExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreDecExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreDecExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PreDecExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreDecExpr
    -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreIncExpr

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.PreIncExpr

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PreIncExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreIncExpr
    diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index f1994c20..6fff6729 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; @@ -382,12 +383,6 @@ public class BytecodeGen implements ASTVisitor { } - @Override - public void visit(UnaryPlus unaryPlus) { - // TODO Auto-generated method stub - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub @@ -453,4 +448,9 @@ public class BytecodeGen implements ASTVisitor { } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 5368cf93..0d0f8cea 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Handle; @@ -336,6 +337,11 @@ public class BytecodeGenMethod implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -367,11 +373,6 @@ public class BytecodeGenMethod implements StatementVisitor{ mv.visitVarInsn(Opcodes.ALOAD, 0); } - @Override - public void visit(UnaryPlus unaryPlus) { - System.out.println("++ In UnaryPlus: "); - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java index 36a28ce6..5264888c 100644 --- a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java +++ b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.ArrayList; @@ -126,6 +127,11 @@ public class KindOfLambda implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -155,12 +161,6 @@ public class KindOfLambda implements StatementVisitor{ this.argumentList.add(aThis.getType()); } - @Override - public void visit(UnaryPlus unaryPlus) { - // TODO Auto-generated method stub - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index f4ada6b2..a1d17e8a 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -16,6 +16,7 @@ import de.dhbwstuttgart.syntaxtree.type.Void; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; +import java.lang.reflect.Type; import java.util.*; public class StatementGenerator { @@ -289,8 +290,8 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { @@ -316,8 +317,10 @@ public class StatementGenerator { } private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement thenBlock = convert(stmt.statementNoShortIf()); + Statement elseBlock = convert(stmt.statement()); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); } private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ @@ -591,7 +594,7 @@ public class StatementGenerator { if(expression.relationalExpression() == null){ return convert(expression.shiftExpression()); }else{ - throw new NotImplementedException(); + return convert(expression.relationalExpression()); } } @@ -604,7 +607,9 @@ public class StatementGenerator { } private Expression convert(Java8Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() == null){ + if(expression.additiveExpression() != null){ + return convert(expression.additiveExpression()); + }else if(expression.multiplicativeExpression() != null){ return convert(expression.multiplicativeExpression()); }else{ throw new NotImplementedException(); @@ -627,9 +632,11 @@ public class StatementGenerator { }else if(expression.unaryExpressionNotPlusMinus() != null){ return convert(expression.unaryExpressionNotPlusMinus()); }else if(expression.getText().startsWith("+")){ - return new UnaryPlus(convert(expression.unaryExpression())); + return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), + TypePlaceholder.fresh(expression.getStart()), expression.getStart()); }else if(expression.getText().startsWith("-")){ - return new UnaryMinus(convert(expression.unaryExpression())); + return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), + TypePlaceholder.fresh(expression.getStart()), expression.getStart()); }else{ //Diese Exceptions sollte nie geworfen werden. //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. @@ -672,10 +679,10 @@ public class StatementGenerator { } for(Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ - expr = new PostIncExpr(expr); + expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), inc.getStart()); } for(Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ - expr = new PostDecExpr(expr); + expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), dec.getStart()); } return expr; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java index d758f52b..d7926004 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -1,6 +1,9 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.List; @@ -10,22 +13,27 @@ public class SyntacticSugar { public static List addTrailingReturn(List statements){ if(statements.size()!=0) { Statement lastStmt = statements.get(statements.size() - 1); - if (lastStmt instanceof Return) return statements; - if (lastStmt instanceof WhileStmt) { - //TODO - //if (hasReturn(((WhileStmt) lastStmt).loopBlock)) return statements; - } else if (lastStmt instanceof IfStmt) { - if (hasReturn(((IfStmt) lastStmt).then_block) - && hasReturn(((IfStmt) lastStmt).else_block)) return statements; - } else if (lastStmt instanceof ForStmt) { - if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements; - } else { - } + ReturnFinder hasReturn = new ReturnFinder(); + lastStmt.accept(hasReturn); + if(hasReturn.hasReturn)return statements; } statements.add(new ReturnVoid(new NullToken())); return statements; } + private static class ReturnFinder extends AbstractASTWalker{ + public boolean hasReturn = false; + @Override + public void visit(Return aReturn) { + hasReturn = true; + } + + @Override + public void visit(ReturnVoid aReturn) { + hasReturn = true; + } + } + private static boolean hasReturn(Block block){ for(Statement s : block.getStatements()) if(s instanceof Return)return true; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index d54b682a..1dd259d0 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -40,7 +40,12 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { if(unannTypeContext.unannPrimitiveType()!=null){ - throw new NotImplementedException(); + if(unannTypeContext.unannPrimitiveType().getText().equals("boolean")){ + return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), unannTypeContext.getStart()); + }else{ + Java8Parser.NumericTypeContext numericType = unannTypeContext.unannPrimitiveType().numericType(); + throw new NotImplementedException(); + } }else if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ //System.out.println(unannTypeContext.getText()); diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 8a66629b..054c3915 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; +import javax.management.Notification; import java.lang.reflect.Modifier; import java.util.Iterator; @@ -202,6 +203,11 @@ public abstract class AbstractASTWalker implements ASTVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + unaryExpr.expr.accept(this); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -227,11 +233,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 195db230..1c4e149c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -50,8 +50,6 @@ public interface StatementVisitor { void visit(This aThis); - void visit(UnaryPlus unaryPlus); - void visit(WhileStmt whileStmt); void visit(DoStmt whileStmt); @@ -67,4 +65,6 @@ public interface StatementVisitor { void visit(SuperCall superCall); void visit(ExpressionReceiver expressionReceiver); + + void visit(UnaryExpr unaryExpr); } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 6acb4214..b0683569 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -133,7 +133,7 @@ public class ASTFactory { } */ - public static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ + private static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ if(type.getTypeName().equals("void")){ return new Void(new NullToken()); }else if(type.getTypeName().equals("int")){ diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 5d6a4658..4af1e770 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -11,11 +11,11 @@ import org.antlr.v4.runtime.Token; public class IfStmt extends Statement { public final Expression expr; - public final Block then_block; - public final Block else_block; + public final Statement then_block; + public final Statement else_block; public IfStmt(RefTypeOrTPHOrWildcardOrGeneric type, - Expression expr, Block thenBlock, Block elseBlock, Token offset) + Expression expr, Statement thenBlock, Statement elseBlock, Token offset) { super(type,offset); this.expr = expr; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java new file mode 100644 index 00000000..366c0602 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +import java.nio.charset.StandardCharsets; + +public abstract class JavaInternalExpression extends Statement{ + public JavaInternalExpression(RefTypeOrTPHOrWildcardOrGeneric retType, Token offset){ + super(retType, offset); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java deleted file mode 100644 index 67e71e95..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import org.antlr.v4.runtime.Token; - - - -public class NegativeExpr extends UnaryExpr -{ - public NegativeExpr(Token offset) - { - super(offset); - } - private UnaryMinus neg; - public Expression expr; - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java deleted file mode 100644 index 0fcfd333..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class NotExpr extends UnaryExpr -{ - public NotExpr(Token offset) - { - super(offset); - } - private UnaryNot not; - public Expression expr; -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java deleted file mode 100644 index bec6ffbe..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class PositivExpr extends UnaryExpr -{ - public PositivExpr(Token offset) - { - super(offset); - } - protected UnaryPlus plus; - public Expression expr; - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java deleted file mode 100644 index 18b56f96..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import de.dhbwstuttgart.parser.antlr.Java8Parser; - - - - -public class PostDecExpr extends PostIncExpr -{ - - public PostDecExpr(Expression expr) { - super(expr); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java deleted file mode 100644 index 1292192b..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.exceptions.NotImplementedException; - - -public class PostIncExpr extends UnaryExpr -{ - public PostIncExpr(Expression expr) { - super(null); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java deleted file mode 100644 index de0fb0b1..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import org.antlr.v4.runtime.Token; - - - - - -public class PreDecExpr extends UnaryExpr -{ - public PreDecExpr(Token offset) - { - super(offset); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java deleted file mode 100644 index 736dc311..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class PreIncExpr extends UnaryExpr -{ - public PreIncExpr(Token offset) - { - super(offset); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java index 2157560c..ca06146a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java @@ -1,14 +1,37 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public abstract class UnaryExpr extends MethodCall +public class UnaryExpr extends JavaInternalExpression { + public enum Operation{ + NOT, + MINUS, + PREINCREMENT, + PREDECREMENT, + POSTINCREMENT, + PLUS, POSTDECREMENT + } + + + public final Operation operation; public Expression expr; - public UnaryExpr(Token offset) + public UnaryExpr(Operation operation, Expression argument, RefTypeOrTPHOrWildcardOrGeneric retType, Token offset) { - super(null,null,null,null,null); + super(retType, offset); + this.expr = argument; + this.operation = operation; } + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java deleted file mode 100644 index 6c100225..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; - -public class UnaryMinus extends UnaryPlus -{ - - public UnaryMinus(Expression expression) { - super(expression); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java deleted file mode 100644 index 2dce91bd..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -public class UnaryNot -{ - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java deleted file mode 100644 index b374166f..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.exceptions.NotImplementedException; - -public class UnaryPlus extends Expression -{ - public UnaryPlus(Expression expression){ - super(expression.getType(),expression.getOffset()); - - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index d56c39d8..191d58a0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -296,11 +296,6 @@ public class OutputGenerator implements ASTVisitor{ out.append("this"); } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { out.append("while("); @@ -349,4 +344,9 @@ public class OutputGenerator implements ASTVisitor{ public void visit(ExpressionReceiver receiver) { receiver.expr.accept(this); } + + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java index dcefafa4..6f90051a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java @@ -214,11 +214,6 @@ public class TypeOutputGenerator extends OutputGenerator { aThis.getType().accept(this); } - @Override - public void visit(UnaryPlus unaryPlus) { - super.visit(unaryPlus); - } - @Override public void visit(WhileStmt whileStmt) { super.visit(whileStmt); diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 8dea5748..ad591ebe 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.typedeployment; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 3448e8e3..ed5837ad 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -2,12 +2,14 @@ package de.dhbwstuttgart.typeinference.typeAlgo; //import com.sun.org.apache.xpath.internal.Arg; +import com.sun.org.apache.xpath.internal.operations.Bool; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; @@ -193,6 +195,23 @@ public class TYPEStmt implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT || + unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT || + unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT || + unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){ + //@see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2 + //Expression muss zu Numeric Convertierbar sein. also von Numeric erben + RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), unaryExpr.getOffset()); + constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.EQUALSDOT)); + //The type of the postfix increment expression is the type of the variable + constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT)); + }else{ + throw new NotImplementedException(); + } + } + @Override public void visit(Return returnExpr) { returnExpr.retexpr.accept(this); @@ -241,14 +260,11 @@ public class TYPEStmt implements StatementVisitor{ }; } - @Override - public void visit(UnaryPlus unaryPlus) { - throw new NotImplementedException(); - } - @Override public void visit(WhileStmt whileStmt) { - throw new NotImplementedException(); + RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); + constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); + whileStmt.loopBlock.accept(this); } @Override diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e6aa84cd..66abd5e0 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -1,54 +1,24 @@ import java.util.Vector; class Matrix extends Vector> { - - methode(Matrix m) { - Vector> i; - methode(i); - } - } - -/* -import java.util.Vector; - -class Matrix extends Vector> { - - Matrix mul_rec(Matrix m) { - auto v1 = new Vector(); - auto v2 = new Vector(); - auto i = 0; - while(i < m.size()) { - auto v; - v = m.elementAt(i); - v2.addElement(v.remove(v.size()-1)); - i++; - } - Matrix ret; - if (m.elementAt(0).size() > 0) { - ret = this.mul_rec(m); - } - else { - ret = new Matrix(); - i = 0; - while (i < this.size()) { - ret.addElement(new Vector()); - i++; - } - } - i = 0; - while (i < this.size()) { - int erg = 0; - auto j; - j = 0; - while (j < v2.size()) { - erg = erg + this.elementAt(i).elementAt(j).intValue() - * v2.elementAt(j).intValue(); - j++; - } - ret.elementAt(i).addElement(erg); - i++; - } - return ret; - } + mul(m) { + var ret = new Matrix(); + var i = 0; + while(i (); + var j = 0; + while(j < v1.size()) { + var erg = 0; + var k = 0; + while(k < v1.size()) { + erg = erg + v1.elementAt(k) + * m.elementAt(k).elementAt(j); + k++; } + v2.addElement(new Integer(erg)); + j++; } + ret.addElement(v2); + i++; } + return ret; + } } -*/ \ No newline at end of file From 3ce4937bc1961b097ffb1e9ef6102649168dbe7e Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Tue, 30 Jan 2018 02:23:58 +0100 Subject: [PATCH 14/17] =?UTF-8?q?Fehler=20im=20Trailing=20Return=20anh?= =?UTF-8?q?=C3=A4ngen=20beheben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SyntaxTreeGenerator/FCGenerator.java | 2 ++ .../StatementGenerator.java | 8 ++++---- .../SyntaxTreeGenerator.java | 2 +- .../syntaxtree/AbstractASTWalker.java | 9 +++++---- .../syntaxtree/visual/OutputGenerator.java | 20 ++++++++++++++++++- test/javFiles/Matrix.jav | 2 +- test/typeinference/JavaTXCompilerTest.java | 2 ++ 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index cbca13ce..7863c8ed 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -141,6 +141,8 @@ public class FCGenerator { public UnifyType visit(GenericRefType genericRefType) { if(! gtvs.containsKey(genericRefType.getParsedName())) throw new DebugException("Dieser Fall darf nicht auftreten"); + //TODO: Diesen Dirty-Hack beseitigen. Fehler tritt bei java.lang.invoke.LambdaFormEditor$Transform$Kind auf. + //return UnifyTypeFactory.convert(ASTFactory.createObjectType()); return gtvs.get(genericRefType.getParsedName()); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index a1d17e8a..069f49dc 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -96,7 +96,7 @@ public class StatementGenerator { private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) { if(stmt.block() != null){ - return convert(stmt.block()); + return convert(stmt.block(), false); }else if(stmt.emptyStatement() != null){ return new EmptyStmt(stmt.getStart()); }else if(stmt.expressionStatement() != null){ @@ -122,14 +122,14 @@ public class StatementGenerator { }else throw new NotImplementedException(); } - public Block convert(Java8Parser.BlockContext block) { + public Block convert(Java8Parser.BlockContext block, boolean addTrailingReturn) { List statements = new ArrayList<>(); if(block.blockStatements() != null) for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ List stmt = convert(statementContext); statements.addAll(stmt); } - statements = SyntacticSugar.addTrailingReturn(statements); + if(addTrailingReturn)statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } @@ -857,7 +857,7 @@ public class StatementGenerator { expression.lambdaBody().expression().getStart())); block = new Block(statements, expression.lambdaBody().getStart()); }else{ - block = lambdaGenerator.convert(expression.lambdaBody().block()); + block = lambdaGenerator.convert(expression.lambdaBody().block(), true); } List funNParams = new ArrayList<>(); funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 3d9d7aca..b681d9c3 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -136,7 +136,7 @@ public class SyntaxTreeGenerator{ //TODO: Error! Abstrakte Methode ohne abstrakt Keyword } }else{ - block = stmtGen.convert(body.block()); + block = stmtGen.convert(body.block(),true); } if(parentClass.equals(new JavaClassName(name))){ return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 054c3915..dfb8438c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -158,12 +158,13 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(ForStmt forStmt) { - + forStmt.body_Loop_block.accept(this); } @Override public void visit(IfStmt ifStmt) { - + ifStmt.then_block.accept(this); + ifStmt.else_block.accept(this); } @Override @@ -235,12 +236,12 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(WhileStmt whileStmt) { - + whileStmt.loopBlock.accept(this); } @Override public void visit(DoStmt whileStmt) { - + whileStmt.loopBlock.accept(this); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 191d58a0..879a4ba2 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -347,6 +347,24 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(UnaryExpr unaryExpr) { - throw new NotImplementedException(); + if(unaryExpr.operation == UnaryExpr.Operation.MINUS){ + out.append("-"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PLUS){ + out.append("+"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT){ + out.append("--"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){ + out.append("++"); + } + unaryExpr.expr.accept(this); + if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT){ + out.append("--"); + } + if(unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT){ + out.append("++"); + } } } \ No newline at end of file diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index 66abd5e0..476027b3 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -4,7 +4,7 @@ class Matrix extends Vector> { mul(m) { var ret = new Matrix(); var i = 0; - while(i (); var j = 0; diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index f4622954..03452018 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -2,6 +2,7 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; @@ -51,6 +52,7 @@ public class JavaTXCompilerTest { for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); System.out.println(ASTTypePrinter.print(sf)); + System.out.println(ASTPrinter.print(sf)); //List results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen assert results.size()>0; Set insertedTypes = new HashSet<>(); From 8e4791cca6beff87e588959c73df93cf98cf41a6 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 31 Jan 2018 13:34:54 +0100 Subject: [PATCH 15/17] Anpassungen im Matrix Test --- test/javFiles/Matrix.jav | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index 476027b3..e1183535 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -1,4 +1,6 @@ import java.util.Vector; +import java.lang.Integer; +import java.lang.Boolean; class Matrix extends Vector> { mul(m) { From e556a470e7dc586360ae68b9d9c2af948b52bbd1 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 31 Jan 2018 15:39:19 +0100 Subject: [PATCH 16/17] =?UTF-8?q?Literal=20l=C3=B6schen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/BoolLiteral.html | 4 +-- .../syntaxtree/statement/CharLiteral.html | 4 +-- .../syntaxtree/statement/DoubleLiteral.html | 4 +-- .../syntaxtree/statement/FloatLiteral.html | 4 +-- .../syntaxtree/statement/IntLiteral.html | 4 +-- .../syntaxtree/statement/Literal.html | 2 +- .../syntaxtree/statement/LongLiteral.html | 4 +-- .../syntaxtree/statement/Null.html | 4 +-- .../syntaxtree/statement/StringLiteral.html | 4 +-- .../statement/class-use/Literal.html | 4 +-- .../dhbwstuttgart/bytecode/BytecodeGen.java | 17 ++-------- .../bytecode/BytecodeGenMethod.java | 11 ++----- .../dhbwstuttgart/bytecode/KindOfLambda.java | 11 ++----- .../environment/CompilationEnvironment.java | 16 +++++---- src/de/dhbwstuttgart/parser/JavaTXParser.java | 2 +- .../StatementGenerator.java | 33 +++++++++++-------- .../dhbwstuttgart/syntaxtree/ASTVisitor.java | 6 ---- .../syntaxtree/AbstractASTWalker.java | 13 ++------ .../syntaxtree/StatementVisitor.java | 11 ++----- .../syntaxtree/statement/Binary.java | 27 --------------- .../syntaxtree/statement/BinaryExpr.java | 33 +++++++++++++++++-- .../syntaxtree/statement/InstanceOf.java | 5 ++- .../statement/{literal => }/Literal.java | 12 ++++--- .../syntaxtree/statement/MethodCall.java | 1 - .../statement/literal/BoolLiteral.java | 26 --------------- .../statement/literal/CharLiteral.java | 21 ------------ .../syntaxtree/statement/literal/Null.java | 21 ------------ .../statement/literal/NumberLiteral.java | 22 ------------- .../statement/literal/StringLiteral.java | 23 ------------- .../syntaxtree/visual/ASTTypePrinter.java | 7 ---- .../syntaxtree/visual/OutputGenerator.java | 23 +++++-------- .../visual/TypeOutputGenerator.java | 17 +++------- .../typeinference/assumptions/FunNClass.java | 1 - .../typeinference/typeAlgo/TYPEStmt.java | 27 ++++++--------- 34 files changed, 123 insertions(+), 301 deletions(-) delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/Binary.java rename src/de/dhbwstuttgart/syntaxtree/statement/{literal => }/Literal.java (52%) delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/BoolLiteral.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/CharLiteral.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/Null.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/NumberLiteral.java delete mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/literal/StringLiteral.java diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html index 35ca0132..be892a1e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/BoolLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.Expr
    • -
    • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
    • +
    • de.dhbwstuttgart.syntaxtree.statement.Literal
      • de.dhbwstuttgart.syntaxtree.statement.literal.BoolLiteral
      • @@ -253,7 +253,7 @@ extends
          -
        • +
        • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

          diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html index b13baef9..7d0bcafb 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/CharLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
        • de.dhbwstuttgart.syntaxtree.statement.Expr
          • -
          • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
          • +
          • de.dhbwstuttgart.syntaxtree.statement.Literal
            • de.dhbwstuttgart.syntaxtree.statement.literal.CharLiteral
            • @@ -253,7 +253,7 @@ extends
                -
              • +
              • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html index 3f3f7c69..d367cd8a 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/DoubleLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
              • de.dhbwstuttgart.syntaxtree.statement.Expr
                • -
                • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                • +
                • de.dhbwstuttgart.syntaxtree.statement.Literal
                  • de.dhbwstuttgart.syntaxtree.statement.literal.DoubleLiteral
                  • @@ -263,7 +263,7 @@ extends
                      -
                    • +
                    • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                      diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html index 4491e9b7..2c88f19d 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/FloatLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                    • de.dhbwstuttgart.syntaxtree.statement.Expr
                      • -
                      • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                      • +
                      • de.dhbwstuttgart.syntaxtree.statement.Literal
                        • de.dhbwstuttgart.syntaxtree.statement.literal.FloatLiteral
                        • @@ -263,7 +263,7 @@ extends
                            -
                          • +
                          • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                            diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html index 664dd208..7d7ef3eb 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/IntLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                          • de.dhbwstuttgart.syntaxtree.statement.Expr
                            • -
                            • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                            • +
                            • de.dhbwstuttgart.syntaxtree.statement.Literal
                              • de.dhbwstuttgart.syntaxtree.statement.literal.NumberLiteral
                              • @@ -263,7 +263,7 @@ extends
                                  -
                                • +
                                • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                  diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html index 63ee66ac..8fdde516 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Literal.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                                • de.dhbwstuttgart.syntaxtree.statement.Expr
                                  • -
                                  • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                  • +
                                  • de.dhbwstuttgart.syntaxtree.statement.Literal
                                diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html index 9604e4c8..ba044487 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/LongLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                              • de.dhbwstuttgart.syntaxtree.statement.Expr
                                • -
                                • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                • +
                                • de.dhbwstuttgart.syntaxtree.statement.Literal
                                  • de.dhbwstuttgart.syntaxtree.statement.LongLiteral
                                  • @@ -263,7 +263,7 @@ extends
                                      -
                                    • +
                                    • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                      diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html index d7812a4f..23f741de 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/Null.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                                    • de.dhbwstuttgart.syntaxtree.statement.Expr
                                      • -
                                      • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                      • +
                                      • de.dhbwstuttgart.syntaxtree.statement.Literal
                                        • de.dhbwstuttgart.syntaxtree.statement.literal.Null
                                        • @@ -245,7 +245,7 @@ extends
                                            -
                                          • +
                                          • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                            diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html index 4d1c4988..89acbb3b 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/StringLiteral.html @@ -112,7 +112,7 @@ var activeTableTab = "activeTableTab";
                                          • de.dhbwstuttgart.syntaxtree.statement.Expr
                                            • -
                                            • de.dhbwstuttgart.syntaxtree.statement.literal.Literal
                                            • +
                                            • de.dhbwstuttgart.syntaxtree.statement.Literal
                                              • de.dhbwstuttgart.syntaxtree.statement.literal.StringLiteral
                                              • @@ -253,7 +253,7 @@ extends
                                                  -
                                                • +
                                                • Methods inherited from class de.dhbwstuttgart.syntaxtree.statement.Literal

                                                  diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html index 5c5ba6a3..33eabfbd 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/Literal.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.literal.Literal +Uses of Class de.dhbwstuttgart.syntaxtree.statement.Literal @@ -70,7 +70,7 @@
  • -

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.Literal

    +

    Uses of Class
    de.dhbwstuttgart.syntaxtree.statement.Literal

      diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 6fff6729..2c68ad43 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.bytecode; -import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -10,14 +9,10 @@ import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; -import org.objectweb.asm.signature.SignatureVisitor; -import org.objectweb.asm.signature.SignatureWriter; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; @@ -282,9 +277,8 @@ public class BytecodeGen implements ASTVisitor { } @Override - public void visit(Binary binary) { - // TODO Auto-generated method stub - + public void visit(BinaryExpr binary) { + } @Override @@ -395,11 +389,6 @@ public class BytecodeGen implements ASTVisitor { } - @Override - public void visit(Null aNull) { - // TODO Auto-generated method stub - - } // ??? @Override public void visit(Literal literal) { diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 0d0f8cea..8cbb64fa 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -20,9 +20,7 @@ import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; -import de.dhbwstuttgart.syntaxtree.type.FunN; +import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -161,7 +159,7 @@ public class BytecodeGenMethod implements StatementVisitor{ } @Override - public void visit(Binary binary) { + public void visit(BinaryExpr binary) { System.out.println("\t++ In Binary: "); } @@ -385,11 +383,6 @@ public class BytecodeGenMethod implements StatementVisitor{ } - @Override - public void visit(Null aNull) { - mv.visitInsn(Opcodes.ACONST_NULL); - } - @Override public void visit(Literal literal) { // value? diff --git a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java index 5264888c..1cc8e386 100644 --- a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java +++ b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java @@ -8,8 +8,7 @@ import java.util.List; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class KindOfLambda implements StatementVisitor{ @@ -46,7 +45,7 @@ public class KindOfLambda implements StatementVisitor{ } @Override - public void visit(Binary binary) { + public void visit(BinaryExpr binary) { // TODO Auto-generated method stub } @@ -173,12 +172,6 @@ public class KindOfLambda implements StatementVisitor{ } - @Override - public void visit(Null aNull) { - // TODO Auto-generated method stub - - } - @Override public void visit(Literal literal) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java index 7044da6b..eca8c9c2 100644 --- a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -41,14 +41,16 @@ public class CompilationEnvironment { */ public CompilationEnvironment(List sourceFiles) { String bootClassPath = System.getProperty("sun.boot.class.path"); - librarys = new ArrayList<>(); - for(String path : bootClassPath.split(File.pathSeparator)) { - try { - librarys.add(new URL("file:"+path)); - } catch (MalformedURLException e) { - new DebugException("Fehler im Classpath auf diesem System"); + librarys = new ArrayList<>(); + if(bootClassPath != null){ + for(String path : bootClassPath.split(File.pathSeparator)) { + try { + librarys.add(new URL("file:"+path)); + } catch (MalformedURLException e) { + new DebugException("Fehler im Classpath auf diesem System"); + } } - } + } this.sourceFiles = sourceFiles; this.packageCrawler = new PackageCrawler(librarys); } diff --git a/src/de/dhbwstuttgart/parser/JavaTXParser.java b/src/de/dhbwstuttgart/parser/JavaTXParser.java index c860a6a7..aad0a575 100644 --- a/src/de/dhbwstuttgart/parser/JavaTXParser.java +++ b/src/de/dhbwstuttgart/parser/JavaTXParser.java @@ -29,7 +29,7 @@ public class JavaTXParser { */ } - /* Für das Typsystem ist es notwendig, dass sich der Source in einer Datei befindet: + /* Für das Typsystem ist es notwendig, dass sich der Source in einer Datei befindet: public SourceFile parse(String fileContent) throws IOException, java.lang.ClassNotFoundException { return this.parse(new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8))); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 069f49dc..1586ba3f 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -7,7 +7,6 @@ import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -16,7 +15,6 @@ import de.dhbwstuttgart.syntaxtree.type.Void; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; -import java.lang.reflect.Type; import java.util.*; public class StatementGenerator { @@ -544,8 +542,7 @@ public class StatementGenerator { if(expression.conditionalOrExpression() == null){ return convert(expression.conditionalAndExpression()); }else{ - return new Binary(convert(expression.conditionalOrExpression()), - convert(expression.conditionalAndExpression()), Binary.Operator.OR); + throw new NotImplementedException(); } } @@ -553,8 +550,7 @@ public class StatementGenerator { if(expression.conditionalAndExpression() == null){ return convert(expression.inclusiveOrExpression()); }else{ - return new Binary(convert(expression.conditionalAndExpression()), - convert(expression.inclusiveOrExpression()), Binary.Operator.AND); + throw new NotImplementedException(); } } @@ -591,11 +587,20 @@ public class StatementGenerator { } private Expression convert(Java8Parser.RelationalExpressionContext expression) { - if(expression.relationalExpression() == null){ + if(expression.relationalExpression() == null) { return convert(expression.shiftExpression()); - }else{ - return convert(expression.relationalExpression()); + }else { + String operator = expression.getChild(1).getText(); + return new BinaryExpr(convertBinaryOperator(operator), TypePlaceholder.fresh(expression.getStart()), + convert(expression.relationalExpression()), + convert(expression.shiftExpression()), expression.getStart()); } + //}else throw new NotImplementedException(); + } + + private BinaryExpr.Operator convertBinaryOperator(String operator) { + + return null; } private Expression convert(Java8Parser.ShiftExpressionContext expression) { @@ -778,25 +783,25 @@ public class StatementGenerator { private Expression convert(Java8Parser.LiteralContext literal) { if(literal.IntegerLiteral() != null || literal.FloatingPointLiteral()!= null){ Number value = Double.parseDouble(literal.IntegerLiteral().getText()); - return new NumberLiteral(TypePlaceholder.fresh(literal.getStart()), + return new Literal(TypePlaceholder.fresh(literal.getStart()), value, literal.getStart()); }else if(literal.BooleanLiteral() != null){ RefType type = new RefType(reg.getName("java.lang.Boolean"),literal.getStart()); - return new BoolLiteral(type, + return new Literal(type, Boolean.parseBoolean(literal.BooleanLiteral().getText()), literal.getStart()); }else if(literal.CharacterLiteral() != null){ RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); - return new CharLiteral(type, + return new Literal(type, literal.CharacterLiteral().getText().charAt(0), literal.getStart()); }else if(literal.StringLiteral()!=null){ RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); - return new StringLiteral(type, + return new Literal(type, literal.StringLiteral().getText(), literal.getStart()); }else if(literal.NullLiteral() != null){ - return new Null(TypePlaceholder.fresh(literal.getStart()), + return new Literal(TypePlaceholder.fresh(literal.getStart()), null, literal.getStart()); }else { throw new NotImplementedException(); diff --git a/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java b/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java index cecbc6cb..5f77601d 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java @@ -1,12 +1,6 @@ package de.dhbwstuttgart.syntaxtree; -import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.typeinference.constraints.Constraint; - -import java.lang.reflect.Type; public interface ASTVisitor extends StatementVisitor{ diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index dfb8438c..4168b587 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -1,14 +1,10 @@ package de.dhbwstuttgart.syntaxtree; -import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.type.*; -import javax.management.Notification; -import java.lang.reflect.Modifier; import java.util.Iterator; public abstract class AbstractASTWalker implements ASTVisitor{ @@ -130,7 +126,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } @Override - public void visit(Binary binary) { + public void visit(BinaryExpr binary) { } @@ -244,11 +240,6 @@ public abstract class AbstractASTWalker implements ASTVisitor{ whileStmt.loopBlock.accept(this); } - @Override - public void visit(Null aNull) { - - } - @Override public void visit(Literal literal) { diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 1c4e149c..d6e0d1d5 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -2,9 +2,6 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; -import de.dhbwstuttgart.syntaxtree.statement.literal.StringLiteral; public interface StatementVisitor { @@ -14,7 +11,7 @@ public interface StatementVisitor { void visit(Assign assign); - void visit(Binary binary); + void visit(BinaryExpr binary); void visit(Block block); @@ -54,10 +51,6 @@ public interface StatementVisitor { void visit(DoStmt whileStmt); - void visit(Null aNull); - - void visit(Literal literal); - void visit(AssignToField assignLeftSide); void visit(AssignToLocal assignLeftSide); @@ -67,4 +60,6 @@ public interface StatementVisitor { void visit(ExpressionReceiver expressionReceiver); void visit(UnaryExpr unaryExpr); + + void visit(Literal literal); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java b/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java deleted file mode 100644 index 8a0a0b35..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Binary.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; - - -// neu von Felix -public class Binary extends Expression -{ - public enum Operator{ - ADD, - SUB, - MUL, - AND, OR, DIV - } - public Binary(Expression expr1, Expression expr2, Operator op){ - super(null, null); - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index 7d9270e0..f7db0481 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -1,14 +1,41 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public abstract class BinaryExpr extends Expression +public class BinaryExpr extends Expression { + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } - public BinaryExpr(Token offset) + public enum Operator{ + ADD, + SUB, + MUL, + AND, + OR, + DIV, + LESSTHAN, + BIGGERTHAN, + LESSEQUAL, + BIGGEREQUAL + } + + public final Operator operation; + public final Expression lexpr; + public final Expression rexpr; + + public BinaryExpr(Operator operation, RefTypeOrTPHOrWildcardOrGeneric type, Expression lexpr, Expression rexpr, Token offset) { - super(null,null); + super(type,offset); + + this.operation = operation; + this.lexpr = lexpr; + this.rexpr = rexpr; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java b/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java index 154b5980..02fd667b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/InstanceOf.java @@ -3,8 +3,6 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; public class InstanceOf extends BinaryExpr @@ -14,7 +12,8 @@ public class InstanceOf extends BinaryExpr public InstanceOf(int offset,int variableLength) { - super(null); + super(null, null, null, null, null); + throw new NotImplementedException(); // #JB# 20.04.2005 } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/literal/Literal.java b/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java similarity index 52% rename from src/de/dhbwstuttgart/syntaxtree/statement/literal/Literal.java rename to src/de/dhbwstuttgart/syntaxtree/statement/Literal.java index 7c48c3ae..b6ad9e96 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/literal/Literal.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java @@ -1,19 +1,21 @@ -package de.dhbwstuttgart.syntaxtree.statement.literal; +package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import org.antlr.v4.runtime.Token; -public abstract class Literal extends Expression +public class Literal extends Expression { - public Literal(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { + public Literal(RefTypeOrTPHOrWildcardOrGeneric type, Object value, Token offset) { super(type, offset); } - public ConstraintSet getConstraints(TypeInferenceBlockInformation info){ - return new ConstraintSet(); + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index 435b1b65..f6307f1e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -2,7 +2,6 @@ package de.dhbwstuttgart.syntaxtree.statement; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/literal/BoolLiteral.java b/src/de/dhbwstuttgart/syntaxtree/statement/literal/BoolLiteral.java deleted file mode 100644 index 29bdb47d..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/literal/BoolLiteral.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement.literal; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import org.antlr.v4.runtime.Token; - - -public class BoolLiteral extends Literal -{ - - private boolean value; - - public BoolLiteral(RefTypeOrTPHOrWildcardOrGeneric type, boolean value, Token offset) - { - super(type,offset); - this.value = value; - } - - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } - - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/literal/CharLiteral.java b/src/de/dhbwstuttgart/syntaxtree/statement/literal/CharLiteral.java deleted file mode 100644 index 7e379690..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/literal/CharLiteral.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement.literal; -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import org.antlr.v4.runtime.Token; - - -public class CharLiteral extends Literal -{ - private char value; - - public CharLiteral(RefTypeOrTPHOrWildcardOrGeneric type, char value, Token offset) - { - super(type,offset); - this.value = value; - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/literal/Null.java b/src/de/dhbwstuttgart/syntaxtree/statement/literal/Null.java deleted file mode 100644 index 254408e8..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/literal/Null.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement.literal; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import org.antlr.v4.runtime.Token; - - -public class Null extends Literal -{ - - public Null(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) - { - super(type,offset); - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/literal/NumberLiteral.java b/src/de/dhbwstuttgart/syntaxtree/statement/literal/NumberLiteral.java deleted file mode 100644 index 2c90f413..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/literal/NumberLiteral.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement.literal; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import org.antlr.v4.runtime.Token; - - -public class NumberLiteral extends Literal -{ - private Number value; - - public NumberLiteral(RefTypeOrTPHOrWildcardOrGeneric type, Number value, Token offset) - { - super(type,offset); - this.value = value; - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/literal/StringLiteral.java b/src/de/dhbwstuttgart/syntaxtree/statement/literal/StringLiteral.java deleted file mode 100644 index d6ab9808..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/literal/StringLiteral.java +++ /dev/null @@ -1,23 +0,0 @@ - -package de.dhbwstuttgart.syntaxtree.statement.literal; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.syntaxtree.type.RefType; -import org.antlr.v4.runtime.Token; - - -public class StringLiteral extends Literal -{ - private String value; - - public StringLiteral(RefType type, String value, Token offset) - { - super(type,offset); - this.value = value; - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ASTTypePrinter.java b/src/de/dhbwstuttgart/syntaxtree/visual/ASTTypePrinter.java index 2dc7ae54..cbfc1a19 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/ASTTypePrinter.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ASTTypePrinter.java @@ -1,13 +1,6 @@ package de.dhbwstuttgart.syntaxtree.visual; import de.dhbwstuttgart.syntaxtree.*; -import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; -import de.dhbwstuttgart.syntaxtree.type.*; - -import java.lang.reflect.Modifier; -import java.util.Iterator; public class ASTTypePrinter extends ASTPrinter{ diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 879a4ba2..7d97d4b7 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -4,8 +4,6 @@ import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; import java.lang.reflect.Modifier; @@ -191,8 +189,10 @@ public class OutputGenerator implements ASTVisitor{ } @Override - public void visit(Binary binary) { - + public void visit(BinaryExpr binary) { + binary.lexpr.accept(this); + out.append(" op "); + binary.rexpr.accept(this); } @Override @@ -313,16 +313,6 @@ public class OutputGenerator implements ASTVisitor{ out.append(");"); } - @Override - public void visit(Null aNull) { - - } - - @Override - public void visit(Literal literal) { - - } - @Override public void visit(AssignToField assignLeftSide) { assignLeftSide.field.accept(this); @@ -367,4 +357,9 @@ public class OutputGenerator implements ASTVisitor{ out.append("++"); } } + + @Override + public void visit(de.dhbwstuttgart.syntaxtree.statement.Literal literal) { + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java index 6f90051a..8a210db1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java @@ -2,13 +2,9 @@ package de.dhbwstuttgart.syntaxtree.visual; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; +import de.dhbwstuttgart.syntaxtree.statement.Literal; import de.dhbwstuttgart.syntaxtree.type.*; -import java.lang.reflect.Modifier; -import java.util.Iterator; - public class TypeOutputGenerator extends OutputGenerator { TypeOutputGenerator(StringBuilder out){ @@ -100,8 +96,10 @@ public class TypeOutputGenerator extends OutputGenerator { } @Override - public void visit(Binary binary) { - super.visit(binary); + public void visit(BinaryExpr binary) { + binary.lexpr.accept(this); + out.append(" | "); + binary.rexpr.accept(this); } @Override @@ -219,11 +217,6 @@ public class TypeOutputGenerator extends OutputGenerator { super.visit(whileStmt); } - @Override - public void visit(Null aNull) { - super.visit(aNull); - } - @Override public void visit(Literal literal) { super.visit(literal); diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java index 0eba225d..a04ce9bb 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.typeinference.assumptions; -import com.sun.org.apache.regexp.internal.RE; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; import de.dhbwstuttgart.parser.scope.GenericTypeName; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index ed5837ad..423a4f65 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -1,8 +1,5 @@ package de.dhbwstuttgart.typeinference.typeAlgo; -//import com.sun.org.apache.xpath.internal.Arg; - -import com.sun.org.apache.xpath.internal.operations.Bool; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; @@ -11,8 +8,6 @@ import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.*; -import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; -import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; import de.dhbwstuttgart.typeinference.assumptions.FunNClass; @@ -87,7 +82,7 @@ public class TYPEStmt implements StatementVisitor{ } @Override - public void visit(Binary binary) { + public void visit(BinaryExpr binary) { throw new NotImplementedException(); } @@ -212,6 +207,11 @@ public class TYPEStmt implements StatementVisitor{ } } + @Override + public void visit(Literal literal) { + throw new NotImplementedException(); + } + @Override public void visit(Return returnExpr) { returnExpr.retexpr.accept(this); @@ -263,7 +263,11 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(WhileStmt whileStmt) { RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); + //Expression inferieren: + whileStmt.expr.accept(this); + //Expression muss boolean sein: constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); + //LoopBlock inferieren: whileStmt.loopBlock.accept(this); } @@ -272,17 +276,6 @@ public class TYPEStmt implements StatementVisitor{ throw new NotImplementedException(); } - @Override - public void visit(Null aNull) { - throw new NotImplementedException(); - } - - @Override - public void visit(Literal literal) { - //Hier gibt es nichts zu tun - //throw new NotImplementedException(); - } - @Override public void visit(AssignToField assignLeftSide) { //Hier ist kein Code nötig. Es werden keine extra Constraints generiert From 15d120d59481865172fcced93afb632d7f3e6b65 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 31 Jan 2018 15:51:38 +0100 Subject: [PATCH 17/17] =?UTF-8?q?Funktionsf=C3=A4higes=20Matrix=20Beispiel?= =?UTF-8?q?,=20aber=20nicht=20komplett?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/dhbwstuttgart/core/JavaTXCompiler.java | 2 +- src/de/dhbwstuttgart/syntaxtree/statement/Literal.java | 3 +++ src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java | 2 +- src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 86666a49..e8d74cb6 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -82,7 +82,7 @@ public class JavaTXCompiler { List allClasses = new ArrayList<>();//environment.getAllAvailableClasses(); //Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC for(SourceFile sf : this.sourceFiles.values()) { - //allClasses.addAll(getAvailableClasses(sf)); + allClasses.addAll(getAvailableClasses(sf)); allClasses.addAll(sf.getClasses()); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java b/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java index b6ad9e96..8bbaae0a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Literal.java @@ -10,8 +10,11 @@ import org.antlr.v4.runtime.Token; public class Literal extends Expression { + public final Object value; + public Literal(RefTypeOrTPHOrWildcardOrGeneric type, Object value, Token offset) { super(type, offset); + this.value = value; } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 7d97d4b7..c7a06502 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -360,6 +360,6 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(de.dhbwstuttgart.syntaxtree.statement.Literal literal) { - throw new NotImplementedException(); + out.append(literal.value); } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 423a4f65..a06062c0 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -83,7 +83,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(BinaryExpr binary) { - throw new NotImplementedException(); + //TODO } @Override @@ -209,7 +209,7 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(Literal literal) { - throw new NotImplementedException(); + //Nothing to do here. Literale kriegen beim parsen den korrekten Typ. } @Override
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr