diff --git a/pom.xml b/pom.xml index 371f5190..5858aaee 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,12 @@ asm-all [4.0.0,) + @@ -66,12 +72,24 @@ src/de/dhbwstuttgart/parser/antlr/ src/de/dhbwstuttgart/parser/antlr/ + -package de.dhbwstuttgart.parser.antlr + + + + aspParser + + antlr4 + + + src/de/dhbwstuttgart/sat/asp/parser/antlr/ + src/de/dhbwstuttgart/sat/asp/parser/antlr/ -package - de.dhbwstuttgart.parser.antlr + de.dhbwstuttgart.sat.asp.parser.antlr + @@ -126,6 +144,14 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 2c68ad43..f24bb3fd 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -10,6 +10,10 @@ import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString; +import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; +import de.dhbwstuttgart.bytecode.signature.Signature; +import de.dhbwstuttgart.bytecode.signature.TypeToString; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.Literal; @@ -51,9 +55,9 @@ public class BytecodeGen implements ASTVisitor { @Override public void visit(SourceFile sourceFile) { for(ClassOrInterface cl : sourceFile.getClasses()) { + System.out.println("in Class: " + cl.getClassName().toString()); BytecodeGen classGen = new BytecodeGen(classFiles, resultSet); cl.accept(classGen); - System.out.println("In CLASS: "+(cl.getClassName().toString())); classGen.writeClass(cl.getClassName().toString()); } } @@ -79,9 +83,10 @@ public class BytecodeGen implements ASTVisitor { public void visit(ClassOrInterface classOrInterface) { className = classOrInterface.getClassName().toString(); - isInterface = (classOrInterface.getModifiers()&512)==512; - System.out.println("IS Interface = "+"modifiers= "+classOrInterface.getModifiers()+" ->"+(classOrInterface.getModifiers()&512) + isInterface); + cw.visitSource(className +".jav", null); + isInterface = (classOrInterface.getModifiers()&512)==512; + int acc = isInterface?classOrInterface.getModifiers()+Opcodes.ACC_ABSTRACT:classOrInterface.getModifiers()+Opcodes.ACC_SUPER; String sig = null; /* if class has generics then creates signature @@ -90,8 +95,6 @@ public class BytecodeGen implements ASTVisitor { */ if(classOrInterface.getGenerics().iterator().hasNext()) { Signature signature = new Signature(classOrInterface, genericsAndBounds); - - System.out.println(signature.toString()); sig = signature.toString(); } // needs implemented Interfaces? @@ -100,8 +103,6 @@ public class BytecodeGen implements ASTVisitor { // for each field in the class for(Field f : classOrInterface.getFieldDecl()) { - System.out.println("get Fields"); - System.out.println(f.getName()); f.accept(this); } @@ -112,7 +113,6 @@ public class BytecodeGen implements ASTVisitor { for(Method m : classOrInterface.getMethods()) { m.accept(this); } - cw.visitSource(classOrInterface.getClassName().toString()+".jav", null); } @Override @@ -121,24 +121,25 @@ public class BytecodeGen implements ASTVisitor { String desc = null; boolean hasGen = false; + for(String paramName : methodParamsAndTypes.keySet()) { - genericsAndBounds.containsKey(paramName); - hasGen = true; + String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); + if(genericsAndBounds.containsKey(typeOfParam)) { + hasGen = true; + break; + } } 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,cw, - genericsAndBoundsMethod,genericsAndBounds,isInterface); + genericsAndBoundsMethod,genericsAndBounds,isInterface,classFiles); if(!field.getParameterList().iterator().hasNext()) { mv.visitInsn(Opcodes.RETURN); } @@ -155,45 +156,41 @@ public class BytecodeGen implements ASTVisitor { String methDesc = null; // Method getModifiers() ? - int acc = isInterface?Opcodes.ACC_ABSTRACT:0; + int acc = isInterface?Opcodes.ACC_ABSTRACT:method.modifier; - System.out.println("-----Method-----"); - - boolean hasGenInParameterList = genericsAndBounds.containsKey(method.getReturnType().acceptTV(new TypeToDescriptor())); + boolean hasGenInParameterList = genericsAndBounds.containsKey(resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor())); if(!hasGenInParameterList) { for(String paramName : methodParamsAndTypes.keySet()) { - if(genericsAndBounds.containsKey(paramName)) { + String typeOfParam = methodParamsAndTypes.get(paramName).acceptTV(new TypeToDescriptor()); + if(genericsAndBounds.containsKey(typeOfParam)) { hasGenInParameterList = true; break; } } } + + //TODO: Test if the return-type or any of the parameter is a parameterized type. (VP) + //than create the descriptor with the new syntax. + 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) { + + /* if method has generics or return type is TPH, create signature */ + if(hasGen||method.getReturnType().acceptTV(new TypeToString()).equals("TPH")) { // resultset hier zum testen Signature signature = new Signature(method, genericsAndBoundsMethod, methodParamsAndTypes,resultSet); sig = signature.toString(); - System.out.println(sig); - } - + System.out.println(sig); NormalMethod meth = new NormalMethod(method,genericsAndBounds,genericsAndBoundsMethod,hasGen); methDesc = meth.accept(new DescriptorToString(resultSet)); - System.out.println("methDesc" + methDesc); + System.out.println(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,cw,genericsAndBounds,genericsAndBounds,isInterface); + BytecodeGenMethod gen = new BytecodeGenMethod(className,resultSet,method, mv,paramsAndLocals,cw, + genericsAndBounds,genericsAndBounds,isInterface,classFiles); mv.visitMaxs(0, 0); mv.visitEnd(); } @@ -207,7 +204,7 @@ public class BytecodeGen implements ASTVisitor { while(itr.hasNext()) { FormalParameter fp = itr.next(); paramsAndLocals.put(fp.getName(), i); - methodParamsAndTypes.put(fp.getName(), fp.getType()); + methodParamsAndTypes.put(fp.getName(), resultSet.resolveType(fp.getType()).resolvedType); fp.accept(this); i++; } @@ -250,7 +247,6 @@ public class BytecodeGen implements ASTVisitor { // ?? @Override public void visit(FieldVar fieldVar) { - System.out.println("in fieldvar"); // cw.newField(fieldVar.receiver.toString(), fieldVar.fieldVarName.toString(), fieldVar.getType().toString()); FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, fieldVar.fieldVarName, "L"+fieldVar.getType()+";", null, null); fv.visitEnd(); @@ -259,7 +255,6 @@ public class BytecodeGen implements ASTVisitor { // access flages?? modifiers @Override public void visit(Field field) { - System.out.println("in field"); FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, field.getName(), "L"+field.getType().toString().replace(".", "/")+";", null, null); fv.visitEnd(); } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index 8cbb64fa..df714015 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -1,5 +1,9 @@ package de.dhbwstuttgart.bytecode; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.lang.invoke.CallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; @@ -15,12 +19,21 @@ import org.objectweb.asm.Handle; 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.bytecode.descriptor.DescriptorToString; +import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; +import de.dhbwstuttgart.bytecode.signature.Signature; +import de.dhbwstuttgart.bytecode.signature.TypeToSignature; +import de.dhbwstuttgart.bytecode.signature.TypeToString; 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; + import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -30,28 +43,27 @@ public class BytecodeGenMethod implements StatementVisitor{ private MethodVisitor mv; private HashMap paramsAndLocals = new HashMap<>(); private String className; - private int lamCounter; + private int lamCounter = -1; private ClassWriter cw; private ResultSet resultSet; private boolean isInterface; HashMap genericsAndBoundsMethod; private HashMap genericsAndBounds; + private boolean isBinaryExp = false; //for tests ** private String fieldName; private String fieldDesc; private Expression rightSideTemp; - private String where; private boolean isRightSideALambda = false; private KindOfLambda kindOfLambda; + private HashMap classFiles; - private ArrayList varsFunInterface; + private ArrayList varsFunInterface = new ArrayList<>();; public BytecodeGenMethod(String className,ResultSet resultSet, Method m, MethodVisitor mv, - HashMap paramsAndLocals, ClassWriter cw, - HashMap genericsAndBoundsMethod, HashMap genericsAndBounds, boolean isInterface) { - - this.where = "<<<<<< NORMAL METHOD >>>>>>"; + HashMap paramsAndLocals, ClassWriter cw, HashMap genericsAndBoundsMethod, + HashMap genericsAndBounds, boolean isInterface, HashMap classFiles) { this.className = className; this.resultSet = resultSet; @@ -62,31 +74,20 @@ public class BytecodeGenMethod implements StatementVisitor{ this.genericsAndBoundsMethod = genericsAndBoundsMethod; this.genericsAndBounds = genericsAndBounds; this.isInterface = isInterface; - this.lamCounter = -1; - - this.varsFunInterface = new ArrayList<>(); - System.out.println("PARAMS = "+this.paramsAndLocals.size()); + this.classFiles = classFiles; if(!isInterface) this.m.block.accept(this); -// System.out.println("PARAMS = "+this.paramsAndLocals.size()); -// for(int i = 0; i classFiles) { - this.where = "<<<<<< LAMBDA METHOD >>>>>>"; this.resultSet = resultSet; this.mv = mv; this.isInterface = isInterface; - this.lamCounter = -1; - this.varsFunInterface = new ArrayList<>(); + this.classFiles = classFiles; Iterator itr = lambdaExpression.params.iterator(); int i = indexOfFirstParamLam; @@ -99,7 +100,6 @@ public class BytecodeGenMethod implements StatementVisitor{ } private String getResolvedType(RefTypeOrTPHOrWildcardOrGeneric type) { -// return resultSet.resolveType(type).resolvedType.toString().replace(".", "/"); return resultSet.resolveType(type).resolvedType.acceptTV(new TypeToDescriptor()); } @@ -107,10 +107,8 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Block block) { for(Statement stmt : block.getStatements()) { - System.out.println(where); - System.out.println("Stmt : " + stmt.toString()); +// System.out.println(where); stmt.accept(this); - System.out.println("--------------------------\n"); } } @@ -118,58 +116,67 @@ public class BytecodeGenMethod implements StatementVisitor{ public void visit(SuperCall superCall) { 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, "()V",isInterface); } // ?? @Override public void visit(LocalVar localVar) { - System.out.println("in Local Var: " + localVar.name); mv.visitVarInsn(Opcodes.ALOAD, paramsAndLocals.get(localVar.name)); + if(isBinaryExp) { + getVlaue(getResolvedType(localVar.getType())); + } } // ?? @Override public void visit(LocalVarDecl localVarDecl) { -// Integer i; -// paramsAndLocals.put(localVarDecl.getName(), paramsAndLocals.size()+1); - System.out.println("In localVarDecl :: "+localVarDecl.getName()); + } @Override public void visit(Assign assign) { - System.out.println("Assign : \nright = "+assign.rightSide + "\nLeft = " + assign.lefSide); - // if the right side is a lambda => the left side must be a functional interface - if(assign.rightSide.getClass().equals(LambdaExpression.class)) { + if(assign.rightSide instanceof LambdaExpression) { isRightSideALambda = true; }else { isRightSideALambda = false; } - System.out.println("\t isRight Side lambda: " + isRightSideALambda); - if(assign.lefSide.getClass().equals(AssignToField.class)) { + if(assign.rightSide instanceof BinaryExpr) + isBinaryExp = true; + + if(assign.lefSide instanceof AssignToField) { // load_0, ldc or .. then putfield this.rightSideTemp = assign.rightSide; - assign.lefSide.accept(this); }else { assign.rightSide.accept(this); - assign.lefSide.accept(this); } + if(isBinaryExp) { + doAssign(getResolvedType(assign.lefSide.getType())); + isBinaryExp = false; + } + assign.lefSide.accept(this); } @Override public void visit(BinaryExpr binary) { - System.out.println("\t++ In Binary: "); + binary.lexpr.accept(this); + binary.rexpr.accept(this); + switch (binary.operation.toString()) { + case "ADD": + mv.visitInsn(Opcodes.IADD); + break; + + default: + break; + } + } @Override 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 @@ -181,10 +188,20 @@ 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;"; + + // Für die Parameter-Typen und Return-Typ braucht man die Bounds (für die Typlöschung) + + String typeErasure = "("; + Iterator itr = lambdaExpression.params.iterator(); + while(itr.hasNext()) { + itr.next(); + typeErasure += "L"+Type.getInternalName(Object.class) + ";"; + } + + typeErasure += ")L"+Type.getInternalName(Object.class) + ";"; // Type erasure -// Type arg1 = Type.getMethodType(typeErasure); - Type arg1 = Type.getMethodType(lamDesc); + Type arg1 = Type.getMethodType(typeErasure); +// Type arg1 = Type.getMethodType(lamDesc); // real Type Type arg3 = Type.getMethodType(lamDesc); @@ -209,20 +226,64 @@ public class BytecodeGenMethod implements StatementVisitor{ SamMethod samMethod = new SamMethod(kindOfLambda.getArgumentList(), lambdaExpression.getType()); // Desc: (this/nothing)TargetType String fiMethodDesc = samMethod.accept(new DescriptorToString(resultSet)); - mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, - arg1, arg2,arg3); + mv.visitInvokeDynamicInsn("apply", fiMethodDesc, bootstrap, arg1, arg2,arg3); MethodVisitor mvLambdaBody = cw.visitMethod(Opcodes.ACC_PRIVATE+ staticOrInstance + Opcodes.ACC_SYNTHETIC, methodName, arg3.toString(), null, null); - new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,indexOfFirstParamLam,isInterface); + new BytecodeGenMethod(lambdaExpression,this.resultSet,mvLambdaBody,indexOfFirstParamLam,isInterface, + classFiles); mvLambdaBody.visitMaxs(0, 0); mvLambdaBody.visitEnd(); cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); + +// generateBCForFunN(lambdaExpression,typeErasure); } + private void generateBCForFunN(LambdaExpression lambdaExpression, String methDesc) { + ClassWriter classWriter =new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); + + SignatureWriter methSig = new SignatureWriter(); + + int numberOfParams = 0; + SignatureVisitor paramVisitor = methSig.visitParameterType(); + Iterator itr = lambdaExpression.params.iterator(); + while(itr.hasNext()) { + numberOfParams++; + // getBounds + paramVisitor.visitTypeVariable("T"+numberOfParams); + itr.next(); + } + methSig.visitReturnType().visitTypeVariable("R"); + // ")"+lam.getReturn.getBounds + Signature sig = new Signature(lambdaExpression,numberOfParams); + String name = "Fun"+numberOfParams; + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_INTERFACE+Opcodes.ACC_ABSTRACT, name, + sig.toString(), Type.getInternalName(Object.class), null); + MethodVisitor mvApply = classWriter.visitMethod(Opcodes.ACC_PUBLIC+Opcodes.ACC_ABSTRACT, "apply", + methDesc, methSig.toString(), null); + mvApply.visitEnd(); + writeClassFile(classWriter.toByteArray(),name); + } + + public void writeClassFile(byte[] bytecode, String name) { + FileOutputStream output; + try { + System.out.println("generating "+name+ ".class file..."); + output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/generatedBC/examples/" +name+".class")); + output.write(bytecode); + output.close(); + System.out.println(name+".class file generated"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + +} + @Override public void visit(CastExpr castExpr) { // TODO Auto-generated method stub @@ -237,7 +298,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(FieldVar fieldVar) { - System.out.println("in fieldVar " + fieldVar.fieldVarName + " ** receiver: "+fieldVar.receiver); fieldName = fieldVar.fieldVarName; fieldDesc = "L"+getResolvedType(fieldVar.getType())+";"; @@ -261,7 +321,7 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(IfStmt ifStmt) { - System.out.println("++ IF-Statment: "); + System.out.println("If"); } @Override @@ -272,14 +332,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(MethodCall methodCall) { - System.out.println(" In Methodcall: (" +methodCall.name+")" ); - System.out.print("\t\tMethod-Receiver: "); - if(methodCall.receiver instanceof ExpressionReceiver){ - System.out.print(((ExpressionReceiver) methodCall.receiver).expr + "\n"); - }else{ - System.out.print(((StaticClassName) methodCall.receiver).getType().toString() + "\n"); - } - methodCall.receiver.accept(this); methodCall.arglist.accept(this); @@ -287,14 +339,11 @@ public class BytecodeGenMethod implements StatementVisitor{ genericsAndBoundsMethod,genericsAndBounds); 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, false); }else { - System.out.println("mDesc = " + mDesc); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getResolvedType(methodCall.receiver.getType()), methodCall.name, mDesc, isInterface); } @@ -306,8 +355,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(NewClass methodCall) { - System.out.println("In NewClass: "); - System.out.println("\t\tname: " + methodCall.name + " *** " + "Receiver: " + methodCall.receiver); mv.visitTypeInsn(Opcodes.NEW, methodCall.name.replace(".", "/")); mv.visitInsn(Opcodes.DUP); @@ -330,14 +377,12 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(ExpressionReceiver receiver) { - System.out.println(" in Receiver"); - System.out.println(" expr : " + receiver.expr); receiver.expr.accept(this); } @Override public void visit(UnaryExpr unaryExpr) { - throw new NotImplementedException(); + System.out.println(unaryExpr.operation.toString()); } @Override @@ -353,7 +398,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(StaticClassName staticClassName) { - System.out.println("In StaticClassName: "); // mv.visitMethodInsn(Opcodes.INVOKESTATIC, staticClassName.getType().toString().replace(".", "/"), // staticClassName.toString(), staticClassName.getType().toString(), false); mv.visitFieldInsn(Opcodes.GETSTATIC, getResolvedType(staticClassName.getType()), @@ -362,19 +406,18 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Super aSuper) { - System.out.println(">> In Super: "); + } @Override public void visit(This aThis) { - System.out.println("-> IN This"); mv.visitVarInsn(Opcodes.ALOAD, 0); } @Override public void visit(WhileStmt whileStmt) { - // TODO Auto-generated method stub - + whileStmt.expr.accept(this); + whileStmt.loopBlock.accept(this); } @Override @@ -385,17 +428,209 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(Literal literal) { - // value? - mv.visitLdcInsn(getResolvedType(literal.getType())); + Object value = literal.value; + String typeOfLiteral = resultSet.resolveType(literal.getType()).resolvedType.acceptTV(new TypeToDescriptor()); + if(this.isBinaryExp) { + getVlaue(typeOfLiteral); + }else { + doAssign(typeOfLiteral, value); + } + + + } + + private void getVlaue(String typeOfLiteral) { + switch (typeOfLiteral) { + case "java/lang/String": + break; + case "java/lang/Boolean": + break; + case "java/lang/Byte": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", + "(B)Ljava/lang/Byte;", false); + break; + case "java/lang/Short": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", + "(S)Ljava/lang/Short;", false); + break; + case "java/lang/Integer": + mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", + "()I", false); + break; + case "java/lang/Long": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", + "(J)Ljava/lang/Long;", false); + break; + case "java/lang/Float": + break; + case "java/lang/Double": + break; + case "java/lang/Character": + break; + default: + break; + } + } + + private void doAssign(String type, Object value) { + switch (type) { + case "java/lang/String": + mv.visitLdcInsn(String.valueOf(value)); + break; + case "java/lang/Boolean": + visitBooleanLiteral((Boolean) value); + break; + case "java/lang/Byte": + visitByteLiteral(((Double) value).byteValue(),false); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", + "(B)Ljava/lang/Byte;", false); + break; + case "java/lang/Short": + visitShortLiteral(((Double) value).shortValue(),false); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", + "(S)Ljava/lang/Short;", false); + break; + case "java/lang/Integer": + //zweite Argument isLong + visitIntegerLiteral(((Double) value).intValue(), false); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", + "(I)Ljava/lang/Integer;", false); + break; + case "java/lang/Long": + visitLongLiteral(((Double) value).longValue(), true); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", + "(J)Ljava/lang/Long;", false); + break; + case "java/lang/Float": + visitFloatLiteral(((Double) value).floatValue()); + break; + case "java/lang/Double": + visitDoubleLiteral((Double) value); + break; + case "java/lang/Character": + visitCharLiteral((Character) value); + break; + default: + break; + } + } + + private void doAssign(String type) { + switch (type) { + case "java/lang/String": + break; + case "java/lang/Boolean": + break; + case "java/lang/Byte": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", + "(B)Ljava/lang/Byte;", false); + break; + case "java/lang/Short": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", + "(S)Ljava/lang/Short;", false); + break; + case "java/lang/Integer": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", + "(I)Ljava/lang/Integer;", false); + break; + case "java/lang/Long": + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", + "(J)Ljava/lang/Long;", false); + break; + case "java/lang/Float": + break; + case "java/lang/Double": + break; + case "java/lang/Character": + break; + default: + break; + } + } + + private void visitCharLiteral(Character value) { + mv.visitIntInsn(Opcodes.BIPUSH, (int) value); + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", + "(C)Ljava/lang/Character;", false); + } + + private void visitDoubleLiteral(Double value) { + if(value == 0) { + mv.visitInsn(Opcodes.DCONST_0); + }else if(value == 1) { + mv.visitInsn(Opcodes.DCONST_1); + }else { + mv.visitLdcInsn(value); + } + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", + "(D)Ljava/lang/Double;", false); + } + + private void visitFloatLiteral(Float value) { + if(value.intValue()>-1 && value.intValue() < 3) { + //Opcodes.FCONST_0 = 11, Opcodes.FCONST_1 = 12, usw + mv.visitInsn(value.intValue()+11); + }else { + mv.visitLdcInsn(value); + } + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", + "(F)Ljava/lang/Float;", false); + } + + private void visitLongLiteral(Long value, boolean isLong) { + if(value=-Math.pow(2, 15))&&value<-128) { + visitShortLiteral(value.shortValue(),isLong); + }else { + mv.visitLdcInsn(value); + } + } + + private void visitShortLiteral(Short value,boolean isLong) { + if(value< 128 || (value>-129 && value<-1)) { + visitByteLiteral(value.byteValue(), isLong); + }else if(value=-Math.pow(2, 15))&&value<-128) { + mv.visitIntInsn(Opcodes.SIPUSH, value); + } + } + + private void visitByteLiteral(Byte value, boolean isLong) { + + if(!isLong && value<6 && value>-1) { + //Opcodes.ICONST_0 = 3, Opcodes.ICONST_1 = 4, usw + mv.visitInsn(value+3); + }else if(isLong && value>-1 && value<2){ + //Opcodes.LCONST_0 = 9, Opcodes.LCONST_1 = 10 + mv.visitInsn(value+9); + }else { + mv.visitIntInsn(Opcodes.BIPUSH, value); + } + + } + + private void visitIntegerLiteral(Integer value, boolean isLong) { + + if(value=-Math.pow(2, 15))&&value<-128) { + visitShortLiteral(value.shortValue(),isLong); + }else { + mv.visitLdcInsn(value); + } + } + + private void visitBooleanLiteral(Boolean b) { + if(b) { + mv.visitInsn(Opcodes.ICONST_1); + }else { + mv.visitInsn(Opcodes.ICONST_0); + } + mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", + "(Z)Ljava/lang/Boolean;", false); } @Override public void visit(ArgumentList argumentList) { - System.out.println("in ArgumentList: "); for(Expression al : argumentList.getArguments()) { al.accept(this); } - System.out.println("out from Argumentlist"); } @Override @@ -414,7 +649,6 @@ public class BytecodeGenMethod implements StatementVisitor{ @Override public void visit(AssignToLocal assignLeftSide) { - System.out.println("In Assign To Local: "); if(isRightSideALambda) varsFunInterface.add(assignLeftSide.localVar.getType()); paramsAndLocals.put(assignLeftSide.localVar.name, paramsAndLocals.size()+1); diff --git a/src/de/dhbwstuttgart/bytecode/ClassFile.java b/src/de/dhbwstuttgart/bytecode/ClassFile.java deleted file mode 100644 index fcbad90b..00000000 --- a/src/de/dhbwstuttgart/bytecode/ClassFile.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class ClassFile { - - String name; - byte[] bytecode; - - public ClassFile(String name, byte[] bytecode) { - this.name = name; - this.bytecode = bytecode; - } - - -} diff --git a/src/de/dhbwstuttgart/bytecode/Lambda.java b/src/de/dhbwstuttgart/bytecode/Lambda.java index 283b7393..c3b4e8f5 100644 --- a/src/de/dhbwstuttgart/bytecode/Lambda.java +++ b/src/de/dhbwstuttgart/bytecode/Lambda.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; diff --git a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java index 330b0666..0f781b46 100644 --- a/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java +++ b/src/de/dhbwstuttgart/bytecode/MethodFromMethodCall.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; diff --git a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java index d2174fd4..a9aa482a 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalConstructor.java +++ b/src/de/dhbwstuttgart/bytecode/NormalConstructor.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.ParameterList; diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index 16e84cc5..5afc31fb 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; diff --git a/src/de/dhbwstuttgart/bytecode/SamMethod.java b/src/de/dhbwstuttgart/bytecode/SamMethod.java index 9cf039a2..f14d5407 100644 --- a/src/de/dhbwstuttgart/bytecode/SamMethod.java +++ b/src/de/dhbwstuttgart/bytecode/SamMethod.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.bytecode; import java.util.List; +import de.dhbwstuttgart.bytecode.descriptor.DescriptorVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; public class SamMethod { diff --git a/src/de/dhbwstuttgart/bytecode/Test.java b/src/de/dhbwstuttgart/bytecode/Test.java deleted file mode 100644 index 333ec41f..00000000 --- a/src/de/dhbwstuttgart/bytecode/Test.java +++ /dev/null @@ -1,150 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.lang.invoke.CallSite; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; - -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Handle; -import org.objectweb.asm.Label; -import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.Type; - - -public class Test { - private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/"; - - protected static ClassLoader getClassLoader() throws Exception { - File file = new File(rootDirectory); - URL url = file.toURI().toURL(); - URL[] urls = new URL[] { url }; - System.out.println(urls[0]); - return new URLClassLoader(urls); - } - - public static void main(String[] args) { - // Test Lambda - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TestClass", null, "java/lang/Object", null); - - cw.visitSource("TestClass.java", null); - - // Create Constructor - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); - mv.visitVarInsn(Opcodes.ALOAD, 0); - - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); - - // mv.visitMethodInsn(INVOKEDYNAMIC, "#0", "run", "()Ljava/lang/Runnable"); - //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, - MethodType.class, MethodType.class, MethodHandle.class, MethodType.class); - - Handle bootstrap = new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", - mt.toMethodDescriptorString()); - Handle arg2 = new Handle(Opcodes.H_INVOKESTATIC, "TestClass", "lambda$0", "()V"); - - mv.visitInvokeDynamicInsn("run", "()Ljava/lang/Runnable;", bootstrap, - Type.getMethodType("()V"), arg2, - Type.getMethodType("()V")); - - mv.visitVarInsn(Opcodes.ASTORE, 1); - mv.visitVarInsn(Opcodes.ALOAD, 1); - mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/lang/Runnable", "run", "()V"); - mv.visitInsn(Opcodes.RETURN); - - // creates bridge method, contains lambdas body - MethodVisitor mvl = cw.visitMethod(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC, "lambda$0", - "()V", null, null); - mvl.visitCode(); - mvl.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); - mvl.visitLdcInsn("lambda"); - mvl.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"); - mvl.visitInsn(Opcodes.RETURN); - mvl.visitMaxs(2, 0); - mvl.visitEnd(); - - mv.visitMaxs(1, 2); - mv.visitEnd(); - - - cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup", "java/lang/invoke/MethodHandles", "Lookup", - Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL); - cw.visitEnd(); - - byte[] b = cw.toByteArray(); - - // Test if statement - /* - * ClassWriter cw = new - * ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); - * - * cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC+Opcodes.ACC_SUPER, "TestIf", null, - * "java/lang/Object", null); MethodVisitor mv = - * cw.visitMethod(Opcodes.ACC_PUBLIC, "", "(Ljava/lang/Boolean;)V", null, - * null); mv.visitCode(); - * - * // Label l0 = new Label(); // mv.visitLabel(l0); - * - * mv.visitVarInsn(Opcodes.ALOAD, 0); - * - * mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", - * "()V"); - * - * // Label l1 = new Label(); // mv.visitLabel(l1); - * mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, - * "java/lang/Boolean", "booleanValue", "()Z"); - * - * Label label = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, label); - * - * mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", - * "Ljava/io/PrintStream;"); mv.visitLdcInsn("1"); - * mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", - * "(Ljava/lang/String;)V"); - * - * Label endLabel = new Label(); mv.visitJumpInsn(Opcodes.GOTO, endLabel); - * - * mv.visitLabel(label); mv.visitFieldInsn(Opcodes.GETSTATIC, - * "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitLdcInsn("0"); - * mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", - * "(Ljava/lang/String;)V"); - * - * - * - * mv.visitLabel(endLabel); mv.visitInsn(Opcodes.RETURN); - * - * // Label l2 = new Label(); // mv.visitLabel(l2); - * - * // mv.visitLocalVariable("this", "LTestIf;", null, l0, l2, 0); // - * mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1); - * mv.visitMaxs(2, 2); mv.visitEnd(); - * - * cw.visitEnd(); byte[] b = cw.toByteArray(); - */ - FileOutputStream output; - - try { - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TestClass.class")); - output.write(b); - output.close(); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TestClass.java b/src/de/dhbwstuttgart/bytecode/TestClass.java deleted file mode 100644 index 168447af..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestClass.java +++ /dev/null @@ -1,8 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TestClass { - public TestClass() { - Runnable lam = () -> System.out.println("lambda"); - lam.run(); - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TestFields.java b/src/de/dhbwstuttgart/bytecode/TestFields.java deleted file mode 100644 index 652b8d19..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestFields.java +++ /dev/null @@ -1,59 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; - -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; - -public class TestFields { - private static final String rootDirectory = System.getProperty("user.dir") + "/bin/de/dhbwstuttgart/bytecode/"; - - public static void main(String[] args) { - // TODO Auto-generated method stub - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "TetsF", null, "java/lang/Object", null); - - cw.visitSource("TetsF.java", null); - - FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, "z", Type.INT_TYPE.getDescriptor(), null, null); - fv.visitEnd(); - - FieldVisitor fvS = cw.visitField(Opcodes.ACC_PUBLIC, "s", "Ljava/lang/String;", null, null); - fvS.visitEnd(); - // Create Constructor - MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "", "()V", null, null); - mv.visitCode(); - - mv.visitVarInsn(Opcodes.ALOAD, 0); - mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "", "()V"); - mv.visitVarInsn(Opcodes.ALOAD, 0); - mv.visitLdcInsn(""); - mv.visitFieldInsn(Opcodes.PUTFIELD, "TetsF", "s", "Ljava/lang/String;"); - mv.visitInsn(Opcodes.RETURN); - mv.visitMaxs(2, 1); - mv.visitEnd(); - - byte[] b = cw.toByteArray(); - - FileOutputStream output; - - try { - output = new FileOutputStream(new File(System.getProperty("user.dir") + "/testBytecode/TetsF.class")); - output.write(b); - output.close(); - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - -} diff --git a/src/de/dhbwstuttgart/bytecode/TestIf.java b/src/de/dhbwstuttgart/bytecode/TestIf.java deleted file mode 100644 index 36e3c706..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestIf.java +++ /dev/null @@ -1,11 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TestIf { - public TestIf(Boolean b) { - if(b) { - System.out.println("1"); - }else { - System.out.println("0"); - } - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TestMeth.java b/src/de/dhbwstuttgart/bytecode/TestMeth.java deleted file mode 100644 index 51223ea1..00000000 --- a/src/de/dhbwstuttgart/bytecode/TestMeth.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TestMeth { - private int z; - public String s; - public TestMeth(int temp) { - this.z = temp; - } - public void m1(int a, int b) { - int y = m2(1,2,3,4); - } - - public int m2(int a, int b, int x, int y) { - Integer c = 55; - Integer g; - return a+b+y+c; - } -} diff --git a/src/de/dhbwstuttgart/bytecode/TetsF.java b/src/de/dhbwstuttgart/bytecode/TetsF.java deleted file mode 100644 index af72a8d3..00000000 --- a/src/de/dhbwstuttgart/bytecode/TetsF.java +++ /dev/null @@ -1,6 +0,0 @@ -package de.dhbwstuttgart.bytecode; - -public class TetsF { - private int z; - public String s = ""; -} diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java similarity index 85% rename from src/de/dhbwstuttgart/bytecode/DescriptorToString.java rename to src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java index 481dbc5d..c9725a82 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorToString.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorToString.java @@ -1,9 +1,16 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.descriptor; import java.util.Iterator; +import de.dhbwstuttgart.bytecode.Lambda; +import de.dhbwstuttgart.bytecode.MethodFromMethodCall; +import de.dhbwstuttgart.bytecode.NormalConstructor; +import de.dhbwstuttgart.bytecode.NormalMethod; +import de.dhbwstuttgart.bytecode.SamMethod; +import de.dhbwstuttgart.bytecode.signature.TypeToSignature; import de.dhbwstuttgart.syntaxtree.FormalParameter; import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -30,6 +37,8 @@ public class DescriptorToString implements DescriptorVisitor{ Iterator itr = method.getParameterList().iterator(); while(itr.hasNext()) { FormalParameter fp = itr.next(); +// System.out.println(resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToSignature())); +// System.out.println("Parmetrisierte typ ? "+ ((RefType) fp.getType()).getParaList().size()); if(method.hasGen()) { String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); if(method.getGenericsAndBoundsMethod().containsKey(fpDesc)) { @@ -39,7 +48,11 @@ public class DescriptorToString implements DescriptorVisitor{ }else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } - }else { + } +// else if(((RefType) fp.getType()).getParaList().size() > 0){ +// desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.toString().replace(".", "%").replace("<", "%%").replace(">", "%%")+ ";"; +// } + else { desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } } @@ -71,16 +84,16 @@ public class DescriptorToString implements DescriptorVisitor{ while(itr.hasNext()) { FormalParameter fp = itr.next(); if(constructor.hasGen()) { - System.out.println("Cons has Gens"); +// System.out.println("Cons has Gens"); String fpDesc = fp.getType().acceptTV(new TypeToDescriptor()); - System.out.println(fpDesc); +// 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"); +// System.out.println("Cons has NOT Gens"); desc += "L"+resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor())+ ";"; } } @@ -96,7 +109,6 @@ 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; } diff --git a/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java similarity index 50% rename from src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java rename to src/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java index 63198828..6bc57f21 100644 --- a/src/de/dhbwstuttgart/bytecode/DescriptorVisitor.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/DescriptorVisitor.java @@ -1,4 +1,10 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.descriptor; + +import de.dhbwstuttgart.bytecode.Lambda; +import de.dhbwstuttgart.bytecode.MethodFromMethodCall; +import de.dhbwstuttgart.bytecode.NormalConstructor; +import de.dhbwstuttgart.bytecode.NormalMethod; +import de.dhbwstuttgart.bytecode.SamMethod; public interface DescriptorVisitor { public String visit(NormalMethod method); diff --git a/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java b/src/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java similarity index 95% rename from src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java rename to src/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java index 5271e63d..e374bb93 100644 --- a/src/de/dhbwstuttgart/bytecode/TypeToDescriptor.java +++ b/src/de/dhbwstuttgart/bytecode/descriptor/TypeToDescriptor.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.descriptor; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; diff --git a/src/de/dhbwstuttgart/bytecode/Signature.java b/src/de/dhbwstuttgart/bytecode/signature/Signature.java similarity index 75% rename from src/de/dhbwstuttgart/bytecode/Signature.java rename to src/de/dhbwstuttgart/bytecode/signature/Signature.java index 362429a3..46202cb1 100644 --- a/src/de/dhbwstuttgart/bytecode/Signature.java +++ b/src/de/dhbwstuttgart/bytecode/signature/Signature.java @@ -1,15 +1,18 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.signature; import java.util.HashMap; import java.util.Iterator; +import org.objectweb.asm.Type; import org.objectweb.asm.signature.SignatureVisitor; import org.objectweb.asm.signature.SignatureWriter; +import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -49,6 +52,29 @@ public class Signature { createSignatureForConsOrMethod(this.method,false); } + public Signature(LambdaExpression lambdaExpression,int numberOfParams) { + sw = new SignatureWriter(); + createSignatureForFunN(lambdaExpression, numberOfParams); + } + + private void createSignatureForFunN(LambdaExpression lambdaExpression, int numberOfParams) { + + sw.visitFormalTypeParameter("R"); + // getBounds vom Return-Type + sw.visitClassBound().visitClassType(Type.getInternalName(Object.class)); + sw.visitClassBound().visitEnd(); + for(int i = 0;i;"); - + RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType; + if(!r.acceptTV(new TypeToSignature()).substring(0, 4).equals("TPH ")) + sv.visitInterface().visitClassType(r.acceptTV(new TypeToSignature())); +// sv.visitClassType(r.acceptTV(new TypeToSignature())); + System.out.println(r.getClass()+" Signature TPH: "+r.acceptTV(new TypeToSignature())); break; default: if(!isParameterType) @@ -135,15 +163,16 @@ public class Signature { * @param genAndBounds */ private void getBoundsOfTypeVar(GenericTypeVar g, HashMap genAndBounds) { - sw.visitFormalTypeParameter(g.getParsedName()); + sw.visitFormalTypeParameter(g.getName()); Iterator bItr = g.getBounds().iterator(); while(bItr.hasNext()) { RefTypeOrTPHOrWildcardOrGeneric b =bItr.next(); String boundDesc = b.acceptTV(new TypeToDescriptor()); +// System.out.println("GetBounds: " + boundDesc); // Ensure that <...> extends java.lang.Object OR ... sw.visitClassBound().visitClassType(boundDesc); - genAndBounds.put(g.getParsedName(), boundDesc); + genAndBounds.put(g.getName(), boundDesc); } sw.visitClassBound().visitEnd(); } diff --git a/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java new file mode 100644 index 00000000..78b2fb22 --- /dev/null +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToSignature.java @@ -0,0 +1,53 @@ +package de.dhbwstuttgart.bytecode.signature; + +import java.util.Iterator; + +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.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.type.TypeVisitor; + +public class TypeToSignature implements TypeVisitor { + + @Override + public String visit(RefType refType) { +// return refType.toString().replace(".", "/"); + String params = ""; + if(refType.getParaList().size()>0){ + params += "<"; + Iterator it = refType.getParaList().iterator(); + while(it.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric param = it.next(); + params += "L"+param.toString().replace(".", "/"); + if(it.hasNext())params += ";"; + } + params += ";>"; + } + return refType.getName().toString().replace(".", "/") + params+";"; + } + + @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(".", "/"); + } + +} diff --git a/src/de/dhbwstuttgart/bytecode/TypeToString.java b/src/de/dhbwstuttgart/bytecode/signature/TypeToString.java similarity index 95% rename from src/de/dhbwstuttgart/bytecode/TypeToString.java rename to src/de/dhbwstuttgart/bytecode/signature/TypeToString.java index 86d4124a..94314afe 100644 --- a/src/de/dhbwstuttgart/bytecode/TypeToString.java +++ b/src/de/dhbwstuttgart/bytecode/signature/TypeToString.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.bytecode; +package de.dhbwstuttgart.bytecode.signature; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType; diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index e8d74cb6..9a66543a 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -102,7 +102,7 @@ public class JavaTXCompiler { System.out.println(xConsSet); Set> result = unify.unify(xConsSet, finiteClosure); - System.out.println("RESULT: " + result); + System.out.println("RESULT: " + result.size()); results.addAll(result); } return results.stream().map((unifyPairs -> diff --git a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java index eca8c9c2..3d450183 100644 --- a/src/de/dhbwstuttgart/environment/CompilationEnvironment.java +++ b/src/de/dhbwstuttgart/environment/CompilationEnvironment.java @@ -40,17 +40,29 @@ public class CompilationEnvironment { * @param sourceFiles die zu kompilierenden Dateien */ public CompilationEnvironment(List sourceFiles) { - String bootClassPath = System.getProperty("sun.boot.class.path"); + /** + * Java 9 bringt einige Änderungen am Classloader + * So funktioniert der BootClassLoader nicht mehr. + * hier gibts ein paar Quellen zum nachlesen: + * http://java9.wtf/class-loading/ + * https://stackoverflow.com/questions/46494112/classloaders-hierarchy-in-java-9 + * + */ + //String bootClassPath = System.getProperty("sun.boot.class.path"); + // ClassLoader cl = ClassLoader.getPlatformClassLoader(); + ClassLoader cl = ClassLoader.getSystemClassLoader(); + String bootClassPath = System.getProperty("java.class.path"); 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"); - } + 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"); + } } - } + //URLClassLoader loader = new URLClassLoader(new URL[0], cl); + //librarys = Arrays.asList(loader.getURLs()); + this.sourceFiles = sourceFiles; this.packageCrawler = new PackageCrawler(librarys); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java index 7863c8ed..c4fc5b58 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -1,7 +1,7 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.DebugException; -import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -11,6 +11,7 @@ import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.unify.model.*; import java.util.*; +import java.util.stream.Collectors; public class FCGenerator { /** @@ -19,12 +20,15 @@ public class FCGenerator { * * @param availableClasses - Alle geparsten Klassen */ - public static Set toFC(Collection availableClasses) throws ClassNotFoundException { - HashSet pairs = new HashSet<>(); + public static Set toUnifyFC(Collection availableClasses) throws ClassNotFoundException { + return toFC(availableClasses).stream().map(t -> UnifyTypeFactory.convert(t)).collect(Collectors.toSet()); + } + + 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; } @@ -35,67 +39,84 @@ public class FCGenerator { * @param forType * @return */ - private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { + 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<>(); + /** + * + * @param forType + * @param availableClasses + * @param gtvs + * @return + * @throws ClassNotFoundException + */ + 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<>(); + 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()); + if(!gtvs.containsKey(gtv.getName())){ + TypePlaceholder replacePlaceholder = TypePlaceholder.fresh(new NullToken()); + gtvs.put(gtv.getName(), replacePlaceholder); + newGTVs.put(gtv.getName(), replacePlaceholder); } - params.add(gtvs.get(gtv.getParsedName())); + params.add(gtvs.get(gtv.getName())); } - 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(); + + List superClasses = new ArrayList<>(); + superClasses.add(forType.getSuperClass()); + superClasses.addAll(forType.getSuperInterfaces()); + + List retList = new ArrayList<>(); + for(RefType superType : superClasses){ + Optional hasSuperclass = availableClasses.stream().filter(cl -> superType.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(superType.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 in der Superklasse ausgetauscht werden müssen + Iterator itGenParams = superClass.getGenerics().iterator(); + Iterator itSetParams = superType.getParaList().iterator(); + while(itSetParams.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); + //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: + RefTypeOrTPHOrWildcardOrGeneric setSetType = setType.acceptTV(new TypeExchanger(gtvs)); + newGTVs.put(itGenParams.next().getName(), setSetType); + } + + //Für den superType kann man nun zum Austauschen der Generics wieder die gtvs nehmen: + //Die newGTVs sind nur für den superClass ClassOrInterface welches möglicherweise per reflection geladen wurde abgestimmt + RefTypeOrTPHOrWildcardOrGeneric superRefType = superType.acceptTV(new TypeExchanger(gtvs)); + + RefTypeOrTPHOrWildcardOrGeneric t1 = new RefType(forType.getClassName(), params, new NullToken()); + RefTypeOrTPHOrWildcardOrGeneric t2 = superRefType; + + Pair ret = new Pair(t1, t2, PairOperator.SMALLER); + + List superTypes; + //Rekursiver Aufruf. Abbruchbedingung ist Object als Superklasse: + if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ + superTypes = Arrays.asList(new Pair(ASTFactory.createObjectType(), ASTFactory.createObjectType(), PairOperator.SMALLER)); + }else{ + superTypes = getSuperTypes(superClass, availableClasses, newGTVs); + } + + retList.add(ret); + retList.addAll(superTypes); } - /* - 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; } @@ -103,46 +124,43 @@ public class FCGenerator { /** * Tauscht die GTVs in einem Typ gegen die entsprechenden Typen in der übergebenen Map aus. */ - private static class TypeExchanger implements TypeVisitor{ + private static class TypeExchanger implements TypeVisitor{ - private final HashMap gtvs; + private final HashMap gtvs; - TypeExchanger(HashMap gtvs){ + TypeExchanger(HashMap gtvs){ this.gtvs = gtvs; } @Override - public UnifyType visit(RefType refType) { - List params = new ArrayList<>(); + public RefTypeOrTPHOrWildcardOrGeneric 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); + RefTypeOrTPHOrWildcardOrGeneric ret = new RefType(refType.getName(), params, new NullToken()); return ret; } @Override - public UnifyType visit(SuperWildcardType superWildcardType) { + public RefTypeOrTPHOrWildcardOrGeneric visit(SuperWildcardType superWildcardType) { throw new DebugException("Dieser Fall darf nicht auftreten"); } @Override - public UnifyType visit(TypePlaceholder typePlaceholder) { + public RefTypeOrTPHOrWildcardOrGeneric visit(TypePlaceholder typePlaceholder) { throw new DebugException("Dieser Fall darf nicht auftreten"); } @Override - public UnifyType visit(ExtendsWildcardType extendsWildcardType) { + public RefTypeOrTPHOrWildcardOrGeneric visit(ExtendsWildcardType extendsWildcardType) { throw new DebugException("Dieser Fall darf nicht auftreten"); } @Override - public UnifyType visit(GenericRefType genericRefType) { + public RefTypeOrTPHOrWildcardOrGeneric 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/GenericContext.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java index 6e3ce7d9..0cea06ed 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/GenericContext.java @@ -3,8 +3,8 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; public class GenericContext { - public final String parentMethod; - public final JavaClassName parentClass; + private final String parentMethod; + private final JavaClassName parentClass; public GenericContext(JavaClassName parentClass, String parentMethod) { if(parentMethod == null)parentMethod = ""; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 1586ba3f..5310d13b 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -7,6 +7,7 @@ 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.UnaryExpr.Operation; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -278,13 +279,14 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + Expression argument = convert(stmt.unaryExpression()); + Token offset = stmt.getStart(); + return new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); } - private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { + return new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { @@ -293,8 +295,8 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.AssignmentContext stmt) { @@ -599,8 +601,31 @@ public class StatementGenerator { } private BinaryExpr.Operator convertBinaryOperator(String operator) { - - return null; + //return BinaryExpr.Operator.ADD; + if(operator.equals("+")) { + return BinaryExpr.Operator.ADD; + }else if(operator.equals("-")) { + return BinaryExpr.Operator.SUB; + }else if(operator.equals("*")) { + return BinaryExpr.Operator.MUL; + }else if(operator.equals("&")) { + return BinaryExpr.Operator.AND; + }else if(operator.equals("|")) { + return BinaryExpr.Operator.OR; + }else if(operator.equals("/")) { + return BinaryExpr.Operator.DIV; + }else if(operator.equals("<")) { + return BinaryExpr.Operator.LESSTHAN; + }else if(operator.equals(">")) { + return BinaryExpr.Operator.BIGGERTHAN; + }else if(operator.equals(">=")) { + return BinaryExpr.Operator.BIGGEREQUAL; + } else if(operator.equals("<=")) { + return BinaryExpr.Operator.LESSEQUAL; + } else { + throw new NotImplementedException(); + } +// throw new NotImplementedException(); } private Expression convert(Java8Parser.ShiftExpressionContext expression) { @@ -612,12 +637,15 @@ public class StatementGenerator { } private Expression convert(Java8Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() != null){ - return convert(expression.additiveExpression()); - }else if(expression.multiplicativeExpression() != null){ + + if(expression.additiveExpression() == null){ return convert(expression.multiplicativeExpression()); - }else{ - throw new NotImplementedException(); + }else { + Expression leftSide = convert(expression.additiveExpression()); + Expression rightSide = convert(expression.multiplicativeExpression()); + BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(expression.getStart()), leftSide, rightSide, offset); } } @@ -625,7 +653,11 @@ public class StatementGenerator { if(expression.multiplicativeExpression() == null){ return convert(expression.unaryExpression()); }else{ - throw new NotImplementedException(); + Expression leftSide = convert(expression.multiplicativeExpression()); + Expression rightSide = convert(expression.unaryExpression()); + BinaryExpr.Operator op = convertBinaryOperator(expression.getChild(1).getText()); + Token offset = expression.getStart(); + return new BinaryExpr(op, TypePlaceholder.fresh(offset), leftSide, rightSide, offset); } } @@ -793,7 +825,9 @@ public class StatementGenerator { }else if(literal.CharacterLiteral() != null){ RefType type = new RefType(reg.getName("java.lang.Character"),literal.getStart()); return new Literal(type, - literal.CharacterLiteral().getText().charAt(0), + // das gibt immer ' zurück, der Char befindet sich in Position 1 + //literal.CharacterLiteral().getText().charAt(0), + literal.CharacterLiteral().getText().charAt(1), literal.getStart()); }else if(literal.StringLiteral()!=null){ RefType type = new RefType(reg.getName("java.lang.String"),literal.getStart()); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index b681d9c3..b7f14f19 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -73,6 +73,7 @@ public class SyntaxTreeGenerator{ } public SourceFile convert(Java8Parser.CompilationUnitContext ctx, PackageCrawler packageCrawler) throws ClassNotFoundException{ + if(ctx.packageDeclaration()!=null)this.pkgName = convert(ctx.packageDeclaration()); List classes = new ArrayList<>(); Map imports = GatherNames.getImports(ctx, packageCrawler); this.imports = imports.keySet().stream().map(name -> reg.getName(name)).collect(Collectors.toSet()); @@ -89,6 +90,15 @@ public class SyntaxTreeGenerator{ return new SourceFile(this.pkgName, classes, this.imports); } + private String convert(Java8Parser.PackageDeclarationContext packageDeclarationContext) { + String ret = ""; + for(TerminalNode identifier : packageDeclarationContext.Identifier()){ + ret += identifier.getText()+"."; + } + ret = ret.substring(0, ret.length()-1); + return ret; + } + public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier()); @@ -164,7 +174,12 @@ public class SyntaxTreeGenerator{ modifiers += newModifier; } } - JavaClassName name = reg.getName(ctx.Identifier().getText()); + String className = this.pkgName + (this.pkgName.length()>0?".":"") + ctx.Identifier().getText(); + JavaClassName name = reg.getName(className); //Holt den Package Namen mit dazu + if(! name.toString().equals(className)){ //Kommt die Klasse schon in einem anderen Package vor? + throw new TypeinferenceException("Name " + className + " bereits vorhanden in " + reg.getName(className).toString() + ,ctx.getStart()); + } GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, "", reg, new GenericsRegistry(globalGenerics)); Token offset = ctx.getStart(); GenericDeclarationList genericClassParameters; @@ -198,20 +213,20 @@ public class SyntaxTreeGenerator{ } Boolean isInterface = false; - List implementedInterfaces = convert(ctx.superinterfaces(), generics); + List implementedInterfaces = convert(ctx.superinterfaces(), generics); return new ClassOrInterface(modifiers, name, fielddecl, methods, konstruktoren, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); } - private List convert(Java8Parser.SuperinterfacesContext ctx, GenericsRegistry generics) { + private List convert(Java8Parser.SuperinterfacesContext ctx, GenericsRegistry generics) { if(ctx == null)return new ArrayList<>(); return convert(ctx.interfaceTypeList(), generics); } - private List convert(Java8Parser.InterfaceTypeListContext ctx, GenericsRegistry generics) { - List ret = new ArrayList<>(); + private List convert(Java8Parser.InterfaceTypeListContext ctx, GenericsRegistry generics) { + List ret = new ArrayList<>(); for(Java8Parser.InterfaceTypeContext interfaceType : ctx.interfaceType()){ - ret.add(TypeGenerator.convert(interfaceType.classType(), reg, generics)); + ret.add((RefType) TypeGenerator.convert(interfaceType.classType(), reg, generics)); } return ret; } @@ -395,7 +410,7 @@ public class SyntaxTreeGenerator{ List fields = convertFields(ctx.interfaceBody()); List methods = convertMethods(ctx.interfaceBody(), name, superClass, generics); - List extendedInterfaces = convert(ctx.extendsInterfaces(), generics); + List extendedInterfaces = convert(ctx.extendsInterfaces(), generics); return new ClassOrInterface(modifiers, name, fields, methods, new ArrayList<>(), genericParams, superClass, true, extendedInterfaces, ctx.getStart()); @@ -418,7 +433,7 @@ public class SyntaxTreeGenerator{ return ret; } - private List convert(Java8Parser.ExtendsInterfacesContext extendsInterfacesContext, GenericsRegistry generics) { + private List convert(Java8Parser.ExtendsInterfacesContext extendsInterfacesContext, GenericsRegistry generics) { if(extendsInterfacesContext == null)return new ArrayList<>(); return convert(extendsInterfacesContext.interfaceTypeList(), generics); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index 1dd259d0..38399bf0 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -3,7 +3,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.antlr.Java8Parser; -import de.dhbwstuttgart.parser.scope.GenericTypeName; import de.dhbwstuttgart.parser.scope.GenericsRegistry; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; @@ -75,7 +74,7 @@ public class TypeGenerator { //Problem ist erlaubt, würde aber bei den Bounds von A den Generic B nicht als solchen erkennen List bounds = TypeGenerator.convert(typeParameter.typeBound(),reg, generics); - GenericTypeVar ret = new GenericTypeVar(new GenericTypeName(new GenericContext(parentClass, parentMethod), name), bounds, typeParameter.getStart(), typeParameter.getStop()); + GenericTypeVar ret = new GenericTypeVar(name, bounds, typeParameter.getStart(), typeParameter.getStop()); generics.put(name, new GenericContext(parentClass, parentMethod)); return ret; } @@ -114,7 +113,9 @@ public class TypeGenerator { if(referenceTypeContext.classOrInterfaceType() != null){ if(referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType()!= null){ Java8Parser.ClassType_lfno_classOrInterfaceTypeContext ctx = referenceTypeContext.classOrInterfaceType().classType_lfno_classOrInterfaceType(); - return convertTypeName(ctx.Identifier().toString(), ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics); + //return convertTypeName(ctx.Identifier().toString(), ctx.typeArguments(),referenceTypeContext.getStart(), reg, generics); + if(ctx.typeArguments() != null)throw new NotImplementedException(); + return convertTypeName(referenceTypeContext.getText(), null,referenceTypeContext.getStart(), reg, generics); }else{ throw new NotImplementedException(); } @@ -131,7 +132,7 @@ public class TypeGenerator { String name, Java8Parser.TypeArgumentsContext typeArguments, Token offset, JavaClassRegistry reg, GenericsRegistry generics){ if(!reg.contains(name)){ //Dann könnte es ein Generische Type sein if(generics.contains(name)){ - return new GenericRefType(new GenericTypeName(generics.get(name),name), offset); + return new GenericRefType(name, offset); }else{ throw new TypeinferenceException("Der Typ "+ name + " ist nicht vorhanden",offset); } diff --git a/src/de/dhbwstuttgart/parser/scope/GenericTypeName.java b/src/de/dhbwstuttgart/parser/scope/GenericTypeName.java deleted file mode 100644 index c5355ebd..00000000 --- a/src/de/dhbwstuttgart/parser/scope/GenericTypeName.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.dhbwstuttgart.parser.scope; - -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; - -public class GenericTypeName extends JavaClassName { - private final static String DELIMITER = "%"; - - private final JavaClassName parentClass; - private final String methodName; - - public GenericTypeName(GenericContext genericContext, String name) { - super(name); - this.parentClass = genericContext.parentClass; - this.methodName = genericContext.parentMethod; - } - - public String getUniqueIdentifier() { - return parentClass.toString() - + DELIMITER + methodName - + DELIMITER + super.toString(); - } - - public JavaClassName getParentClass() { - return parentClass; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java b/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java new file mode 100644 index 00000000..5235ab1a --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/ASPStringConverter.java @@ -0,0 +1,40 @@ +package de.dhbwstuttgart.sat.asp; + +import de.dhbwstuttgart.parser.scope.JavaClassName; + +import java.util.HashMap; +import java.util.Map; + +public class ASPStringConverter { + private static final Map replacements = new HashMap<>(); + static{ + replacements.put(".", "_DOT_"); + replacements.put("$", "_DOLLAR_"); + } + + public static String toConstant(JavaClassName name){ + return toConstant(name.toString()); + } + + public static String toConstant(String name){ + return "c" + replace(name); + } + + public static String fromConstant(String s){ + return unReplace(s).substring(1); + } + + private static String replace(String input){ + for(String toReplace : replacements.keySet()){ + input = input.replace(toReplace, replacements.get(toReplace)); + } + return input; + } + + private static String unReplace(String input){ + for(String toReplace : replacements.keySet()){ + input = input.replace(replacements.get(toReplace), toReplace); + } + return input; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java index 85f6dc56..9f529bbe 100644 --- a/src/de/dhbwstuttgart/sat/asp/Clingo.java +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -17,26 +17,32 @@ public class Clingo { 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/finiteclosure.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/reduce.lp")); programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/adapt.lp")); } public Clingo(List inputFiles){ this.input = inputFiles; } + /* + TODO: Clingo per Java Wrapper https://stackoverflow.com/questions/3356200/using-java-to-wrap-over-c + */ 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 + //commands.add("--outf=2"); //use JSON-Output + commands.add("--outf=1"); //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(); diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java new file mode 100644 index 00000000..e16592cc --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPGencayRule.java @@ -0,0 +1,26 @@ +package de.dhbwstuttgart.sat.asp.model; + +public enum ASPGencayRule { + ASP_PAIR_EQUALS_NAME("equals"), + ASP_PAIR_SMALLER_NAME("sub"), + ASP_PAIR_SMALLER_DOT_NAME("subEq"), + ASP_PARAMLIST_NAME("paramEq"), + ASP_FC_PARAMLIST_NAME("param"), + ASP_PARAMLIST_END_POINTER("null"), + ASP_TYPE("typeEq"), + ASP_FCTYPE("type"), + ASP_TYPE_VAR("var"), + ASP_GENERIC_VAR("pph"), + ASP_PARAMLIST_ORDER("paramOrder"); + + private final String text; + + private ASPGencayRule(final String text) { + this.text = text; + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java index 41237547..20d221f9 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -1,15 +1,14 @@ 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") - ; + ASP_TYPE("type"), + ASP_FCTYPE("typeFC"), + ASP_TYPE_VAR("typeVar"); private final String text; diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java index c7be7dbc..80ebfc38 100644 --- a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -1,17 +1,231 @@ package de.dhbwstuttgart.sat.asp.parser; -import de.dhbwstuttgart.typeinference.result.ResultPair; -import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultBaseListener; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultLexer; +import de.dhbwstuttgart.sat.asp.parser.antlr.UnifyResultParser; +import de.dhbwstuttgart.sat.asp.parser.model.ParsedType; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.result.*; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTreeWalker; -import java.util.HashSet; -import java.util.Set; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonObject; +import java.io.StringReader; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; -public class ASPParser { - ResultSet parse(String result){ - Set ret = new HashSet<>(); - for(String pair : result.split(",")){ +/** + * Ablauf: + * - Erst werden alle benötigten Statements eingelesen. Die Pointer bleiben als Strings erhalten + * - Anschließend müssen diese wieder zu einem Unify Ergebnis zurückgewandelt werden + * - Hier nicht die Typen aus dem unify.model packages verwenden. + * TODO: Überlegen welche Informationen noch nach der Unifizierung gebraucht werden + * -> Eigentlich nur die korrekten Namen der Typen und TPHs + */ +public class ASPParser extends UnifyResultBaseListener { + private Collection originalTPHs; + private ResultSet resultSet; + private Map types = new HashMap<>(); + private Set tphs = new HashSet<>(); + private Map parameterLists = new HashMap<>(); + private Set equalsRelations = new HashSet<>(); + private Set smallerRelations = new HashSet<>(); + /** + * Parst clingo output welcher als JSON (option --outf=2) ausgibt + * @param toParse + * @return + */ + public static ResultSet parse(String toParse, Collection oldPlaceholders){ + return new ASPParser(toParse, oldPlaceholders).resultSet; + } + + @Override + public void enterParameter(UnifyResultParser.ParameterContext ctx) { + //Linked List (pointer, Type, nextPointer) + List params = parseParameterList(ctx.parameterList()); + parameterLists.put(params.get(0), new ParameterListNode(params.get(1), params.get(2))); + } + + private static class Relation { + public final String right; + public final String left; + + Relation(String leftType, String rightType){ + this.left = leftType; + this.right = rightType; } - return new ResultSet(ret); + + @Override + public int hashCode() { + return (left+right).hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(obj instanceof Relation) + return (right+left).equals(((Relation) obj).left+((Relation) obj).right); + return super.equals(obj); + } + } + + private List parseParameterList(UnifyResultParser.ParameterListContext ctx){ + return ctx.value().stream().map(v -> + //ASPStringConverter.fromConstant(v.getText()) + v.getText() + ).collect(Collectors.toList()); + } + + @Override + public void enterEquals(UnifyResultParser.EqualsContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Equals-Regel"); + String ls = parameterList.get(0); + String rs = parameterList.get(1); + equalsRelations.add(new Relation(ls, rs)); + + } + + @Override + public void enterSmaller(UnifyResultParser.SmallerContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<2)throw new DebugException("Fehler in Smaller-Regel"); + String ls = parameterList.get(0); + String rs = parameterList.get(1); + smallerRelations.add(new Relation(ls, rs)); + } + + @Override + public void enterTypeVar(UnifyResultParser.TypeVarContext ctx) { + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<1)throw new DebugException("Fehler in typeVar-Regel"); + tphs.add(parameterList.get(0)); + } + + @Override + public void enterType(UnifyResultParser.TypeContext ctx){ + List parameterList = parseParameterList(ctx.parameterList()); + if(parameterList.size()<3)throw new DebugException("Fehler in Type-Regel"); + String name = parameterList.get(0); + String typeName = parameterList.get(1); + String paramPointer = parameterList.get(2); + types.put(name, new ParsedType(typeName, paramPointer)); + } + + /* + private List parsedRule; + private List parseRule(String rule){ + UnifyResultLexer lexer = new UnifyResultLexer(CharStreams.fromString(rule)); + UnifyResultParser.AspruleContext parseTree = new UnifyResultParser(new CommonTokenStream(lexer)).asprule(); + parsedRule = new ArrayList<>(); + new ParseTreeWalker().walk(this, parseTree); + return parsedRule; + } + + @Override + public void enterAsprule(UnifyResultParser.AspruleContext ctx) { + super.enterAsprule(ctx); + for(int i = 0; i< ctx.getChildCount();i++){ + parsedRule.add(ctx.getChild(i).getText()); + } + } + */ + private ASPParser(String toParse, Collection oldPlaceholders){ + //System.out.println(toParse); + this.originalTPHs = oldPlaceholders; + + /* + JsonObject jsonResult = Json.createReader(new StringReader(toParse)).readObject(); + JsonArray results = jsonResult.getJsonArray("Call").getJsonObject(0). + getJsonArray("Witnesses").getJsonObject(0). + getJsonArray("Value"); + + //Im ersten Schritt werden alle Regeln geparst + String completeResult = ""; + for(int i = 0; i ret = new HashSet<>(); + //Zuerst die params und typeVars: + //for(String paramPointer : types.values().stream().map(parsedType -> parsedType.params).collect(Collectors.toList())){ } + + //Dann die Equalsdot Statements + for(Relation eq : equalsRelations){ + RefTypeOrTPHOrWildcardOrGeneric lsType = this.getType(eq.left); + RefTypeOrTPHOrWildcardOrGeneric rsType = this.getType(eq.right); + if(lsType instanceof TypePlaceholder && rsType instanceof RefType) + ret.add(new PairTPHequalRefTypeOrWildcardType((TypePlaceholder) lsType, rsType)); + else if(lsType instanceof TypePlaceholder && rsType instanceof TypePlaceholder) + ret.add(new PairTPHEqualTPH((TypePlaceholder)lsType, (TypePlaceholder)rsType)); + else throw new NotImplementedException(); + } + this.resultSet = new ResultSet(ret); + } + + private RefTypeOrTPHOrWildcardOrGeneric getType(String name) { + if(tphs.contains(name)){ + name = ASPStringConverter.fromConstant(name); + for(TypePlaceholder tph : originalTPHs){ + if(tph.getName().equals(name))return tph; + } + return TypePlaceholder.fresh(new NullToken()); + }else + if(types.containsKey(name)){ + List params = new ArrayList<>(); + ParsedType t = types.get(name); + + for(String param : getParams(t.params)){ + params.add(this.getType(param)); + } + return new RefType(new JavaClassName(ASPStringConverter.fromConstant(t.name)), params, new NullToken()); + }else throw new DebugException("Der Typ " + name + " konnte nicht bestimmt werden"); + } + + private static class ParameterListNode{ + final String type; + final String nextNode; + + public ParameterListNode(String type, String next) { + this.type = type; + this.nextNode = next; + } + } + + private List getParams(String pointer) { + List params = new ArrayList<>(); + while(pointer != null){ + if(pointer.equals(ASPRule.ASP_PARAMLIST_END_POINTER.toString()))return params; + if(!parameterLists.containsKey(pointer)) + throw new DebugException("Fehler in Ergebnisparsen"); + //TODO: Fehler in ASP. Die adapt Regel muss erkennen, wenn die Parameterliste auf der linken Seite kürzer ist und diese Rechtzeitig beenden + ParameterListNode param = parameterLists.get(pointer); + pointer = param.nextNode; + params.add(param.type); + } + return params; + //Optional ret = parameterLists.stream().filter(aspParameterList -> aspParameterList.name.equals(rs)).findAny(); + //return ret.get(); } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 new file mode 100644 index 00000000..98b6a5fb --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.g4 @@ -0,0 +1,37 @@ +grammar UnifyResult; + +answer : 'ANSWER' (resultSetRule '.')*; + +resultSetRule : + parameter + | equals + | smaller + | typeVar + | type + | otherRule + ; + +parameterList : '(' value (',' value)* ')'; +value : NAME + | resultSetRule ; + +parameter : PARAMLIST_NAME parameterList; +equals : EQUALS_NAME parameterList; +smaller : SMALLER_NAME parameterList; +typeVar : TYPEVAR_NAME parameterList; +type : TYPE_NAME parameterList; +otherRule : NAME parameterList; + +//TODO: Es sollte Regeln für das Result set geben, welche sich nicht mit den anderen überdecken, dann auch nur diese im Result ausgeben +PARAMLIST_NAME : 'param'; +EQUALS_NAME : 'equals'; +SMALLER_NAME : 'smaller'; +TYPEVAR_NAME : 'typeVar'; +TYPE_NAME : 'type'; +NAME : [a-zA-Z0-9_]+; + +WS : [ \t\r\n\u000C]+ -> skip + ; +LINE_COMMENT + : '%' ~[\r\n]* -> skip + ; diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens new file mode 100644 index 00000000..8f9c0e0a --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResult.tokens @@ -0,0 +1,23 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +PARAMLIST_NAME=6 +EQUALS_NAME=7 +SMALLER_NAME=8 +TYPEVAR_NAME=9 +TYPE_NAME=10 +NAME=11 +WS=12 +LINE_COMMENT=13 +'ANSWER'=1 +'.'=2 +'('=3 +','=4 +')'=5 +'param'=6 +'equals'=7 +'smaller'=8 +'typeVar'=9 +'type'=10 diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java new file mode 100644 index 00000000..f33525d7 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultBaseListener.java @@ -0,0 +1,159 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; + +import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.tree.ErrorNode; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * This class provides an empty implementation of {@link UnifyResultListener}, + * which can be extended to create a listener which only needs to handle a subset + * of the available methods. + */ +public class UnifyResultBaseListener implements UnifyResultListener { + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterAnswer(UnifyResultParser.AnswerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitAnswer(UnifyResultParser.AnswerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterResultSetRule(UnifyResultParser.ResultSetRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitResultSetRule(UnifyResultParser.ResultSetRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterParameterList(UnifyResultParser.ParameterListContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitParameterList(UnifyResultParser.ParameterListContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterValue(UnifyResultParser.ValueContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitValue(UnifyResultParser.ValueContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterParameter(UnifyResultParser.ParameterContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitParameter(UnifyResultParser.ParameterContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterEquals(UnifyResultParser.EqualsContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitEquals(UnifyResultParser.EqualsContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterSmaller(UnifyResultParser.SmallerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitSmaller(UnifyResultParser.SmallerContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterTypeVar(UnifyResultParser.TypeVarContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitTypeVar(UnifyResultParser.TypeVarContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterType(UnifyResultParser.TypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitType(UnifyResultParser.TypeContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterOtherRule(UnifyResultParser.OtherRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitOtherRule(UnifyResultParser.OtherRuleContext ctx) { } + + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void enterEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void exitEveryRule(ParserRuleContext ctx) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitTerminal(TerminalNode node) { } + /** + * {@inheritDoc} + * + *

The default implementation does nothing.

+ */ + @Override public void visitErrorNode(ErrorNode node) { } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java new file mode 100644 index 00000000..b8660eba --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.java @@ -0,0 +1,134 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; +import org.antlr.v4.runtime.Lexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.Token; +import org.antlr.v4.runtime.TokenStream; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.misc.*; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class UnifyResultLexer extends Lexer { + static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, PARAMLIST_NAME=6, EQUALS_NAME=7, + SMALLER_NAME=8, TYPEVAR_NAME=9, TYPE_NAME=10, NAME=11, WS=12, LINE_COMMENT=13; + public static String[] channelNames = { + "DEFAULT_TOKEN_CHANNEL", "HIDDEN" + }; + + public static String[] modeNames = { + "DEFAULT_MODE" + }; + + public static final String[] ruleNames = { + "T__0", "T__1", "T__2", "T__3", "T__4", "PARAMLIST_NAME", "EQUALS_NAME", + "SMALLER_NAME", "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" + }; + + private static final String[] _LITERAL_NAMES = { + null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'equals'", "'smaller'", + "'typeVar'", "'type'" + }; + private static final String[] _SYMBOLIC_NAMES = { + null, null, null, null, null, null, "PARAMLIST_NAME", "EQUALS_NAME", "SMALLER_NAME", + "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" + }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + + public UnifyResultLexer(CharStream input) { + super(input); + _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + + @Override + public String getGrammarFileName() { return "UnifyResult.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public String[] getChannelNames() { return channelNames; } + + @Override + public String[] getModeNames() { return modeNames; } + + @Override + public ATN getATN() { return _ATN; } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\17c\b\1\4\2\t\2\4"+ + "\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t"+ + "\13\4\f\t\f\4\r\t\r\4\16\t\16\3\2\3\2\3\2\3\2\3\2\3\2\3\2\3\3\3\3\3\4"+ + "\3\4\3\5\3\5\3\6\3\6\3\7\3\7\3\7\3\7\3\7\3\7\3\b\3\b\3\b\3\b\3\b\3\b\3"+ + "\b\3\t\3\t\3\t\3\t\3\t\3\t\3\t\3\t\3\n\3\n\3\n\3\n\3\n\3\n\3\n\3\n\3\13"+ + "\3\13\3\13\3\13\3\13\3\f\6\fP\n\f\r\f\16\fQ\3\r\6\rU\n\r\r\r\16\rV\3\r"+ + "\3\r\3\16\3\16\7\16]\n\16\f\16\16\16`\13\16\3\16\3\16\2\2\17\3\3\5\4\7"+ + "\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\3\2\5\6\2\62;C\\"+ + "aac|\5\2\13\f\16\17\"\"\4\2\f\f\17\17\2e\2\3\3\2\2\2\2\5\3\2\2\2\2\7\3"+ + "\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2\2"+ + "\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\3\35"+ + "\3\2\2\2\5$\3\2\2\2\7&\3\2\2\2\t(\3\2\2\2\13*\3\2\2\2\r,\3\2\2\2\17\62"+ + "\3\2\2\2\219\3\2\2\2\23A\3\2\2\2\25I\3\2\2\2\27O\3\2\2\2\31T\3\2\2\2\33"+ + "Z\3\2\2\2\35\36\7C\2\2\36\37\7P\2\2\37 \7U\2\2 !\7Y\2\2!\"\7G\2\2\"#\7"+ + "T\2\2#\4\3\2\2\2$%\7\60\2\2%\6\3\2\2\2&\'\7*\2\2\'\b\3\2\2\2()\7.\2\2"+ + ")\n\3\2\2\2*+\7+\2\2+\f\3\2\2\2,-\7r\2\2-.\7c\2\2./\7t\2\2/\60\7c\2\2"+ + "\60\61\7o\2\2\61\16\3\2\2\2\62\63\7g\2\2\63\64\7s\2\2\64\65\7w\2\2\65"+ + "\66\7c\2\2\66\67\7n\2\2\678\7u\2\28\20\3\2\2\29:\7u\2\2:;\7o\2\2;<\7c"+ + "\2\2<=\7n\2\2=>\7n\2\2>?\7g\2\2?@\7t\2\2@\22\3\2\2\2AB\7v\2\2BC\7{\2\2"+ + "CD\7r\2\2DE\7g\2\2EF\7X\2\2FG\7c\2\2GH\7t\2\2H\24\3\2\2\2IJ\7v\2\2JK\7"+ + "{\2\2KL\7r\2\2LM\7g\2\2M\26\3\2\2\2NP\t\2\2\2ON\3\2\2\2PQ\3\2\2\2QO\3"+ + "\2\2\2QR\3\2\2\2R\30\3\2\2\2SU\t\3\2\2TS\3\2\2\2UV\3\2\2\2VT\3\2\2\2V"+ + "W\3\2\2\2WX\3\2\2\2XY\b\r\2\2Y\32\3\2\2\2Z^\7\'\2\2[]\n\4\2\2\\[\3\2\2"+ + "\2]`\3\2\2\2^\\\3\2\2\2^_\3\2\2\2_a\3\2\2\2`^\3\2\2\2ab\b\16\2\2b\34\3"+ + "\2\2\2\6\2QV^\3\b\2\2"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens new file mode 100644 index 00000000..8f9c0e0a --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultLexer.tokens @@ -0,0 +1,23 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +PARAMLIST_NAME=6 +EQUALS_NAME=7 +SMALLER_NAME=8 +TYPEVAR_NAME=9 +TYPE_NAME=10 +NAME=11 +WS=12 +LINE_COMMENT=13 +'ANSWER'=1 +'.'=2 +'('=3 +','=4 +')'=5 +'param'=6 +'equals'=7 +'smaller'=8 +'typeVar'=9 +'type'=10 diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java new file mode 100644 index 00000000..6158b123 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultListener.java @@ -0,0 +1,110 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; +import org.antlr.v4.runtime.tree.ParseTreeListener; + +/** + * This interface defines a complete listener for a parse tree produced by + * {@link UnifyResultParser}. + */ +public interface UnifyResultListener extends ParseTreeListener { + /** + * Enter a parse tree produced by {@link UnifyResultParser#answer}. + * @param ctx the parse tree + */ + void enterAnswer(UnifyResultParser.AnswerContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#answer}. + * @param ctx the parse tree + */ + void exitAnswer(UnifyResultParser.AnswerContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#resultSetRule}. + * @param ctx the parse tree + */ + void enterResultSetRule(UnifyResultParser.ResultSetRuleContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#resultSetRule}. + * @param ctx the parse tree + */ + void exitResultSetRule(UnifyResultParser.ResultSetRuleContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#parameterList}. + * @param ctx the parse tree + */ + void enterParameterList(UnifyResultParser.ParameterListContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#parameterList}. + * @param ctx the parse tree + */ + void exitParameterList(UnifyResultParser.ParameterListContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#value}. + * @param ctx the parse tree + */ + void enterValue(UnifyResultParser.ValueContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#value}. + * @param ctx the parse tree + */ + void exitValue(UnifyResultParser.ValueContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#parameter}. + * @param ctx the parse tree + */ + void enterParameter(UnifyResultParser.ParameterContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#parameter}. + * @param ctx the parse tree + */ + void exitParameter(UnifyResultParser.ParameterContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#equals}. + * @param ctx the parse tree + */ + void enterEquals(UnifyResultParser.EqualsContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#equals}. + * @param ctx the parse tree + */ + void exitEquals(UnifyResultParser.EqualsContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#smaller}. + * @param ctx the parse tree + */ + void enterSmaller(UnifyResultParser.SmallerContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#smaller}. + * @param ctx the parse tree + */ + void exitSmaller(UnifyResultParser.SmallerContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#typeVar}. + * @param ctx the parse tree + */ + void enterTypeVar(UnifyResultParser.TypeVarContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#typeVar}. + * @param ctx the parse tree + */ + void exitTypeVar(UnifyResultParser.TypeVarContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#type}. + * @param ctx the parse tree + */ + void enterType(UnifyResultParser.TypeContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#type}. + * @param ctx the parse tree + */ + void exitType(UnifyResultParser.TypeContext ctx); + /** + * Enter a parse tree produced by {@link UnifyResultParser#otherRule}. + * @param ctx the parse tree + */ + void enterOtherRule(UnifyResultParser.OtherRuleContext ctx); + /** + * Exit a parse tree produced by {@link UnifyResultParser#otherRule}. + * @param ctx the parse tree + */ + void exitOtherRule(UnifyResultParser.OtherRuleContext ctx); +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java new file mode 100644 index 00000000..bf0789f1 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/antlr/UnifyResultParser.java @@ -0,0 +1,639 @@ +// Generated from UnifyResult.g4 by ANTLR 4.7 +package de.dhbwstuttgart.sat.asp.parser.antlr; +import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.dfa.DFA; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.misc.*; +import org.antlr.v4.runtime.tree.*; +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; + +@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"}) +public class UnifyResultParser extends Parser { + static { RuntimeMetaData.checkVersion("4.7", RuntimeMetaData.VERSION); } + + protected static final DFA[] _decisionToDFA; + protected static final PredictionContextCache _sharedContextCache = + new PredictionContextCache(); + public static final int + T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, PARAMLIST_NAME=6, EQUALS_NAME=7, + SMALLER_NAME=8, TYPEVAR_NAME=9, TYPE_NAME=10, NAME=11, WS=12, LINE_COMMENT=13; + public static final int + RULE_answer = 0, RULE_resultSetRule = 1, RULE_parameterList = 2, RULE_value = 3, + RULE_parameter = 4, RULE_equals = 5, RULE_smaller = 6, RULE_typeVar = 7, + RULE_type = 8, RULE_otherRule = 9; + public static final String[] ruleNames = { + "answer", "resultSetRule", "parameterList", "value", "parameter", "equals", + "smaller", "typeVar", "type", "otherRule" + }; + + private static final String[] _LITERAL_NAMES = { + null, "'ANSWER'", "'.'", "'('", "','", "')'", "'param'", "'equals'", "'smaller'", + "'typeVar'", "'type'" + }; + private static final String[] _SYMBOLIC_NAMES = { + null, null, null, null, null, null, "PARAMLIST_NAME", "EQUALS_NAME", "SMALLER_NAME", + "TYPEVAR_NAME", "TYPE_NAME", "NAME", "WS", "LINE_COMMENT" + }; + public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES); + + /** + * @deprecated Use {@link #VOCABULARY} instead. + */ + @Deprecated + public static final String[] tokenNames; + static { + tokenNames = new String[_SYMBOLIC_NAMES.length]; + for (int i = 0; i < tokenNames.length; i++) { + tokenNames[i] = VOCABULARY.getLiteralName(i); + if (tokenNames[i] == null) { + tokenNames[i] = VOCABULARY.getSymbolicName(i); + } + + if (tokenNames[i] == null) { + tokenNames[i] = ""; + } + } + } + + @Override + @Deprecated + public String[] getTokenNames() { + return tokenNames; + } + + @Override + + public Vocabulary getVocabulary() { + return VOCABULARY; + } + + @Override + public String getGrammarFileName() { return "UnifyResult.g4"; } + + @Override + public String[] getRuleNames() { return ruleNames; } + + @Override + public String getSerializedATN() { return _serializedATN; } + + @Override + public ATN getATN() { return _ATN; } + + public UnifyResultParser(TokenStream input) { + super(input); + _interp = new ParserATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache); + } + public static class AnswerContext extends ParserRuleContext { + public List resultSetRule() { + return getRuleContexts(ResultSetRuleContext.class); + } + public ResultSetRuleContext resultSetRule(int i) { + return getRuleContext(ResultSetRuleContext.class,i); + } + public AnswerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_answer; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterAnswer(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitAnswer(this); + } + } + + public final AnswerContext answer() throws RecognitionException { + AnswerContext _localctx = new AnswerContext(_ctx, getState()); + enterRule(_localctx, 0, RULE_answer); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(20); + match(T__0); + setState(26); + _errHandler.sync(this); + _la = _input.LA(1); + while ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << PARAMLIST_NAME) | (1L << EQUALS_NAME) | (1L << SMALLER_NAME) | (1L << TYPEVAR_NAME) | (1L << TYPE_NAME) | (1L << NAME))) != 0)) { + { + { + setState(21); + resultSetRule(); + setState(22); + match(T__1); + } + } + setState(28); + _errHandler.sync(this); + _la = _input.LA(1); + } + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ResultSetRuleContext extends ParserRuleContext { + public ParameterContext parameter() { + return getRuleContext(ParameterContext.class,0); + } + public EqualsContext equals() { + return getRuleContext(EqualsContext.class,0); + } + public SmallerContext smaller() { + return getRuleContext(SmallerContext.class,0); + } + public TypeVarContext typeVar() { + return getRuleContext(TypeVarContext.class,0); + } + public TypeContext type() { + return getRuleContext(TypeContext.class,0); + } + public OtherRuleContext otherRule() { + return getRuleContext(OtherRuleContext.class,0); + } + public ResultSetRuleContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_resultSetRule; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterResultSetRule(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitResultSetRule(this); + } + } + + public final ResultSetRuleContext resultSetRule() throws RecognitionException { + ResultSetRuleContext _localctx = new ResultSetRuleContext(_ctx, getState()); + enterRule(_localctx, 2, RULE_resultSetRule); + try { + setState(35); + _errHandler.sync(this); + switch (_input.LA(1)) { + case PARAMLIST_NAME: + enterOuterAlt(_localctx, 1); + { + setState(29); + parameter(); + } + break; + case EQUALS_NAME: + enterOuterAlt(_localctx, 2); + { + setState(30); + equals(); + } + break; + case SMALLER_NAME: + enterOuterAlt(_localctx, 3); + { + setState(31); + smaller(); + } + break; + case TYPEVAR_NAME: + enterOuterAlt(_localctx, 4); + { + setState(32); + typeVar(); + } + break; + case TYPE_NAME: + enterOuterAlt(_localctx, 5); + { + setState(33); + type(); + } + break; + case NAME: + enterOuterAlt(_localctx, 6); + { + setState(34); + otherRule(); + } + break; + default: + throw new NoViableAltException(this); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParameterListContext extends ParserRuleContext { + public List value() { + return getRuleContexts(ValueContext.class); + } + public ValueContext value(int i) { + return getRuleContext(ValueContext.class,i); + } + public ParameterListContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_parameterList; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterParameterList(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitParameterList(this); + } + } + + public final ParameterListContext parameterList() throws RecognitionException { + ParameterListContext _localctx = new ParameterListContext(_ctx, getState()); + enterRule(_localctx, 4, RULE_parameterList); + int _la; + try { + enterOuterAlt(_localctx, 1); + { + setState(37); + match(T__2); + setState(38); + value(); + setState(43); + _errHandler.sync(this); + _la = _input.LA(1); + while (_la==T__3) { + { + { + setState(39); + match(T__3); + setState(40); + value(); + } + } + setState(45); + _errHandler.sync(this); + _la = _input.LA(1); + } + setState(46); + match(T__4); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ValueContext extends ParserRuleContext { + public TerminalNode NAME() { return getToken(UnifyResultParser.NAME, 0); } + public ResultSetRuleContext resultSetRule() { + return getRuleContext(ResultSetRuleContext.class,0); + } + public ValueContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_value; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterValue(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitValue(this); + } + } + + public final ValueContext value() throws RecognitionException { + ValueContext _localctx = new ValueContext(_ctx, getState()); + enterRule(_localctx, 6, RULE_value); + try { + setState(50); + _errHandler.sync(this); + switch ( getInterpreter().adaptivePredict(_input,3,_ctx) ) { + case 1: + enterOuterAlt(_localctx, 1); + { + setState(48); + match(NAME); + } + break; + case 2: + enterOuterAlt(_localctx, 2); + { + setState(49); + resultSetRule(); + } + break; + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class ParameterContext extends ParserRuleContext { + public TerminalNode PARAMLIST_NAME() { return getToken(UnifyResultParser.PARAMLIST_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public ParameterContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_parameter; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterParameter(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitParameter(this); + } + } + + public final ParameterContext parameter() throws RecognitionException { + ParameterContext _localctx = new ParameterContext(_ctx, getState()); + enterRule(_localctx, 8, RULE_parameter); + try { + enterOuterAlt(_localctx, 1); + { + setState(52); + match(PARAMLIST_NAME); + setState(53); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class EqualsContext extends ParserRuleContext { + public TerminalNode EQUALS_NAME() { return getToken(UnifyResultParser.EQUALS_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public EqualsContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_equals; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterEquals(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitEquals(this); + } + } + + public final EqualsContext equals() throws RecognitionException { + EqualsContext _localctx = new EqualsContext(_ctx, getState()); + enterRule(_localctx, 10, RULE_equals); + try { + enterOuterAlt(_localctx, 1); + { + setState(55); + match(EQUALS_NAME); + setState(56); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class SmallerContext extends ParserRuleContext { + public TerminalNode SMALLER_NAME() { return getToken(UnifyResultParser.SMALLER_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public SmallerContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_smaller; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterSmaller(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitSmaller(this); + } + } + + public final SmallerContext smaller() throws RecognitionException { + SmallerContext _localctx = new SmallerContext(_ctx, getState()); + enterRule(_localctx, 12, RULE_smaller); + try { + enterOuterAlt(_localctx, 1); + { + setState(58); + match(SMALLER_NAME); + setState(59); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeVarContext extends ParserRuleContext { + public TerminalNode TYPEVAR_NAME() { return getToken(UnifyResultParser.TYPEVAR_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public TypeVarContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_typeVar; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterTypeVar(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitTypeVar(this); + } + } + + public final TypeVarContext typeVar() throws RecognitionException { + TypeVarContext _localctx = new TypeVarContext(_ctx, getState()); + enterRule(_localctx, 14, RULE_typeVar); + try { + enterOuterAlt(_localctx, 1); + { + setState(61); + match(TYPEVAR_NAME); + setState(62); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class TypeContext extends ParserRuleContext { + public TerminalNode TYPE_NAME() { return getToken(UnifyResultParser.TYPE_NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public TypeContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_type; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterType(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitType(this); + } + } + + public final TypeContext type() throws RecognitionException { + TypeContext _localctx = new TypeContext(_ctx, getState()); + enterRule(_localctx, 16, RULE_type); + try { + enterOuterAlt(_localctx, 1); + { + setState(64); + match(TYPE_NAME); + setState(65); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static class OtherRuleContext extends ParserRuleContext { + public TerminalNode NAME() { return getToken(UnifyResultParser.NAME, 0); } + public ParameterListContext parameterList() { + return getRuleContext(ParameterListContext.class,0); + } + public OtherRuleContext(ParserRuleContext parent, int invokingState) { + super(parent, invokingState); + } + @Override public int getRuleIndex() { return RULE_otherRule; } + @Override + public void enterRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).enterOtherRule(this); + } + @Override + public void exitRule(ParseTreeListener listener) { + if ( listener instanceof UnifyResultListener ) ((UnifyResultListener)listener).exitOtherRule(this); + } + } + + public final OtherRuleContext otherRule() throws RecognitionException { + OtherRuleContext _localctx = new OtherRuleContext(_ctx, getState()); + enterRule(_localctx, 18, RULE_otherRule); + try { + enterOuterAlt(_localctx, 1); + { + setState(67); + match(NAME); + setState(68); + parameterList(); + } + } + catch (RecognitionException re) { + _localctx.exception = re; + _errHandler.reportError(this, re); + _errHandler.recover(this, re); + } + finally { + exitRule(); + } + return _localctx; + } + + public static final String _serializedATN = + "\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\3\17I\4\2\t\2\4\3\t"+ + "\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4\13\t\13\3"+ + "\2\3\2\3\2\3\2\7\2\33\n\2\f\2\16\2\36\13\2\3\3\3\3\3\3\3\3\3\3\3\3\5\3"+ + "&\n\3\3\4\3\4\3\4\3\4\7\4,\n\4\f\4\16\4/\13\4\3\4\3\4\3\5\3\5\5\5\65\n"+ + "\5\3\6\3\6\3\6\3\7\3\7\3\7\3\b\3\b\3\b\3\t\3\t\3\t\3\n\3\n\3\n\3\13\3"+ + "\13\3\13\3\13\2\2\f\2\4\6\b\n\f\16\20\22\24\2\2\2F\2\26\3\2\2\2\4%\3\2"+ + "\2\2\6\'\3\2\2\2\b\64\3\2\2\2\n\66\3\2\2\2\f9\3\2\2\2\16<\3\2\2\2\20?"+ + "\3\2\2\2\22B\3\2\2\2\24E\3\2\2\2\26\34\7\3\2\2\27\30\5\4\3\2\30\31\7\4"+ + "\2\2\31\33\3\2\2\2\32\27\3\2\2\2\33\36\3\2\2\2\34\32\3\2\2\2\34\35\3\2"+ + "\2\2\35\3\3\2\2\2\36\34\3\2\2\2\37&\5\n\6\2 &\5\f\7\2!&\5\16\b\2\"&\5"+ + "\20\t\2#&\5\22\n\2$&\5\24\13\2%\37\3\2\2\2% \3\2\2\2%!\3\2\2\2%\"\3\2"+ + "\2\2%#\3\2\2\2%$\3\2\2\2&\5\3\2\2\2\'(\7\5\2\2(-\5\b\5\2)*\7\6\2\2*,\5"+ + "\b\5\2+)\3\2\2\2,/\3\2\2\2-+\3\2\2\2-.\3\2\2\2.\60\3\2\2\2/-\3\2\2\2\60"+ + "\61\7\7\2\2\61\7\3\2\2\2\62\65\7\r\2\2\63\65\5\4\3\2\64\62\3\2\2\2\64"+ + "\63\3\2\2\2\65\t\3\2\2\2\66\67\7\b\2\2\678\5\6\4\28\13\3\2\2\29:\7\t\2"+ + "\2:;\5\6\4\2;\r\3\2\2\2<=\7\n\2\2=>\5\6\4\2>\17\3\2\2\2?@\7\13\2\2@A\5"+ + "\6\4\2A\21\3\2\2\2BC\7\f\2\2CD\5\6\4\2D\23\3\2\2\2EF\7\r\2\2FG\5\6\4\2"+ + "G\25\3\2\2\2\6\34%-\64"; + public static final ATN _ATN = + new ATNDeserializer().deserialize(_serializedATN.toCharArray()); + static { + _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()]; + for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) { + _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i); + } + } +} \ 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 deleted file mode 100644 index 614bddb0..00000000 --- a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.sat.asp.parser.model; - -public class ParsedASPStatement { - public ParsedASPStatement(String statement){ - - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java new file mode 100644 index 00000000..d9e06e50 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedType.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.sat.asp.parser.model; + +import java.util.List; + +public class ParsedType { + public final String name; + public final String params; + public ParsedType(String name, String params){ + this.name = name; + this.params = params; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java new file mode 100644 index 00000000..1777eabd --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPFactory.java @@ -0,0 +1,127 @@ +package de.dhbwstuttgart.sat.asp.writer; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; +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.PairOperator; + +import java.util.*; + +public class ASPFactory implements TypeVisitor{ + + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + ASPFactory factory = new ASPFactory(); + factory.convertFC(fcClasses); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + factory.convertPair(p); + } + } + + return factory.writer.getASPFile(); + } + + ASPWriter writer = new ASPWriter(); + boolean isFCType = false; + + private void convertFC(Collection classes) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes); + isFCType = true; + for(Pair fcp : fc){ + convertPair(fcp); + } + isFCType = false; + } + + private void convertPair(Pair p){ + String ls = p.TA1.acceptTV(this); + String rs = p.TA2.acceptTV(this); + ASPStatement pairStmt = null; + if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ + pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + pairStmt = makeStatement(ASPRule.ASP_PAIR_EQUALS_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + pairStmt = makeStatement(ASPRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); + }else throw new NotImplementedException(); + writer.add(pairStmt); + } + + private ASPStatement makeStatement(String rule, String... params){ + String stmt = rule + "("; + for(String param : params){ + stmt += param + ","; + } + stmt = stmt.substring(0,stmt.length()-1); + stmt += ")"; + return new ASPStatement(stmt); + } + + private String convertParameterlist(List pointers){ + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + Iterator it = pointers.iterator(); + String p = pointer; + if(!it.hasNext()){ + return ASPRule.ASP_PARAMLIST_END_POINTER.toString(); + } + while (it.hasNext()){ + ASPStatement stmt; + String type = it.next(); + String nextP = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + if(it.hasNext()){ + stmt = makeStatement(ASPRule.ASP_PARAMLIST_NAME.toString(), p, type, nextP); + }else{ + stmt = makeStatement(ASPRule.ASP_PARAMLIST_NAME.toString(), p, type, + ASPRule.ASP_PARAMLIST_END_POINTER.toString()); + } + p = nextP; + writer.add(stmt); + } + return pointer; + } + + @Override + public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + List params = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + params.add(param.acceptTV(this)); + } + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType?ASPRule.ASP_FCTYPE.toString():ASPRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); + writer.add(stmt); + return pointer; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + String name = ASPStringConverter.toConstant(typePlaceholder.getName()); + ASPStatement stmt = makeStatement(ASPRule.ASP_TYPE_VAR.toString(), name); + writer.add(stmt); + return name; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java new file mode 100644 index 00000000..11408c25 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGencayFactory.java @@ -0,0 +1,264 @@ +package de.dhbwstuttgart.sat.asp.writer; + +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; +import de.dhbwstuttgart.sat.asp.ASPStringConverter; +import de.dhbwstuttgart.sat.asp.model.ASPGencayRule; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; +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.PairOperator; + +import java.util.*; + + +public class ASPGencayFactory implements TypeVisitor { + /* TODO: + * Alle TPHs müssen als var(tph) definiert sein + * Wenn es eine Variable ist, dann direkt in die type-Regel schreiben: type(p, type, tph) + * Für die FCTypen eindeutige Namen für die pph-Regeln + * (ergibt sich von selbst, weil man einfach den Namen der TPH in der FC verwenden kann) + * paramOrder wird benötigt! + * Nur bei parameterlisten > 1 + * paramOrder(paralistPointer, parameter, num) + * (ähnlich meiner paramNum) + * Trennung von FC und Eq: + * sub -> type -> param + * ...Eq -> typeEq -> paramEq + * type..(_,_,_p): p kann sein: + * 1. Variable + * 2. ParameterPointer + * 3. null + */ + ASPWriter writer = new ASPWriter(); + boolean isFCType = false; + + private static List generateVar(ConstraintSet constraints){ + List ret = new ArrayList<>(); + for(TypePlaceholder tph : getInvolvedTPHS(constraints)){ + ret.add(makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), + ASPStringConverter.toConstant(tph.getName()))); + } + return ret; + } + + protected static Collection getInvolvedTPHS(ConstraintSet toTest) { + List ret = new ArrayList<>(); + toTest.map((Pair p)-> { + ret.addAll(p.TA1.acceptTV(new TPHExtractor())); + ret.addAll(p.TA2.acceptTV(new TPHExtractor())); + return p; + }); + return ret; + } + + public static String generateASP(ConstraintSet constraints, Collection fcClasses) throws ClassNotFoundException{ + ASPGencayFactory factory = new ASPGencayFactory(); + factory.convertFC(fcClasses); + List> constraints1 = constraints.cartesianProduct().iterator().next(); + for(Constraint constraint : constraints1){ + for(Pair p : constraint){ + factory.convertPair(p); + } + } + factory.writer.addAll(generateVar(constraints)); + + return factory.writer.getASPFile(); + } + + private void convertFC(Collection classes) throws ClassNotFoundException { + Set fc = FCGenerator.toFC(classes); + isFCType = true; + for(Pair fcp : fc){ + generateTheta((RefType) fcp.TA1); + generateTheta((RefType) fcp.TA2); + convertPair(fcp); + } + isFCType = false; + } + + private void generateTheta(RefType t){ + String rule = "theta"+t.getParaList().size() ; + String name = ASPStringConverter.toConstant(t.getName()); + writer.add(makeStatement(rule, name)); + } + + private void convertPair(Pair p){ + String ls = p.TA1.acceptTV(this); + String rs = p.TA2.acceptTV(this); + ASPStatement pairStmt = null; + if(p.GetOperator().equals(PairOperator.SMALLERDOT)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_DOT_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_EQUALS_NAME.toString(), ls, rs); + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + pairStmt = makeStatement(ASPGencayRule.ASP_PAIR_SMALLER_NAME.toString(), ls, rs); + }else throw new NotImplementedException(); + writer.add(pairStmt); + } + + private static ASPStatement makeStatement(String rule, String... params){ + String stmt = rule + "("; + for(String param : params){ + stmt += param + ","; + } + stmt = stmt.substring(0,stmt.length()-1); + stmt += ")"; + return new ASPStatement(stmt); + } + + private String convertParameterlist(List pointers){ + //if(pointers.size()==1)return pointers.get(0); + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + Iterator it = pointers.iterator(); + String p = pointer; + String paramname = ASPGencayRule.ASP_PARAMLIST_NAME.toString(); + if(this.isFCType)paramname = ASPGencayRule.ASP_FC_PARAMLIST_NAME.toString(); + if(!it.hasNext()){ + return ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + } + while (it.hasNext()){ + ASPStatement stmt; + String type = it.next(); + String nextP = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + if(it.hasNext()){ + stmt = makeStatement(paramname, p, type, nextP); + }else{ + stmt = makeStatement(paramname, p, type, + ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString()); + } + p = nextP; + writer.add(stmt); + } + return pointer; + } + + //Wird zum erstellen der pph(..) Regeln gebraucht + private String currentFCTypePointer = ""; + @Override + public String visit(RefType refType) { + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + currentFCTypePointer = pointer; + String paramPointer = ASPGencayRule.ASP_PARAMLIST_END_POINTER.toString(); + if(refType.getParaList().size() == 1 ){ + if(refType.getParaList().get(0) instanceof TypePlaceholder){ + TypePlaceholder typePlaceholder = (TypePlaceholder) refType.getParaList().get(0); + paramPointer = typePlaceholder.acceptTV(this); + } + }else{ + List params = null; + params = generateParameter(refType); + params.remove(0); + paramPointer = convertParameterlist(params); + if(refType.getParaList().size()>1){ + //paramOrder generieren: + for(String param : params) { + ASPStatement pOstmt = makeStatement(ASPGencayRule.ASP_PARAMLIST_ORDER.toString(), + paramPointer, param); + writer.add(pOstmt); + } + } + } + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, paramPointer); + writer.add(stmt); + return pointer; + /* + String pointer = ASPStringConverter.toConstant(NameGenerator.makeNewName()); + List params = null; + params = generateParameter(refType); + params.remove(0); //Das erste ist der eigentliche Typ kein parameter + String typeName = ASPStringConverter.toConstant(refType.getName()); + String ruleName = isFCType? ASPGencayRule.ASP_FCTYPE.toString(): ASPGencayRule.ASP_TYPE.toString(); + ASPStatement stmt = makeStatement(ruleName, pointer, typeName, convertParameterlist(params)); + writer.add(stmt); + return pointer; + */ + } + + /** + * Erstellt die Parameterliste, wobei type auch schon als Parameter betrachtet wird. + * Die RefTypes werden nicht zu extra type-Regeln umgewandelt. Es wird nur ihr Name als Konstante benutzt + * Das funktioniert, weil nacher das ParamOrder zuteilt, welche Typen zusammenhängen. + * Diese funktion nur verwenden, wenn auch ein paramOrder generiert wird + */ + private List generateParameter(RefType type){ + List ret = new ArrayList<>(); + ret.add(ASPStringConverter.toConstant(type.getName())); + for(RefTypeOrTPHOrWildcardOrGeneric param : type.getParaList()){ + if(param instanceof RefType){ + ret.addAll(generateParameter((RefType) param)); + }else if(param instanceof TypePlaceholder){ + ret.add(param.acceptTV(this)); + }else throw new NotImplementedException(); + } + return ret; + } + + @Override + public String visit(SuperWildcardType superWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(TypePlaceholder typePlaceholder) { + String name = ASPStringConverter.toConstant(typePlaceholder.getName()); + + ASPStatement stmt = null; + if(isFCType){ + stmt = makeStatement(ASPGencayRule.ASP_GENERIC_VAR.toString(), + currentFCTypePointer, name); + } else { + stmt = makeStatement(ASPGencayRule.ASP_TYPE_VAR.toString(), name); + } + + writer.add(stmt); + return name; + } + + @Override + public String visit(ExtendsWildcardType extendsWildcardType) { + throw new NotImplementedException(); + } + + @Override + public String visit(GenericRefType genericRefType) { + throw new NotImplementedException(); + } + + private static class TPHExtractor implements TypeVisitor>{ + @Override + public List visit(RefType refType) { + ArrayList ret = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + ret.addAll(param.acceptTV(this)); + } + return ret; + } + + @Override + public List visit(SuperWildcardType superWildcardType) { + return superWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(TypePlaceholder typePlaceholder) { + return Arrays.asList(typePlaceholder); + } + + @Override + public List visit(ExtendsWildcardType extendsWildcardType) { + return extendsWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(GenericRefType genericRefType) { + return new ArrayList<>(); + } + } + +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java deleted file mode 100644 index 002e3c7f..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java +++ /dev/null @@ -1,111 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer; - -import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.writer.model.*; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; -import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -public class ASPGenerator { - ASPWriter writer = new ASPWriter(); - private final String asp; - - public ASPGenerator(ConstraintSet constraints, Collection fcClasses){ - List> constraints1 = constraints.cartesianProduct().iterator().next(); - List constraintPairs = new ArrayList<>(); - for(Constraint constraint : constraints1){ - System.out.println(UnifyTypeFactory.convert(constraint)); - constraintPairs.addAll(constraint); - } - asp = toASP(constraintPairs, fcClasses); - } - - public String getASP(){ - return asp; - } - - private String toASP(List constraintSet, Collection fcClasses){ - TypeConverter converter = new TypeConverter(); - for(ClassOrInterface cl : fcClasses){ - ASPType superClass = cl.getSuperClass().acceptTV(converter); - ASPPairSmaller fcEntry = new ASPPairSmaller(convert(cl), superClass); - writer.add(new ASPStatement(fcEntry.toASP())); - } - for(Pair cons : constraintSet){ - writer.add(new ASPStatement(convert(cons).toASP())); - } - - return writer.getASPFile(); - } - - private ASPPair convert(Pair pair){ - TypeConverter converter = new TypeConverter(); - ASPType ls = pair.TA1.acceptTV(converter); - ASPType rs = pair.TA2.acceptTV(converter); - if(pair.OperatorEqual()){ - return new ASPPairEquals(ls, rs); - }else if(pair.OperatorSmallerDot()){ - return new ASPPairSmallerDot(ls, rs); - }else throw new NotImplementedException(); - } - - private ASPType convert(ClassOrInterface cl){ - List paramList = new ArrayList<>(); - for(GenericTypeVar gtv : cl.getGenerics()){ - paramList.add(new ASPGenericType(toConstant(gtv.getName()))); - } - ASPParameterList params = new ASPParameterList(paramList, writer); - return new ASPRefType(toConstant(cl.getClassName()), params); - } - - public static String toConstant(JavaClassName name){ - return toConstant(name.toString().replace(".", "_")); - } - - public static String toConstant(String name){ - return "c" + name.toString().replace(".", "_"); - } - - private class TypeConverter implements TypeVisitor{ - - @Override - public ASPType visit(RefType type) { - List paramList = new ArrayList<>(); - for(RefTypeOrTPHOrWildcardOrGeneric gtv : type.getParaList()){ - paramList.add(gtv.acceptTV(this)); - } - ASPParameterList params = new ASPParameterList(paramList, writer); - return new ASPRefType(toConstant(type.getName()), params); - } - - @Override - public ASPType visit(SuperWildcardType superWildcardType) { - throw new NotImplementedException(); - } - - @Override - public ASPType visit(TypePlaceholder typePlaceholder) { - return new ASPTypeVar(toConstant(typePlaceholder.getName())); - } - - @Override - public ASPType visit(ExtendsWildcardType extendsWildcardType) { - throw new NotImplementedException(); - } - - @Override - public ASPType visit(GenericRefType genericRefType) { - return new ASPRefType(toConstant(genericRefType.getName()), - new ASPParameterList(new ArrayList<>(), writer)); - } - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPStatement.java similarity index 91% rename from src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPStatement.java index 76bb63e8..8d342464 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPStatement.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.writer.model; +package de.dhbwstuttgart.sat.asp.writer; public class ASPStatement { private final String stmt; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index 450950d9..57aef860 100644 --- a/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,8 +1,8 @@ package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; - +import java.util.Collection; import java.util.HashSet; +import java.util.List; public class ASPWriter { @@ -19,4 +19,8 @@ public class ASPWriter { } return ret; } + + public void addAll(Collection aspStatements) { + content.addAll(aspStatements); + } } diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java deleted file mode 100644 index 57619edf..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -public class ASPGenericType implements ASPType{ - private final String name; - - public ASPGenericType(String name){ - this.name = name; - } - - public String toString(){ - return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java deleted file mode 100644 index 662f995c..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -public abstract class ASPPair { - public final ASPType leftSide; - public final ASPType rightSide; - - public ASPPair(ASPType ls, ASPType rs){ - this.leftSide = ls; - this.rightSide = rs; - } - - public String toASP(){ - return this.getRuleName() + "(" + leftSide + ","+ rightSide + ")"; - } - - public String toString(){ - return toASP(); - } - - protected abstract String getRuleName(); -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java deleted file mode 100644 index 8806f2ce..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -public class ASPPairEquals extends ASPPair{ - public ASPPairEquals(ASPType ls, ASPType rs){ - super(ls, rs); - } - - @Override - protected String getRuleName() { - return ASPRule.ASP_PAIR_EQUALS_NAME.toString(); - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java deleted file mode 100644 index f57170f6..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java +++ /dev/null @@ -1,16 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -import java.util.Map; - -public class ASPPairSmaller extends ASPPair{ - public ASPPairSmaller(ASPType ls, ASPType rs){ - super(ls, rs); - } - - @Override - protected String getRuleName() { - return ASPRule.ASP_PAIR_SMALLER_NAME.toString(); - } -} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java deleted file mode 100644 index e8482bfc..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -public class ASPPairSmallerDot extends ASPPair{ - public ASPPairSmallerDot(ASPType ls, ASPType rs){ - super(ls, rs); - } - - @Override - protected String getRuleName() { - return ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(); - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java deleted file mode 100644 index 3c176e49..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java +++ /dev/null @@ -1,45 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -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.Iterator; -import java.util.List; - -public class ASPParameterList { - 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 = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); - }else{ - name = newName(); - String nextPointer = name; - Iterator it = types.iterator(); - while(it.hasNext()){ - ASPType t = it.next(); - String param = nextPointer + "," + t.toString() + ","; - nextPointer = newName(); - if(! it.hasNext())nextPointer = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); - param += nextPointer; - 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 + ")")); - } - } - } - - private String newName() { - return ASPGenerator.toConstant(NameGenerator.makeNewName()); - } - - public String toString(){ - return name; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java deleted file mode 100644 index 6491ff33..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -import de.dhbwstuttgart.sat.asp.model.ASPRule; - -public class ASPRefType implements ASPType { - private final ASPParameterList params; - private final String name; - - public ASPRefType(String name, ASPParameterList params){ - this.name = name; - this.params = params; - } - - public ASPParameterList getParams() { - return params; - } - - public String toString(){ - return ASPRule.ASP_TYPE + "(" + name +"," + params.name + ")"; - } -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java deleted file mode 100644 index da694218..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -public interface ASPType { -} diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java deleted file mode 100644 index 4b07d30b..00000000 --- a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.sat.asp.writer.model; - -public class ASPTypeVar implements ASPType{ - private final String name; - - public ASPTypeVar(String name){ - this.name = name; - } - - @Override - public String toString() { - return "typeVar("+ name +")"; - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index 5d5fc3e2..72097fc0 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -13,6 +13,7 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; import org.antlr.v4.runtime.Token; import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -26,11 +27,11 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ private GenericDeclarationList genericClassParameters; private RefType superClass; protected boolean isInterface; - private List implementedInterfaces; + private List implementedInterfaces; private List constructors; public ClassOrInterface(int modifiers, JavaClassName name, List fielddecl, List methods, List constructors, GenericDeclarationList genericClassParameters, - RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset){ + RefType superClass, Boolean isInterface, List implementedInterfaces, Token offset){ super(offset); this.modifiers = modifiers; this.name = name; @@ -101,4 +102,8 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope{ public void accept(ASTVisitor visitor) { visitor.visit(this); } + + public Collection getSuperInterfaces() { + return implementedInterfaces; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java b/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java index 02042ca3..6ee03b60 100644 --- a/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/GenericTypeVar.java @@ -1,7 +1,5 @@ package de.dhbwstuttgart.syntaxtree; -import de.dhbwstuttgart.parser.scope.GenericTypeName; -import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; @@ -26,9 +24,9 @@ public class GenericTypeVar extends SyntaxTreeNode */ List bounds=new ArrayList(); private Token endOffset; - private GenericTypeName name; + private String name; - public GenericTypeVar(GenericTypeName s, List bounds, Token offset, Token endOffset) + public GenericTypeVar(String s, List bounds, Token offset, Token endOffset) { super(offset); name = s; @@ -50,18 +48,15 @@ public class GenericTypeVar extends SyntaxTreeNode return "BoGTV " + this.name; } - public GenericTypeName getName() { - return name; - } - - public String getParsedName(){ + public String getName(){ return name.toString(); } + /* public JavaClassName definingClass(){ return name.getParentClass(); } - +*/ @Override public void accept(ASTVisitor visitor) { visitor.visit(this); diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index b0683569..52921c5d 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -8,7 +8,6 @@ import java.util.List; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; -import de.dhbwstuttgart.parser.scope.GenericTypeName; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassRegistry; import de.dhbwstuttgart.syntaxtree.Field; @@ -43,19 +42,22 @@ public class ASTFactory { } int modifier = jreClass.getModifiers(); boolean isInterface = jreClass.isInterface(); + //see: https://stackoverflow.com/questions/9934774/getting-generic-parameter-from-supertype-class + ParameterizedType parameterSuperClass = null; + Type tempSuperClass = jreClass.getGenericSuperclass(); + if(tempSuperClass != null && tempSuperClass instanceof ParameterizedType) + parameterSuperClass = (ParameterizedType) tempSuperClass; java.lang.Class superjreClass = jreClass.getSuperclass(); RefType superClass; - if(superjreClass != null){ - List params = new ArrayList<>(); - for(TypeVariable tv : superjreClass.getTypeParameters()){ - params.add(new GenericRefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); - } - superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); + if(parameterSuperClass != null){ + superClass = (RefType) convertType(parameterSuperClass); + }else if(superjreClass != null){ + superClass = (RefType) convertType(superjreClass); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) superClass = (RefType) createType(java.lang.Object.class, name, ""); } List implementedInterfaces = new ArrayList<>(); - for(java.lang.Class jreInterface : jreClass.getInterfaces()){ + for(Type jreInterface : jreClass.getGenericInterfaces()){ implementedInterfaces.add((RefType) createType(jreInterface, name, "")); } GenericDeclarationList genericDeclarationList = createGenerics(jreClass.getTypeParameters(), jreClass, null); @@ -65,18 +67,46 @@ public class ASTFactory { return new ClassOrInterface(modifier, name, felder, methoden, konstruktoren, genericDeclarationList, superClass,isInterface, implementedInterfaces, offset); } + private static RefTypeOrTPHOrWildcardOrGeneric convertType(Type type){ + if(type instanceof ParameterizedType){ + List params = new ArrayList<>(); + for(Type paramType : ((ParameterizedType)type).getActualTypeArguments()){ + params.add(convertType(paramType)); + } + JavaClassName name = new JavaClassName(((ParameterizedType) type).getRawType().getTypeName()); + return new RefType(name, params, new NullToken()); + }else if(type instanceof TypeVariable){ + return new GenericRefType(((TypeVariable) type).getName(), new NullToken()); + }else if(type instanceof Class){ + List params = new ArrayList<>(); + Class paramClass = (Class) type; + for(TypeVariable tv : paramClass.getTypeParameters()){ + params.add(new GenericRefType(tv.getName(), new NullToken())); + } + JavaClassName name = new JavaClassName(paramClass.getName()); + return new RefType(name, params, new NullToken()); + }else throw new NotImplementedException(); + } + private static Field createField(java.lang.reflect.Field field, JavaClassName jreClass) { return new Field(field.getName(), createType(field.getType(), jreClass, null), field.getModifiers(), new NullToken()); } + //private static RefType createType(Class classType) { + // return createClass(classType).getType(); + //} + private static de.dhbwstuttgart.syntaxtree.Constructor createConstructor(Constructor constructor, Class inClass) { String name = constructor.getName(); RefTypeOrTPHOrWildcardOrGeneric returnType = createType(inClass, new JavaClassName(inClass.getName()), name); Parameter[] jreParams = constructor.getParameters(); + Type[] jreGenericParams = constructor.getGenericParameterTypes(); List params = new ArrayList<>(); - for(Parameter jreParam : jreParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),new JavaClassName(inClass.getName()), name); - params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); + int i = 0; + for(Type jreParam : jreGenericParams){ + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam,new JavaClassName(inClass.getName()), name); + params.add(new FormalParameter(jreParams[i].getName(),paramType, new NullToken())); + i++; } ParameterList parameterList = new ParameterList(params, new NullToken()); Block block = new Block(new ArrayList(), new NullToken()); @@ -89,21 +119,26 @@ public class ASTFactory { } return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); - } - - //private static RefType createType(Class classType) { - // return createClass(classType).getType(); - //} + } public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ String name = jreMethod.getName(); RefTypeOrTPHOrWildcardOrGeneric returnType; - returnType = createType(jreMethod.getReturnType(),new JavaClassName(inClass.getName()), name); + Type jreRetType; + if(jreMethod.getGenericReturnType()!=null){ + jreRetType = jreMethod.getGenericReturnType(); + }else{ + jreRetType = jreMethod.getReturnType(); + } + returnType = createType(jreRetType,new JavaClassName(inClass.getName()), name); Parameter[] jreParams = jreMethod.getParameters(); + Type[] jreGenericParams = jreMethod.getGenericParameterTypes(); List params = new ArrayList<>(); - for(Parameter jreParam : jreParams){ - RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam.getType(),new JavaClassName(inClass.getName()), name); - params.add(new FormalParameter(jreParam.getName(),paramType, new NullToken())); + int i = 0; + for(Type jreParam : jreGenericParams){ + RefTypeOrTPHOrWildcardOrGeneric paramType = createType(jreParam,new JavaClassName(inClass.getName()), name); + params.add(new FormalParameter(jreParams[i].getName(),paramType, new NullToken())); + i++; } ParameterList parameterList = new ParameterList(params, new NullToken()); Block block = new Block(new ArrayList(), new NullToken()); @@ -153,9 +188,7 @@ public class ASTFactory { }else{ if(type instanceof TypeVariable){ //GTVDeclarationContext via "(TypeVariable) type).getGenericDeclaration()" - return new GenericRefType( - new GenericTypeName(new GenericContext(parentClass, parentMethod),type.getTypeName()), - new NullToken()); + return new GenericRefType(type.getTypeName(), new NullToken()); } List params = new ArrayList<>(); if(type instanceof ParameterizedType){ @@ -163,7 +196,12 @@ public class ASTFactory { params.add(createType(t, parentClass, parentMethod)); } } - RefType ret = new RefType(new JavaClassName(type.getTypeName()), params, new NullToken()); + String name = type.getTypeName(); + if(name.contains("<")){ //Komischer fix. Type von Generischen Typen kann die Generics im Namen enthalten Type
+ //Diese entfernen: + name = name.split("<")[0]; + } + RefType ret = new RefType(new JavaClassName(name), params, new NullToken()); return ret; } } @@ -177,9 +215,7 @@ public class ASTFactory { genericBounds.add((RefType) createType(bound, parentClass, parentMethod)); } } - return new de.dhbwstuttgart.syntaxtree.GenericTypeVar( - new GenericTypeName(new GenericContext(parentClass, parentMethod), jreTVName) - , genericBounds, new NullToken(), new NullToken()); + return new de.dhbwstuttgart.syntaxtree.GenericTypeVar(jreTVName, genericBounds, new NullToken(), new NullToken()); } public static ClassOrInterface createObjectClass() { diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index a962c2bc..e58ce50f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.syntaxtree.factory; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import de.dhbwstuttgart.exceptions.DebugException; @@ -8,8 +10,9 @@ 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.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.syntaxtree.type.WildcardType; @@ -35,7 +38,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - return new FiniteClosure(FCGenerator.toFC(fromClasses)); + return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses)); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){ @@ -74,6 +77,16 @@ public class UnifyTypeFactory { } public static UnifyType convert(RefType t){ + //Check if it is a FunN Type: + Pattern p = Pattern.compile("Fun(\\d+)"); + Matcher m = p.matcher(t.getName().toString()); + boolean b = m.matches(); + if(b){ + Integer N = Integer.valueOf(m.group(1)); + if((N + 1) == t.getParaList().size()){ + return convert(new FunN(t.getParaList())); + } + } UnifyType ret; if(t.getParaList() != null && t.getParaList().size() > 0){ List params = new ArrayList<>(); @@ -129,10 +142,13 @@ public class UnifyTypeFactory { UnifyPair ret = generateSmallerDotPair(UnifyTypeFactory.convert(p.TA1) , UnifyTypeFactory.convert(p.TA2)); return ret; - }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)){ + }else if(p.GetOperator().equals(PairOperator.EQUALSDOT)) { UnifyPair ret = generateEqualDotPair(UnifyTypeFactory.convert(p.TA1) , UnifyTypeFactory.convert(p.TA2)); return ret; + }else if(p.GetOperator().equals(PairOperator.SMALLER)){ + return generateSmallerPair(UnifyTypeFactory.convert(p.TA1), + UnifyTypeFactory.convert(p.TA2)); }else throw new NotImplementedException(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index f7db0481..bb631e04 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -13,16 +13,17 @@ public class BinaryExpr extends Expression } public enum Operator{ - ADD, - SUB, - MUL, - AND, - OR, - DIV, - LESSTHAN, - BIGGERTHAN, - LESSEQUAL, - BIGGEREQUAL + ADD, // + + SUB, // - + MUL, // * + MOD, // Modulo Operator % + AND, // && + OR, // || + DIV, // / + LESSTHAN, // < + BIGGERTHAN, // > + LESSEQUAL, // <= + BIGGEREQUAL // >= } public final Operator operation; diff --git a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java index a5d394aa..a14813e7 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/GenericRefType.java @@ -1,25 +1,19 @@ package de.dhbwstuttgart.syntaxtree.type; -import de.dhbwstuttgart.parser.scope.GenericTypeName; -import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.typeinference.result.ResultSetVisitor; import org.antlr.v4.runtime.Token; public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric { - private GenericTypeName name; + private String name; - public GenericRefType(GenericTypeName name, Token offset) + public GenericRefType(String name, Token offset) { super(offset); this.name = name; } - public GenericTypeName getName(){ - return name; - } - public String getParsedName(){ return name.toString(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java index 4cf85297..3c9a5907 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/RefType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/RefType.java @@ -29,14 +29,17 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric @Override public String toString(){ - String params = "<"; - Iterator it = parameter.iterator(); - while(it.hasNext()){ - RefTypeOrTPHOrWildcardOrGeneric param = it.next(); - params += param.toString(); - if(it.hasNext())params += ", "; + String params = ""; + if(parameter.size()>0){ + params += "<"; + Iterator it = parameter.iterator(); + while(it.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric param = it.next(); + params += param.toString(); + if(it.hasNext())params += ", "; + } + params += ">"; } - params += ">"; return this.name.toString() + params; } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index f630583e..740c80e5 100644 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -30,7 +30,7 @@ public class SuperWildcardType extends WildcardType{ * Beispiel: ? super Integer. * Integer wird zurückgegeben. */ - public RefTypeOrTPHOrWildcardOrGeneric get_SuperType() + public RefTypeOrTPHOrWildcardOrGeneric getInnerType() { return this.innerType; } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index c7a06502..0c623548 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.syntaxtree.visual; -import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; @@ -171,7 +170,7 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(GenericRefType genericRefType) { - out.append(genericRefType.getName().toString()); + out.append(genericRefType.getParsedName().toString()); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java new file mode 100644 index 00000000..f50b05a6 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetOutputGenerator.java @@ -0,0 +1,34 @@ +package de.dhbwstuttgart.syntaxtree.visual; + +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.result.*; + +public class ResultSetOutputGenerator extends OutputGenerator implements ResultSetVisitor{ + + public ResultSetOutputGenerator(StringBuilder out) { + super(out); + } + + @Override + public void visit(PairTPHsmallerTPH p) { + print(p, "<"); + } + + @Override + public void visit(PairTPHequalRefTypeOrWildcardType p) { + print(p, "=."); + } + + @Override + public void visit(PairTPHEqualTPH p) { + print(p, "=."); + } + + private void print(ResultPair p , String operator){ + out.append("("); + p.getLeft().accept((ResultSetVisitor) this); + out.append(" "+operator+" "); + p.getRight().accept((ResultSetVisitor) this); + out.append(")"); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java new file mode 100644 index 00000000..fc67a723 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/visual/ResultSetPrinter.java @@ -0,0 +1,20 @@ +package de.dhbwstuttgart.syntaxtree.visual; + +import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +import java.util.Set; + +public class ResultSetPrinter { + + public static String print(ResultSet toPrint){ + StringBuilder output = new StringBuilder(); + for(ResultPair p : toPrint.results){ + p.accept(new ResultSetOutputGenerator(output)); + output.append("\n"); + } + return output.toString(); + } + +} diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 29492c31..8834f859 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -126,7 +126,7 @@ class TypeToInsertString implements ResultSetVisitor{ @Override public void visit(GenericRefType genericRefType) { - insert += genericRefType.getName(); + insert += genericRefType.getParsedName(); } @Override diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java index 62867a26..e039dcbe 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/FieldAssumption.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.assumptions; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.TypeScope; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; @@ -25,7 +26,7 @@ public class FieldAssumption extends Assumption{ } public RefTypeOrTPHOrWildcardOrGeneric getReceiverType(GenericsResolver resolver) { - - return null; + //TODO + throw new NotImplementedException(); } } diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java index a04ce9bb..3518c16a 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/FunNClass.java @@ -1,8 +1,6 @@ package de.dhbwstuttgart.typeinference.assumptions; import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; -import de.dhbwstuttgart.parser.scope.GenericTypeName; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericDeclarationList; @@ -29,8 +27,7 @@ public class FunNClass extends ClassOrInterface { private static GenericDeclarationList createGenerics(List funNParams) { List generics = new ArrayList<>(); for(RefTypeOrTPHOrWildcardOrGeneric param : funNParams){ - generics.add(new GenericTypeVar(new GenericTypeName(new GenericContext( - new JavaClassName("Fun"+(funNParams.size()-1)), null), NameGenerator.makeNewName()), + generics.add(new GenericTypeVar(NameGenerator.makeNewName(), new ArrayList<>(), new NullToken(), new NullToken())); } return new GenericDeclarationList(generics, new NullToken()); diff --git a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java index 066c3246..e17281e5 100644 --- a/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java +++ b/src/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java @@ -3,9 +3,7 @@ package de.dhbwstuttgart.typeinference.assumptions; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; -import de.dhbwstuttgart.syntaxtree.ParameterList; import de.dhbwstuttgart.syntaxtree.TypeScope; -import de.dhbwstuttgart.syntaxtree.statement.Assign; import de.dhbwstuttgart.syntaxtree.type.FunN; import de.dhbwstuttgart.syntaxtree.type.GenericRefType; import de.dhbwstuttgart.syntaxtree.type.RefType; @@ -14,7 +12,6 @@ import de.dhbwstuttgart.typeinference.constraints.GenericsResolver; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; public class MethodAssumption extends Assumption{ private ClassOrInterface receiver; @@ -40,7 +37,8 @@ public class MethodAssumption extends Assumption{ return receiver; } - public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { + public RefTypeOrTPHOrWildcardOrGeneric getReturnType(GenericsResolver resolver) { + if(retType instanceof GenericRefType)return resolver.resolve(retType); return retType; } diff --git a/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java b/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java index 64d0ce0b..c24fe211 100644 --- a/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java +++ b/src/de/dhbwstuttgart/typeinference/result/PairTPHEqualTPH.java @@ -9,7 +9,7 @@ public class PairTPHEqualTPH extends ResultPair numericAdditionOrStringConcatenation = new HashSet<>(); + Constraint numeric = new Constraint<>(); + //Zuerst der Fall für Numerische AusdrücPairOpnumericeratorke, das sind Mul, Mod und Div immer: + //see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17 + //Expression muss zu Numeric Convertierbar sein. also von Numeric erben + numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); + numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); + /* + In Java passiert bei den binären Operatoren eine sogenannte Type Promotion: + https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2 + Das bedeutet, dass Java die Typen je nach belieben castet, so lange sie nur von Number erben + */ + numeric.add(new Pair(binary.getType(), number, PairOperator.SMALLERDOT)); + numericAdditionOrStringConcatenation.add(numeric); + if(binary.operation.equals(BinaryExpr.Operator.ADD)) { + //Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2) + Constraint stringConcat = new Constraint<>(); + stringConcat.add(new Pair(binary.lexpr.getType(), string, PairOperator.EQUALSDOT)); + stringConcat.add(new Pair(binary.rexpr.getType(), string, PairOperator.EQUALSDOT)); + stringConcat.add(new Pair(binary.getType(), string, PairOperator.EQUALSDOT)); + numericAdditionOrStringConcatenation.add(stringConcat); + } + constraintsSet.addOderConstraint(numericAdditionOrStringConcatenation); + }else if(binary.operation.equals(BinaryExpr.Operator.LESSEQUAL) || + binary.operation.equals(BinaryExpr.Operator.BIGGEREQUAL) || + binary.operation.equals(BinaryExpr.Operator.BIGGERTHAN) || + binary.operation.equals(BinaryExpr.Operator.LESSTHAN)){ + constraintsSet.addUndConstraint(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT)); + constraintsSet.addUndConstraint(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT)); + //Rückgabetyp ist Boolean + constraintsSet.addUndConstraint(new Pair(bool, binary.getType(), PairOperator.EQUALSDOT)); + }else{ + throw new NotImplementedException(); + } + } + @Override public void visit(Literal literal) { - //Nothing to do here. Literale kriegen beim parsen den korrekten Typ. + //Nothing to do here. Literale erzeugen keine Constraints } @Override @@ -303,14 +342,14 @@ public class TYPEStmt implements StatementVisitor{ List params = new ArrayList<>(); for(GenericTypeVar gtv : receiverCl.getGenerics()){ //Die Generics werden alle zu TPHs umgewandelt. - params.add(resolver.resolve(gtv.getParsedName())); + params.add(resolver.resolve(gtv.getName())); } RefTypeOrTPHOrWildcardOrGeneric receiverType = new RefType(assumption.getReceiver().getClassName(), params, forMethod.getOffset()); */ methodConstraint.add(new Pair(forMethod.receiver.getType(), assumption.getReceiverType(resolver), PairOperator.SMALLERDOT)); - methodConstraint.add(new Pair(assumption.getReturnType(), forMethod.getType(), + methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), PairOperator.EQUALSDOT)); methodConstraint.addAll(generateParameterConstraints(forMethod, assumption, info, resolver)); return methodConstraint; @@ -354,7 +393,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.getReturnType()); + RefTypeOrTPHOrWildcardOrGeneric retType = m.getReturnType();//info.checkGTV(m.getReturnType()); ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(),info), createTypeScope(cl, m))); @@ -403,7 +442,7 @@ public class TYPEStmt implements StatementVisitor{ protected Constraint generateConstructorConstraint(NewClass forConstructor, MethodAssumption assumption, TypeInferenceBlockInformation info, GenericsResolver resolver){ Constraint methodConstraint = new Constraint(); - methodConstraint.add(new Pair(assumption.getReturnType(), forConstructor.getType(), + methodConstraint.add(new Pair(assumption.getReturnType(resolver), forConstructor.getType(), PairOperator.SMALLERDOT)); methodConstraint.addAll(generateParameterConstraints(forConstructor, assumption, info, resolver)); return methodConstraint; diff --git a/src/de/dhbwstuttgart/typeinference/unify/Match.java b/src/de/dhbwstuttgart/typeinference/unify/Match.java new file mode 100644 index 00000000..78f7360f --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/Match.java @@ -0,0 +1,89 @@ +package de.dhbwstuttgart.typeinference.unify; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import de.dhbwstuttgart.typeinference.unify.interfaces.IMatch; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType; +import de.dhbwstuttgart.typeinference.unify.model.TypeParams; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; + +/** + * Implementation of match derived from unification algorithm. + * @author Martin Pluemicke + */ +public class Match implements IMatch { + + @Override + public Optional match(ArrayList termsList) { + + // Start with the identity unifier. Substitutions will be added later. + Unifier mgu = Unifier.identity(); + + // Apply rules while possible + int idx = 0; + while(idx < termsList.size()) { + UnifyPair pair = termsList.get(idx); + UnifyType rhsType = pair.getRhsType(); + UnifyType lhsType = pair.getLhsType(); + TypeParams rhsTypeParams = rhsType.getTypeParams(); + TypeParams lhsTypeParams = lhsType.getTypeParams(); + + // REDUCE - Rule + if(!(rhsType instanceof PlaceholderType) && !(lhsType instanceof PlaceholderType)) { + Set result = new HashSet<>(); + + // f<...> = g<...> with f != g are not unifiable + if(!rhsType.getName().equals(lhsType.getName())) + return Optional.empty(); // conflict + // f = f are not unifiable + if(rhsTypeParams.size() != lhsTypeParams.size()) + return Optional.empty(); // conflict + // f = g is not unifiable (cannot be f = f because erase rule would have been applied) + //if(rhsTypeParams.size() == 0) + //return Optional.empty(); + + // Unpack the arguments + for(int i = 0; i < rhsTypeParams.size(); i++) + result.add(new UnifyPair(lhsTypeParams.get(i), rhsTypeParams.get(i), PairOperator.EQUALSDOT)); + + termsList.remove(idx); + termsList.addAll(result); + continue; + } + + // DELETE - Rule + if(pair.getRhsType().equals(pair.getLhsType())) { + termsList.remove(idx); + continue; + } + + // SWAP - Rule + if(!(lhsType instanceof PlaceholderType) && (rhsType instanceof PlaceholderType)) { + return Optional.empty(); // conflict + } + + // OCCURS-CHECK + //deleted + + // SUBST - Rule + if(lhsType instanceof PlaceholderType) { + mgu.add((PlaceholderType) lhsType, rhsType); + termsList = termsList.stream().map(mgu::applyleft).collect(Collectors.toCollection(ArrayList::new)); + idx = idx+1 == termsList.size() ? 0 : idx+1; + continue; + } + + idx++; + } + + return Optional.of(mgu); + } +} diff --git a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 2005a387..e51679e2 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -34,6 +34,7 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyType; public class TypeUnifyTask extends RecursiveTask>> { private static final long serialVersionUID = 1L; + private static int i = 0; /** * The implementation of setOps that will be used during the unification @@ -77,6 +78,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Step 1: Repeated application of reduce, adapt, erase, swap */ + //System.out.println("Unifikation: " + eq); Set eq0 = applyTypeUnificationRules(eq, fc); /* @@ -97,6 +99,8 @@ public class TypeUnifyTask extends RecursiveTask>> { // cartesian product of the sets created by pattern matching. List>> topLevelSets = new ArrayList<>(); + System.out.println(eq2s); + if(eq1s.size() != 0) { // Do not add empty sets or the cartesian product will always be empty. Set> wrap = new HashSet<>(); wrap.add(eq1s); @@ -134,12 +138,16 @@ public class TypeUnifyTask extends RecursiveTask>> { // Sub cartesian products of the second level (pattern matched) sets // "the big (x)" for(Set>> secondLevelSet : secondLevelSets) { + //System.out.println("secondLevelSet "+secondLevelSet.size()); List>> secondLevelSetList = new ArrayList<>(secondLevelSet); Set>> cartResult = setOps.cartesianProduct(secondLevelSetList); - + //System.out.println("CardResult: "+cartResult.size()); // Flatten and add to top level sets Set> flat = new HashSet<>(); + int j = 0; for(List> s : cartResult) { + j++; + //System.out.println("s from CardResult: "+cartResult.size() + " " + j); Set flat1 = new HashSet<>(); for(Set s1 : s) flat1.addAll(s1); @@ -164,6 +172,7 @@ public class TypeUnifyTask extends RecursiveTask>> { /* * Step 5: Substitution */ + System.out.println("vor Subst: " + eqPrime); Optional> eqPrimePrime = rules.subst(eqPrime); /* @@ -176,11 +185,13 @@ public class TypeUnifyTask extends RecursiveTask>> { //(!eqPrimePrime.isPresent()) erfolgt ist, ist das Ergebnis erzielt. eqPrimePrimeSet.add(eqPrime); else if(eqPrimePrime.isPresent()) { + System.out.println("nextStep: " + eqPrimePrime.get()); TypeUnifyTask fork = new TypeUnifyTask(eqPrimePrime.get(), fc, true); forks.add(fork); fork.fork(); } else { + System.out.println("nextStep: " + eqPrime); TypeUnifyTask fork = new TypeUnifyTask(eqPrime, fc, true); forks.add(fork); fork.fork(); @@ -381,9 +392,12 @@ public class TypeUnifyTask extends RecursiveTask>> { UnifyType rhsType = pair.getRhsType(); // Case 1: (a <. Theta') - if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) - result.get(0).add(unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc)); - + if(pairOp == PairOperator.SMALLERDOT && lhsType instanceof PlaceholderType) { + System.out.println(pair); + Set> x1 = unifyCase1((PlaceholderType) pair.getLhsType(), pair.getRhsType(), fc); + System.out.println(x1); + result.get(0).add(x1); + } // Case 2: (a <.? ? ext Theta') else if(pairOp == PairOperator.SMALLERDOTWC && lhsType instanceof PlaceholderType && rhsType instanceof ExtendsType) result.get(1).add(unifyCase2((PlaceholderType) pair.getLhsType(), (ExtendsType) pair.getRhsType(), fc)); @@ -442,13 +456,24 @@ public class TypeUnifyTask extends RecursiveTask>> { break; } - Set cs = fc.getAllTypesByName(thetaPrime.getName()); - cs.add(thetaPrime); + Set cs = fc.getAllTypesByName(thetaPrime.getName());//cs= [java.util.Vector, java.util.Vector>, ????java.util.Vector???] + + //PL 18-02-06 entfernt, kommt durch unify wieder rein + //cs.add(thetaPrime); + //PL 18-02-06 entfernt for(UnifyType c : cs) { - Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); - thetaQs.add(thetaPrime); //PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert, - //da children offensichtlich ein echtes kleiner und kein kleinergleich ist + //PL 18-02-05 getChildren durch smaller ersetzt in getChildren werden die Varianlen nicht ersetzt. + Set thetaQs = fc.smaller(c).stream().collect(Collectors.toCollection(HashSet::new)); + //Set thetaQs = fc.getChildren(c).stream().collect(Collectors.toCollection(HashSet::new)); + //thetaQs.add(thetaPrime); //PL 18-02-05 wieder geloescht + //PL 2017-10-03: War auskommentiert habe ich wieder einkommentiert, + //da children offensichtlich ein echtes kleiner und kein kleinergleich ist + + //PL 18-02-06: eingefuegt, thetaQs der Form V> <. V'> werden entfernt + thetaQs = thetaQs.stream().filter(ut -> ut.getTypeParams().arePlaceholders()).collect(Collectors.toCollection(HashSet::new)); + //PL 18-02-06: eingefuegt + Set thetaQPrimes = new HashSet<>(); TypeParams cParams = c.getTypeParams(); if(cParams.size() == 0) @@ -464,6 +489,11 @@ public class TypeUnifyTask extends RecursiveTask>> { } for(UnifyType tqp : thetaQPrimes) { + //System.out.println(tqp.toString()); + //i++; + //System.out.println(i); + //if (i == 62) + // System.out.println(tqp.toString()); Optional opt = stdUnify.unify(tqp, thetaPrime); if (!opt.isPresent()) continue; @@ -474,10 +504,11 @@ public class TypeUnifyTask extends RecursiveTask>> { for (Entry sigma : unifier) substitutionSet.add(new UnifyPair(sigma.getKey(), sigma.getValue(), PairOperator.EQUALSDOT)); - List freshTphs = new ArrayList<>(); + //List freshTphs = new ArrayList<>(); PL 18-02-06 in die For-Schleife verschoben for (UnifyType tq : thetaQs) { - Set smaller = fc.smaller(unifier.apply(tq)); + Set smaller = fc.smaller(unifier.apply(tq)); for(UnifyType theta : smaller) { + List freshTphs = new ArrayList<>(); Set resultPrime = new HashSet<>(); for(int i = 0; !allGen && i < theta.getTypeParams().size(); i++) { diff --git a/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java new file mode 100644 index 00000000..5e201dd7 --- /dev/null +++ b/src/de/dhbwstuttgart/typeinference/unify/interfaces/IMatch.java @@ -0,0 +1,35 @@ +package de.dhbwstuttgart.typeinference.unify.interfaces; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import de.dhbwstuttgart.typeinference.unify.model.UnifyType; +import de.dhbwstuttgart.typeinference.unify.model.Unifier; +import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; + +/** + * Match + * @author Martin Pluemicke + */ +public interface IMatch { + + /** + * Finds the most general unifier sigma of the set {t1,...,tn} so that + * sigma(t1) = sigma(t2) = ... = sigma(tn). + * @param terms The set of terms to be unified + * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. + */ + public Optional match(ArrayList termsList); + + /** + * Finds the most general unifier sigma of the set {t1,...,tn} so that + * sigma(t1) = sigma(t2) = ... = sigma(tn). + * @param terms The set of terms to be unified + * @return An optional of the most general unifier if it exists or an empty optional if there is no unifier. + */ + + +} diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index aba3d3f6..dcffbee6 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -8,7 +8,11 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +//PL 18-02-05 Unifier durch Matcher ersetzt +//mus greater noch erstezt werden import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify; + +import de.dhbwstuttgart.typeinference.unify.Match; import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure; import de.dhbwstuttgart.typeinference.unify.interfaces.IUnify; @@ -46,6 +50,13 @@ public class FiniteClosure implements IFiniteClosure { if(pair.getPairOp() != PairOperator.SMALLER) continue; + //VERSUCH PL 18-02-06 + //koennte ggf. die FC reduzieren + //TO DO: Ueberpruefen, ob das sinnvll und korrekt ist + //if (!pair.getLhsType().getTypeParams().arePlaceholders() + // && !pair.getRhsType().getTypeParams().arePlaceholders()) + // continue; +; // Add nodes if not already in the graph if(!inheritanceGraph.containsKey(pair.getLhsType())) inheritanceGraph.put(pair.getLhsType(), new Node(pair.getLhsType())); @@ -93,7 +104,9 @@ public class FiniteClosure implements IFiniteClosure { private Set computeSmaller(Set types) { HashSet result = new HashSet<>(); - IUnify unify = new MartelliMontanariUnify(); + //PL 18-02-05 Unifier durch Matcher ersetzt + //IUnify unify = new MartelliMontanariUnify(); + Match match = new Match(); for(UnifyType t : types) { @@ -112,10 +125,16 @@ public class FiniteClosure implements IFiniteClosure { continue; // if T <* T' then sigma(T) <* sigma(T') - Set> candidates = strInheritanceGraph.get(t.getName()); + Set> candidates = strInheritanceGraph.get(t.getName()); //cadidates= [???Node(java.util.Vector>)??? + // , Node(java.util.Vector) + //] for(Node candidate : candidates) { UnifyType theta2 = candidate.getContent(); - Optional optSigma = unify.unify(theta2, t); + //PL 18-02-05 Unifier durch Matcher ersetzt ANFANG + ArrayList termList= new ArrayList(); + termList.add(new UnifyPair(theta2,t, PairOperator.EQUALSDOT)); + Optional optSigma = match.match(termList); + //PL 18-02-05 Unifier durch Matcher ersetzt ENDE if(!optSigma.isPresent()) continue; diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index c6aa9b3e..aa375c09 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -94,6 +94,7 @@ class Node { @Override public String toString() { - return "Node(" + content.toString() + ")\n"; + return "Elem: Node(" + content.toString() + ")\nPrec: " + getContentOfPredecessors().toString() + + "\nDesc: " + getContentOfDescendants().toString() + "\n\n"; } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java index dc90847b..3d5fc278 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/TypeParams.java @@ -98,12 +98,13 @@ public final class TypeParams implements Iterable{ */ public boolean occurs(PlaceholderType t) { for(UnifyType p : typeParams) - if(p instanceof PlaceholderType) + if(p instanceof PlaceholderType) {//PL 2018-01-31 dangeling else Problem { ... } eingefuegt. if(p.equals(t)) return true; - else + } + else { if(p.getTypeParams().occurs(t)) - return true; + return true; } return false; } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java index 2dd7c83f..061ba588 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Unifier.java @@ -66,6 +66,14 @@ public class Unifier implements Function, Iterable 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)); + ret.addUndConstraint(new Pair(testType, ASTFactory.createObjectType(), PairOperator.EQUALSDOT)); return ret; } } diff --git a/test/asp/UnifyWithoutWildcards.java b/test/asp/UnifyWithoutWildcards.java new file mode 100644 index 00000000..6f69d639 --- /dev/null +++ b/test/asp/UnifyWithoutWildcards.java @@ -0,0 +1,103 @@ +package asp; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.Ref; +import java.util.*; + +public class UnifyWithoutWildcards { + + public static final String tempDirectory = "/tmp/"; + + @Test + public void adapt() throws InterruptedException, IOException, ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); + RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken()); + RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); + testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); + ResultSet resultSet = run(testSet); + System.out.println(ResultSetPrinter.print(resultSet)); + assert resultSet.results.size() > 0; + } + + public ResultSet run(ConstraintSet toTest) throws IOException, InterruptedException, ClassNotFoundException { + String content = ""; + content = ASPFactory.generateASP(toTest, this.getFC()); + + 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"))); + String result = clingo.runClingo(); + ResultSet resultSet = ASPParser.parse(result, getInvolvedTPHS(toTest)); + return resultSet; + } + + private static class TPHExtractor implements TypeVisitor>{ + @Override + public List visit(RefType refType) { + ArrayList ret = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + ret.addAll(param.acceptTV(this)); + } + return ret; + } + + @Override + public List visit(SuperWildcardType superWildcardType) { + return superWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(TypePlaceholder typePlaceholder) { + return Arrays.asList(typePlaceholder); + } + + @Override + public List visit(ExtendsWildcardType extendsWildcardType) { + return extendsWildcardType.getInnerType().acceptTV(this); + } + + @Override + public List visit(GenericRefType genericRefType) { + return new ArrayList<>(); + } + } + + protected Collection getInvolvedTPHS(ConstraintSet toTest) { + List ret = new ArrayList<>(); + toTest.map((Pair p)-> { + ret.addAll(p.TA1.acceptTV(new TPHExtractor())); + ret.addAll(p.TA2.acceptTV(new TPHExtractor())); + return p; + }); + return ret; + } + + private Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createClass(Matrix.class)); + //ret.add(ASTFactory.createObjectClass()); + //ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } + private class Matrix extends HashMap>{} +} diff --git a/test/asp/gencay/GeneratorTest.java b/test/asp/gencay/GeneratorTest.java new file mode 100644 index 00000000..2c9fc46f --- /dev/null +++ b/test/asp/gencay/GeneratorTest.java @@ -0,0 +1,63 @@ +package asp.gencay; + +import asp.UnifyWithoutWildcards; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.sat.asp.parser.ASPParser; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; +import de.dhbwstuttgart.sat.asp.writer.ASPGencayFactory; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.syntaxtree.visual.ResultSetPrinter; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.result.ResultSet; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.*; +import java.util.concurrent.ArrayBlockingQueue; + +public class GeneratorTest extends UnifyWithoutWildcards{ + @Test + public void simple() throws ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + RefType t1 = new RefType(new JavaClassName("java.util.List"), list1, new NullToken()); + RefType t2 = new RefType(new JavaClassName("java.util.List"), list2, new NullToken()); + testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); + String resultSet = ASPGencayFactory.generateASP(testSet, + new HashSet<>(Arrays.asList(ASTFactory.createClass(List.class)))); + System.out.println(resultSet); + } + + @Test + public void matrix() throws ClassNotFoundException { + ConstraintSet testSet = new ConstraintSet<>(); + List list1 = Arrays.asList(TypePlaceholder.fresh(new NullToken())); + List list2 = Arrays.asList(TypePlaceholder.fresh(new NullToken()),TypePlaceholder.fresh(new NullToken())); + RefType t1 = new RefType(new JavaClassName("asp.UnifyWithoutWildcards$Matrix"), list1, new NullToken()); + RefType t2 = new RefType(new JavaClassName("java.util.HashMap"), list2, new NullToken()); + testSet.addUndConstraint(new Pair(t1, t2, PairOperator.SMALLERDOT)); + String resultSet = ASPGencayFactory.generateASP(testSet, this.getFC()); + System.out.println(resultSet); + } + + private Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createClass(Matrix.class)); + //ret.add(ASTFactory.createObjectClass()); + //ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } +} + +class Matrix extends Vector> {} \ No newline at end of file diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index b41d9405..632d288e 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.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPFactory; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; @@ -14,7 +14,9 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class ASPTest { @@ -44,17 +46,17 @@ public class ASPTest { //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); - List allClasses = new ArrayList<>(); + Set allClasses = new HashSet<>(); for(SourceFile sf : compiler.sourceFiles.values()) { - //allClasses.addAll(compiler.getAvailableClasses(sf)); + allClasses.addAll(compiler.getAvailableClasses(sf)); } for(SourceFile sf : compiler.sourceFiles.values()) { allClasses.addAll(sf.getClasses()); } final ConstraintSet cons = compiler.getConstraints(); - ASPGenerator generator = new ASPGenerator(cons, allClasses); - System.out.println(generator.getASP()); + String asp = ASPFactory.generateASP(cons, allClasses); + System.out.println(asp); } static String readFile(String path, Charset encoding) diff --git a/test/bytecode/ATest.java b/test/bytecode/ATest.java new file mode 100644 index 00000000..14dbacbf --- /dev/null +++ b/test/bytecode/ATest.java @@ -0,0 +1,8 @@ +package bytecode; + +public class ATest extends JavaTXCompilerTest { + public ATest() { + fileName = "Example"; + } + +} diff --git a/test/bytecode/AssignToLit.jav b/test/bytecode/AssignToLit.jav new file mode 100644 index 00000000..873828ed --- /dev/null +++ b/test/bytecode/AssignToLit.jav @@ -0,0 +1,30 @@ +import java.lang.Integer; +import java.lang.Boolean; +import java.lang.String; +import java.lang.Byte; +import java.lang.Short; +import java.lang.Long; +import java.lang.Float; +import java.lang.Double; +import java.lang.Character; + +class AssignToLit { + void m(){ +// String s = "Test"; +// Boolean b = false; +// Byte byte1 = 5; +// Byte byte2 = 55; +// Short short1 = 5; +// Short short2 = 55; +// Integer int1 = 5; +// Integer int2 = 8888888; +// Long long1 = 1; +// Long long2 = 5; +// Long long3 = 89989898; +// Float float1 = 1; +// Float float2 = 55; +// Double d1 = 1; +// Double d2 = 55; + Character c = 'A'; + } +} \ No newline at end of file diff --git a/test/bytecode/AssignToLitTest.java b/test/bytecode/AssignToLitTest.java new file mode 100644 index 00000000..dc5d397b --- /dev/null +++ b/test/bytecode/AssignToLitTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class AssignToLitTest extends JavaTXCompilerTest { + public AssignToLitTest() { + this.fileName = "AssignToLit"; + } +} diff --git a/test/bytecode/DuMethod.jav b/test/bytecode/DuMethod.jav index b56f6c55..3898a7ba 100644 --- a/test/bytecode/DuMethod.jav +++ b/test/bytecode/DuMethod.jav @@ -1,7 +1,7 @@ public class DuMethod{ method(a){ - return a; + return a+a; } method(a){ diff --git a/test/bytecode/DuMethodTest.java b/test/bytecode/DuMethodTest.java new file mode 100644 index 00000000..a4c8a226 --- /dev/null +++ b/test/bytecode/DuMethodTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class DuMethodTest extends JavaTXCompilerTest{ + public DuMethodTest() { + this.fileName = "DuMethod"; + } +} diff --git a/test/bytecode/Example.jav b/test/bytecode/Example.jav new file mode 100644 index 00000000..b7455a3f --- /dev/null +++ b/test/bytecode/Example.jav @@ -0,0 +1,9 @@ +import java.lang.String; + +public class Example { + + public m() { + String x = "X"; + return x; + } +} \ No newline at end of file diff --git a/test/bytecode/Exceptions.jav b/test/bytecode/Exceptions.jav new file mode 100644 index 00000000..9903511c --- /dev/null +++ b/test/bytecode/Exceptions.jav @@ -0,0 +1,3 @@ +public class Exceptions { +// m(Integer i) throws +} \ No newline at end of file diff --git a/test/bytecode/For.jav b/test/bytecode/For.jav new file mode 100644 index 00000000..c4bd6677 --- /dev/null +++ b/test/bytecode/For.jav @@ -0,0 +1,30 @@ +import java.lang.Integer; +import java.lang.Boolean; + +class For{ + Integer m(Integer x){ + var c = x + 2; +// Boolean b = true; +// c = 5; +// c++; +// ++c; +// c--; +// --c; +// while(x<2){ +// x = x +1; +// b = false; +// } + return c; +// for(int i = 0;i<10;i++) { +// x = x + 5; +// } + } + +// m2(Integer x){ +// if(x<2) { +// return 1; +// }else { +// return 2; +// } +// } +} \ No newline at end of file diff --git a/test/bytecode/ForTest.java b/test/bytecode/ForTest.java new file mode 100644 index 00000000..c95138d3 --- /dev/null +++ b/test/bytecode/ForTest.java @@ -0,0 +1,11 @@ +package bytecode; + +import org.objectweb.asm.Opcodes; + +public class ForTest extends JavaTXCompilerTest { + + public ForTest() { + this.fileName = "For"; + } + +} diff --git a/test/bytecode/Import.jav b/test/bytecode/Import.jav new file mode 100644 index 00000000..c658568f --- /dev/null +++ b/test/bytecode/Import.jav @@ -0,0 +1,8 @@ +import java.util.Vector; + +class Import { + void methode(){ + Vector v = new Vector<>(); + v.add("X"); + } +} \ No newline at end of file diff --git a/test/bytecode/ImportTest.java b/test/bytecode/ImportTest.java new file mode 100644 index 00000000..b7244e15 --- /dev/null +++ b/test/bytecode/ImportTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class ImportTest extends JavaTXCompilerTest{ + public ImportTest() { + this.fileName = "Import"; + } +} diff --git a/test/bytecode/JavaTXCompilerTest.java b/test/bytecode/JavaTXCompilerTest.java index f017e23e..623fc96e 100644 --- a/test/bytecode/JavaTXCompilerTest.java +++ b/test/bytecode/JavaTXCompilerTest.java @@ -34,17 +34,16 @@ public class JavaTXCompilerTest { filesToTest.add(new File(rootDirectory+fileName+".jav")); System.out.println(rootDirectory+fileName+".jav"); JavaTXCompiler compiler = new JavaTXCompiler(filesToTest); - System.out.println("test"); for(File f : filesToTest){ String content = readFile(f.getPath(), StandardCharsets.UTF_8); List typeinferenceResult = compiler.typeInference(); HashMap bytecode = this.getBytecode(compiler.sourceFiles.get(f), typeinferenceResult.get(0)); - for(ResultPair ep : typeinferenceResult.get(0).results) { - System.out.println(ep.getLeft() + " ->" + ep.getRight()); - } +// for(ResultPair ep : typeinferenceResult.get(0).results) { +// System.out.println(ep.getLeft() + " ->" + ep.getRight()); +// } - String name = ""; + String name; int pos = f.getName().lastIndexOf("."); if(pos != -1) { name = f.getName().substring(0, pos); @@ -67,7 +66,7 @@ public class JavaTXCompilerTest { for(String name : classFiles.keySet()) { byte[] bytecode = classFiles.get(name); try { - System.out.println("generating"+name+ ".class file"); + 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(); diff --git a/test/bytecode/LamAssign.jav b/test/bytecode/LamAssign.jav index 1e0ee3d8..82bb31b1 100644 --- a/test/bytecode/LamAssign.jav +++ b/test/bytecode/LamAssign.jav @@ -11,5 +11,5 @@ class LamAssign { } interface Fun1{ - A apply(B b); -} + public A apply(B b); +} \ No newline at end of file diff --git a/test/bytecode/Methods.jav b/test/bytecode/Methods.jav new file mode 100644 index 00000000..3c46739d --- /dev/null +++ b/test/bytecode/Methods.jav @@ -0,0 +1,14 @@ +import java.lang.Integer; + +class Methods { + + m(a,b){ + var c=a+b; + return c; + } + + method2(x){ + Integer i = this.m(x,2); + return i; + } +} \ No newline at end of file diff --git a/test/bytecode/MethodsTest.java b/test/bytecode/MethodsTest.java new file mode 100644 index 00000000..11195b71 --- /dev/null +++ b/test/bytecode/MethodsTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class MethodsTest extends JavaTXCompilerTest { + public MethodsTest() { + this.fileName = "Methods"; + } +} diff --git a/test/bytecode/Op.jav b/test/bytecode/Op.jav new file mode 100644 index 00000000..9c05b0a2 --- /dev/null +++ b/test/bytecode/Op.jav @@ -0,0 +1,12 @@ +import java.lang.Integer; + +class Op { + m(Integer a, Integer b) { + Integer c = a+b; +// d = a-b; +// e = a*b; +// f = a/b; + + return c; + } +} \ No newline at end of file diff --git a/test/bytecode/OpTest.java b/test/bytecode/OpTest.java new file mode 100644 index 00000000..cd8fee06 --- /dev/null +++ b/test/bytecode/OpTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class OpTest extends JavaTXCompilerTest { + public OpTest() { + this.fileName = "Op"; + } +} diff --git a/test/bytecode/OverlaodGen.jav b/test/bytecode/OverlaodGen.jav new file mode 100644 index 00000000..d5946496 --- /dev/null +++ b/test/bytecode/OverlaodGen.jav @@ -0,0 +1,11 @@ +import java.util.Vector; + +class OverlaodGen { + void method(Vector v) { +// Integer i = v.get(0); + } + + void method(Vector v) { +// String s = v.get(0); + } +} \ No newline at end of file diff --git a/test/bytecode/OverlaodGenTest.java b/test/bytecode/OverlaodGenTest.java new file mode 100644 index 00000000..72bde4f9 --- /dev/null +++ b/test/bytecode/OverlaodGenTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class OverlaodGenTest extends JavaTXCompilerTest { + public OverlaodGenTest() { + this.fileName = "OverlaodGen"; + } +} diff --git a/test/bytecode/TestIfTest.java b/test/bytecode/TestIfTest.java new file mode 100644 index 00000000..804a0616 --- /dev/null +++ b/test/bytecode/TestIfTest.java @@ -0,0 +1,7 @@ +package bytecode; + +public class TestIfTest extends JavaTXCompilerTest{ + public TestIfTest() { + this.fileName = "IfTest"; + } +} diff --git a/test/finiteClosure/SuperInterfacesTest.java b/test/finiteClosure/SuperInterfacesTest.java new file mode 100644 index 00000000..0e1368c3 --- /dev/null +++ b/test/finiteClosure/SuperInterfacesTest.java @@ -0,0 +1,46 @@ +package finiteClosure; + +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class SuperInterfacesTest { + @Test + public void test() throws ClassNotFoundException { + Collection classes = new ArrayList<>(); + classes.add(ASTFactory.createClass(TestClass.class)); + System.out.println(FCGenerator.toFC(classes)); + } + + @Test + public void testGeneric() throws ClassNotFoundException { + Collection classes = new ArrayList<>(); + classes.add(ASTFactory.createClass(TestClassGeneric.class)); + System.out.println(FCGenerator.toFC(classes)); + } +} + +class TestClass implements Test2, Test3{ + +} + +class TestClassGeneric implements Test4{ + +} + +interface Test2 { + +} + +interface Test3{ + +} + +interface Test4{ + +} \ No newline at end of file diff --git a/test/javFiles/AddLong.jav b/test/javFiles/AddLong.jav new file mode 100644 index 00000000..ceb31228 --- /dev/null +++ b/test/javFiles/AddLong.jav @@ -0,0 +1,9 @@ +import java.lang.Integer; +import java.lang.Long; + +public class AddLong{ + add(Integer a, Long b) { + Long c = a+b; + return c; + } +} \ No newline at end of file diff --git a/test/javFiles/Expressions.jav b/test/javFiles/Expressions.jav new file mode 100644 index 00000000..e2e992a1 --- /dev/null +++ b/test/javFiles/Expressions.jav @@ -0,0 +1,8 @@ +class Expressions{ + +void test(){ + var x = 2; + x = x + 2; +} + +} \ No newline at end of file diff --git a/test/javFiles/Faculty.jav b/test/javFiles/Faculty.jav index 50bd7e78..ca539add 100644 --- a/test/javFiles/Faculty.jav +++ b/test/javFiles/Faculty.jav @@ -12,4 +12,4 @@ class Faculty { }; return fact; } -} +} \ No newline at end of file diff --git a/test/javFiles/FacultyTyped.jav b/test/javFiles/FacultyTyped.jav new file mode 100644 index 00000000..089d2f33 --- /dev/null +++ b/test/javFiles/FacultyTyped.jav @@ -0,0 +1,19 @@ +import java.lang.Integer; + +class Faculty { + + Integer mul(Integer x, Integer y) { + return x; + } + + Fun1 m () { + var fact = (Integer x) -> { + return mul(x, fact.apply(x)); + }; + return fact; + } +} + +interface Fun1{ + B apply(A a); +} \ No newline at end of file diff --git a/test/javFiles/LambdaRunnable.jav b/test/javFiles/LambdaRunnable.jav index 982680c3..37924ea2 100644 --- a/test/javFiles/LambdaRunnable.jav +++ b/test/javFiles/LambdaRunnable.jav @@ -1,3 +1,6 @@ +import java.lang.Runnable; +import java.lang.String; +import java.lang.System; public class LamRunnable{ diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e1183535..aa8966c9 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -6,21 +6,21 @@ class Matrix extends Vector> { mul(m) { var ret = new Matrix(); var i = 0; - while(i < size()) { + //while(i < size()) { var v1 = this.elementAt(i); - var v2 = new Vector(); - 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++; } + //var v2 = new Vector(); + //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; } } diff --git a/test/javFiles/MethodCallGenerics.jav b/test/javFiles/MethodCallGenerics.jav new file mode 100644 index 00000000..0d02509b --- /dev/null +++ b/test/javFiles/MethodCallGenerics.jav @@ -0,0 +1,14 @@ +import java.lang.String; + +class Generics { + // A mt1(A a, B b){ + B mt1(B a, B b){ + return mt1(a, a); + } +} + +class Test { + methode(String s){ + return new Generics().mt1(s,s); + } +} diff --git a/test/javFiles/Package.jav b/test/javFiles/Package.jav new file mode 100644 index 00000000..bbc1e51d --- /dev/null +++ b/test/javFiles/Package.jav @@ -0,0 +1,5 @@ +package strucType.input; + +class Neu +{ +} \ No newline at end of file diff --git a/test/javFiles/Vector.jav b/test/javFiles/Vector.jav index f2a9cdae..40072fe4 100644 --- a/test/javFiles/Vector.jav +++ b/test/javFiles/Vector.jav @@ -1,11 +1,8 @@ +import java.util.Vector; -class Vector{ +class MyVector{ id(x){ - return id2(x); -} - -id2(x){ -return id(x); + return (x.elementAt(0)); } } \ No newline at end of file diff --git a/test/parser/GeneralParserTest.java b/test/parser/GeneralParserTest.java index c48e1320..b218347f 100644 --- a/test/parser/GeneralParserTest.java +++ b/test/parser/GeneralParserTest.java @@ -41,7 +41,8 @@ public class GeneralParserTest{ filenames.add("FieldVarTest.jav"); filenames.add("StructuralTypes.jav"); */ - filenames.add("ExtendsTest.jav"); +// filenames.add("ExtendsTest.jav"); + filenames.add("OpratorTest.jav"); try{ new JavaTXCompiler(filenames.stream().map(s -> new File(rootDirectory + s)).collect(Collectors.toList())); }catch(Exception exc){ diff --git a/test/parser/OpratorTest.jav b/test/parser/OpratorTest.jav new file mode 100644 index 00000000..2764adf4 --- /dev/null +++ b/test/parser/OpratorTest.jav @@ -0,0 +1,12 @@ +import java.lang.Integer; + +class OpratorTest { + m(Integer a, Integer b) { + c = a+b; +// d = a-b; +// e = a*b; +// f = a/b; + + return c; + } +} \ No newline at end of file diff --git a/test/typeinference/FacultyTest.java b/test/typeinference/FacultyTest.java deleted file mode 100644 index aadaf6e7..00000000 --- a/test/typeinference/FacultyTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class FacultyTest extends JavaTXCompilerTest{ - public FacultyTest() { - this.fileToTest = new File(rootDirectory+"Faculty.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/FiniteClosureTest.java b/test/typeinference/FiniteClosureTest.java deleted file mode 100644 index 8b62e4e5..00000000 --- a/test/typeinference/FiniteClosureTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class FiniteClosureTest extends JavaTXCompilerTest{ - public FiniteClosureTest() { -// this.fileToTest = new File(rootDirectory+"fc.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/GenericsTest.java b/test/typeinference/GenericsTest.java deleted file mode 100644 index abcef013..00000000 --- a/test/typeinference/GenericsTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package typeinference; - -import java.io.File; - -//TODO: Hier gibt es einen Fehler. Das erstellte ConstraintSet stimmt nicht -public class GenericsTest extends JavaTXCompilerTest{ - public GenericsTest() { - this.fileToTest = new File(rootDirectory+"Generics.jav"); - } -} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 03452018..ba004cb5 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -23,19 +23,74 @@ import java.util.Set; public class JavaTXCompilerTest { public static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; - private static final List filesToTest = new ArrayList<>(); - protected File fileToTest = null; - public JavaTXCompilerTest(){ + @Test + public void finiteClosure() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"fc.jav")); } @Test - public void test() throws IOException, ClassNotFoundException { - if(fileToTest != null)filesToTest.add(fileToTest); - else return; - //filesToTest.add(new File(rootDirectory+"Faculty.jav")); - //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); - //filesToTest.add(new File(rootDirectory+"test.jav")); - filesToTest.add(new File(rootDirectory+"EmptyMethod.jav")); + public void lambda() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Lambda.jav")); + } + @Test + public void lambda2() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Lambda2.jav")); + } + @Test + public void lambda3() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Lambda3.jav")); + } + @Test + public void mathStruc() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"mathStruc.jav")); + } + @Test + public void generics() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Generics.jav")); + } + @Test + public void genericsMethodCall() throws IOException, ClassNotFoundException { + TestResultSet result = execute(new File(rootDirectory+"MethodCallGenerics.jav")); + //TODO: Hier sollte der Rückgabetyp der Methode String sein + } + @Test + public void faculty() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Faculty.jav")); + } + @Test + public void facultyTyped() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"FacultyTyped.jav")); + } + @Test + public void matrix() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Matrix.jav")); + } + @Test + public void packageTests() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Package.jav")); + } + @Test + public void vector() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Vector.jav")); + } + @Test + public void lambdaRunnable() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"LambdaRunnable.jav")); + } + @Test + public void expressions() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"Expressions.jav")); + } + @Test + public void addLong() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"AddLong.jav")); + } + + private static class TestResultSet{ + + } + + public TestResultSet execute(File fileToTest) throws IOException, ClassNotFoundException { //filesToTest.add(new File(rootDirectory+"fc.jav")); //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); @@ -45,6 +100,9 @@ public class JavaTXCompilerTest { //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); + // //filesToTest.add(new File(rootDirectory+"Faculty.jav")); + // //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); + // //filesToTest.add(new File(rootDirectory+"test.jav")); JavaTXCompiler compiler = new JavaTXCompiler(fileToTest); List results = compiler.typeInference(); @@ -68,6 +126,7 @@ public class JavaTXCompilerTest { System.out.println(s); } } + return new TestResultSet(); } static String readFile(String path, Charset encoding) diff --git a/test/typeinference/MatrixTest.java b/test/typeinference/MatrixTest.java deleted file mode 100644 index 8cc587c9..00000000 --- a/test/typeinference/MatrixTest.java +++ /dev/null @@ -1,9 +0,0 @@ -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 diff --git a/test/typeinference/VectorTest.java b/test/typeinference/VectorTest.java deleted file mode 100644 index 864535b3..00000000 --- a/test/typeinference/VectorTest.java +++ /dev/null @@ -1,9 +0,0 @@ -package typeinference; - -import java.io.File; - -public class VectorTest extends JavaTXCompilerTest{ - public VectorTest() { - this.fileToTest = new File(rootDirectory+"Vector.jav"); - } -} \ No newline at end of file diff --git a/test/visualisation/ToString.java b/test/visualisation/ToString.java new file mode 100644 index 00000000..00b8ddea --- /dev/null +++ b/test/visualisation/ToString.java @@ -0,0 +1,5 @@ +package visualisation; + +public class ToString { + +}