diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html index 274fb17d..7ddcc746 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html index d59ab82b..1eb88e1d 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/NotExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html index 6dfdb37b..e02874de 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html index 86c5a8fa..9c293c66 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html index 94b06b12..4d52f399 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html index 47989bf2..3027535e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html index 437a143f..e2caa4bd 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.html @@ -115,7 +115,7 @@ var activeTableTab = "activeTableTab";
  • de.dhbwstuttgart.syntaxtree.statement.UnaryExpr
  • diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html index beb55c1f..6e30f09e 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NegativeExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.NegativeExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.NegativeExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.NegativeExpr
    diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NotExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NotExpr.html index 0ebf9584..27253c0f 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NotExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/NotExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.NotExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.NotExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.NotExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.NotExpr
    diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PositivExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PositivExpr.html index 6694e69d..91ed2bf9 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PositivExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PositivExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.PositivExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.PositivExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PositivExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PositivExpr
    diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostDecExpr.html index d6af3f57..317627d3 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostDecExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.PostDecExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostDecExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PostDecExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostDecExpr
    diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostIncExpr.html index 73f60a52..8d6cfd7c 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PostIncExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.PostIncExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostIncExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PostIncExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PostIncExpr
    diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreDecExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreDecExpr.html index d6106964..3fccd8b6 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreDecExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreDecExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.PreDecExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreDecExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PreDecExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreDecExpr
    diff --git a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreIncExpr.html b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreIncExpr.html index 310db4d9..005ea2e5 100644 --- a/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreIncExpr.html +++ b/doc/LambdaJavadoc/de/dhbwstuttgart/syntaxtree/statement/class-use/PreIncExpr.html @@ -3,7 +3,7 @@ -Uses of Class de.dhbwstuttgart.syntaxtree.statement.PreIncExpr +Uses of Class de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreIncExpr @@ -70,9 +70,9 @@
    -

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

    +

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

    -
    No usage of de.dhbwstuttgart.syntaxtree.statement.PreIncExpr
    +
    No usage of de.dhbwstuttgart.syntaxtree.statement.javaInternal.PreIncExpr
    diff --git a/pom.xml b/pom.xml index 6b0f2fba..9cce2d99 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,11 @@ antlr4 4.7 + + commons-io + commons-io + 2.6 + com.google.guava guava diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java index 25c06024..d4fee65f 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGen.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGen.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.bytecode; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.FieldVisitor; @@ -371,12 +372,6 @@ public class BytecodeGen implements ASTVisitor { } - @Override - public void visit(UnaryPlus unaryPlus) { - // TODO Auto-generated method stub - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub @@ -442,4 +437,9 @@ public class BytecodeGen implements ASTVisitor { } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + } diff --git a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java index c19315e8..65834125 100644 --- a/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java +++ b/src/de/dhbwstuttgart/bytecode/BytecodeGenMethod.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Handle; @@ -383,6 +384,11 @@ public class BytecodeGenMethod implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -412,11 +418,6 @@ public class BytecodeGenMethod implements StatementVisitor{ mv.visitVarInsn(Opcodes.ALOAD, 0); } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java index 36a28ce6..5264888c 100644 --- a/src/de/dhbwstuttgart/bytecode/KindOfLambda.java +++ b/src/de/dhbwstuttgart/bytecode/KindOfLambda.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.bytecode; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.ArrayList; @@ -126,6 +127,11 @@ public class KindOfLambda implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + throw new NotImplementedException(); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -155,12 +161,6 @@ public class KindOfLambda implements StatementVisitor{ this.argumentList.add(aThis.getType()); } - @Override - public void visit(UnaryPlus unaryPlus) { - // TODO Auto-generated method stub - - } - @Override public void visit(WhileStmt whileStmt) { // TODO Auto-generated method stub diff --git a/src/de/dhbwstuttgart/bytecode/NormalMethod.java b/src/de/dhbwstuttgart/bytecode/NormalMethod.java index b7320479..5afc31fb 100644 --- a/src/de/dhbwstuttgart/bytecode/NormalMethod.java +++ b/src/de/dhbwstuttgart/bytecode/NormalMethod.java @@ -43,7 +43,7 @@ public class NormalMethod { } public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return method.getType(); + return method.getReturnType(); } public boolean hasGen() { diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 2abd8b9d..86666a49 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -89,6 +89,7 @@ public class JavaTXCompiler { final ConstraintSet cons = getConstraints(); FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); + System.out.println(finiteClosure); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java new file mode 100644 index 00000000..7863c8ed --- /dev/null +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/FCGenerator.java @@ -0,0 +1,150 @@ +package de.dhbwstuttgart.parser.SyntaxTreeGenerator; + +import de.dhbwstuttgart.exceptions.DebugException; +import de.dhbwstuttgart.exceptions.NotImplementedException; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.GenericTypeVar; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.*; + +import java.util.*; + +public class FCGenerator { + /** + * Baut die FiniteClosure aus availableClasses. + * Klassen welche nicht in availableClasses vorkommen werden im Java Classpath nachgeschlagen. + * + * @param availableClasses - Alle geparsten Klassen + */ + public static Set toFC(Collection availableClasses) throws ClassNotFoundException { + HashSet pairs = new HashSet<>(); + for(ClassOrInterface cly : availableClasses){ + pairs.addAll(getSuperTypes(cly, availableClasses)); + } + System.out.println(pairs); + return pairs; + } + + /** + * Bildet eine Kette vom übergebenen Typ bis hin zum höchsten bekannten Typ + * Als Generics werden TPHs benutzt, welche der Unifikationsalgorithmus korrekt interpretieren muss. + * Die verwendeten TPHs werden in der Kette nach oben gereicht, so erhält der selbe GTV immer den selben TPH + * @param forType + * @return + */ + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses) throws ClassNotFoundException { + return getSuperTypes(forType, availableClasses, new HashMap<>()); + } + + //TODO: implements Interface auch als superklassen beachten + private static List getSuperTypes(ClassOrInterface forType, Collection availableClasses, HashMap gtvs) throws ClassNotFoundException { + List params = new ArrayList<>(); + //Die GTVs, die in forType hinzukommen: + HashMap newGTVs = new HashMap<>(); + //Generics mit gleichem Namen müssen den selben TPH bekommen + for(GenericTypeVar gtv : forType.getGenerics()){ + if(!gtvs.containsKey(gtv.getParsedName())){ + gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + newGTVs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); + } + params.add(gtvs.get(gtv.getParsedName())); + } + + Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); + ClassOrInterface superClass; + if(!hasSuperclass.isPresent()) //Wenn es die Klasse in den available Klasses nicht gibt wird sie im Classpath gesucht. Ansonsten Exception + { + superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(forType.getSuperClass().getName().toString())); + }else{ + superClass = hasSuperclass.get(); + } + /* + Die Parameter der superklasse müssen jetzt nach den Angaben in der Subklasse + modifiziert werden + Beispie: Matrix extends Vector> + Den ersten Parameter mit Vector austauschen und dort alle Generics zu den Typplaceholdern in gtvs austauschen + */ + //Hier vermerken, welche Typen im der Superklasse ausgetauscht werden müssen + Iterator itGenParams = superClass.getGenerics().iterator(); + Iterator itSetParams = forType.getSuperClass().getParaList().iterator(); + while(itGenParams.hasNext()){ + RefTypeOrTPHOrWildcardOrGeneric setType = itSetParams.next(); + //In diesem Typ die GTVs durch TPHs und Einsetzungen austauschen: + UnifyType setSetType = setType.acceptTV(new TypeExchanger(gtvs)); + newGTVs.put(itGenParams.next().getParsedName(), setSetType); + } + + UnifyType superType = forType.getSuperClass().acceptTV(new TypeExchanger(newGTVs)); + + TypeParams paramList = new TypeParams(params); + UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); + UnifyType t2 = superType; + + UnifyPair ret = UnifyTypeFactory.generateSmallerPair(t1, t2); + + List superTypes; + //Rekursiver Aufruf. Abbruchbedingung ist Object als Superklasse: + if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ + superTypes = Arrays.asList(UnifyTypeFactory.generateSmallerPair(UnifyTypeFactory.convert(ASTFactory.createObjectType()), UnifyTypeFactory.convert(ASTFactory.createObjectType()))); + }else{ + superTypes = getSuperTypes(superClass, availableClasses, newGTVs); + } + + List retList = new ArrayList<>(); + retList.add(ret); + retList.addAll(superTypes); + + return retList; + } + + /** + * Tauscht die GTVs in einem Typ gegen die entsprechenden Typen in der übergebenen Map aus. + */ + private static class TypeExchanger implements TypeVisitor{ + + private final HashMap gtvs; + + TypeExchanger(HashMap gtvs){ + this.gtvs = gtvs; + } + + @Override + public UnifyType visit(RefType refType) { + List params = new ArrayList<>(); + for(RefTypeOrTPHOrWildcardOrGeneric param : refType.getParaList()){ + params.add(param.acceptTV(this)); + } + TypeParams paramList = new TypeParams(params); + UnifyType ret = new ReferenceType(refType.getName().toString(), paramList); + return ret; + } + + @Override + public UnifyType visit(SuperWildcardType superWildcardType) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(TypePlaceholder typePlaceholder) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(ExtendsWildcardType extendsWildcardType) { + throw new DebugException("Dieser Fall darf nicht auftreten"); + } + + @Override + public UnifyType visit(GenericRefType genericRefType) { + if(! gtvs.containsKey(genericRefType.getParsedName())) + throw new DebugException("Dieser Fall darf nicht auftreten"); + //TODO: Diesen Dirty-Hack beseitigen. Fehler tritt bei java.lang.invoke.LambdaFormEditor$Transform$Kind auf. + //return UnifyTypeFactory.convert(ASTFactory.createObjectType()); + return gtvs.get(genericRefType.getParsedName()); + } + + } +} diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index f4ada6b2..069f49dc 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -16,6 +16,7 @@ import de.dhbwstuttgart.syntaxtree.type.Void; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; +import java.lang.reflect.Type; import java.util.*; public class StatementGenerator { @@ -95,7 +96,7 @@ public class StatementGenerator { private Statement convert(Java8Parser.StatementWithoutTrailingSubstatementContext stmt) { if(stmt.block() != null){ - return convert(stmt.block()); + return convert(stmt.block(), false); }else if(stmt.emptyStatement() != null){ return new EmptyStmt(stmt.getStart()); }else if(stmt.expressionStatement() != null){ @@ -121,14 +122,14 @@ public class StatementGenerator { }else throw new NotImplementedException(); } - public Block convert(Java8Parser.BlockContext block) { + public Block convert(Java8Parser.BlockContext block, boolean addTrailingReturn) { List statements = new ArrayList<>(); if(block.blockStatements() != null) for(Java8Parser.BlockStatementContext statementContext : block.blockStatements().blockStatement()){ List stmt = convert(statementContext); statements.addAll(stmt); } - statements = SyntacticSugar.addTrailingReturn(statements); + if(addTrailingReturn)statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } @@ -289,8 +290,8 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), + TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { @@ -316,8 +317,10 @@ public class StatementGenerator { } private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement thenBlock = convert(stmt.statementNoShortIf()); + Statement elseBlock = convert(stmt.statement()); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()), expr, thenBlock, elseBlock, stmt.getStart()); } private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ @@ -591,7 +594,7 @@ public class StatementGenerator { if(expression.relationalExpression() == null){ return convert(expression.shiftExpression()); }else{ - throw new NotImplementedException(); + return convert(expression.relationalExpression()); } } @@ -604,7 +607,9 @@ public class StatementGenerator { } private Expression convert(Java8Parser.AdditiveExpressionContext expression) { - if(expression.additiveExpression() == null){ + if(expression.additiveExpression() != null){ + return convert(expression.additiveExpression()); + }else if(expression.multiplicativeExpression() != null){ return convert(expression.multiplicativeExpression()); }else{ throw new NotImplementedException(); @@ -627,9 +632,11 @@ public class StatementGenerator { }else if(expression.unaryExpressionNotPlusMinus() != null){ return convert(expression.unaryExpressionNotPlusMinus()); }else if(expression.getText().startsWith("+")){ - return new UnaryPlus(convert(expression.unaryExpression())); + return new UnaryExpr(UnaryExpr.Operation.PLUS, convert(expression.unaryExpression()), + TypePlaceholder.fresh(expression.getStart()), expression.getStart()); }else if(expression.getText().startsWith("-")){ - return new UnaryMinus(convert(expression.unaryExpression())); + return new UnaryExpr(UnaryExpr.Operation.MINUS, convert(expression.unaryExpression()), + TypePlaceholder.fresh(expression.getStart()), expression.getStart()); }else{ //Diese Exceptions sollte nie geworfen werden. //Der Code wurde nur noch nicht getestet. Sollte zur Sicherheit drin bleiben. @@ -672,10 +679,10 @@ public class StatementGenerator { } for(Java8Parser.PostIncrementExpression_lf_postfixExpressionContext inc : expression.postIncrementExpression_lf_postfixExpression()){ - expr = new PostIncExpr(expr); + expr = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, expr, TypePlaceholder.fresh(inc.getStart()), inc.getStart()); } for(Java8Parser.PostDecrementExpression_lf_postfixExpressionContext dec : expression.postDecrementExpression_lf_postfixExpression()){ - expr = new PostDecExpr(expr); + expr = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, expr, TypePlaceholder.fresh(dec.getStart()), dec.getStart()); } return expr; @@ -850,7 +857,7 @@ public class StatementGenerator { expression.lambdaBody().expression().getStart())); block = new Block(statements, expression.lambdaBody().getStart()); }else{ - block = lambdaGenerator.convert(expression.lambdaBody().block()); + block = lambdaGenerator.convert(expression.lambdaBody().block(), true); } List funNParams = new ArrayList<>(); funNParams.add(TypePlaceholder.fresh(expression.getStart()));//ret-Type diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java index d758f52b..d7926004 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -1,6 +1,9 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.AbstractASTWalker; +import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.List; @@ -10,22 +13,27 @@ public class SyntacticSugar { public static List addTrailingReturn(List statements){ if(statements.size()!=0) { Statement lastStmt = statements.get(statements.size() - 1); - if (lastStmt instanceof Return) return statements; - if (lastStmt instanceof WhileStmt) { - //TODO - //if (hasReturn(((WhileStmt) lastStmt).loopBlock)) return statements; - } else if (lastStmt instanceof IfStmt) { - if (hasReturn(((IfStmt) lastStmt).then_block) - && hasReturn(((IfStmt) lastStmt).else_block)) return statements; - } else if (lastStmt instanceof ForStmt) { - if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements; - } else { - } + ReturnFinder hasReturn = new ReturnFinder(); + lastStmt.accept(hasReturn); + if(hasReturn.hasReturn)return statements; } statements.add(new ReturnVoid(new NullToken())); return statements; } + private static class ReturnFinder extends AbstractASTWalker{ + public boolean hasReturn = false; + @Override + public void visit(Return aReturn) { + hasReturn = true; + } + + @Override + public void visit(ReturnVoid aReturn) { + hasReturn = true; + } + } + private static boolean hasReturn(Block block){ for(Statement s : block.getStatements()) if(s instanceof Return)return true; diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index c5b135e6..b681d9c3 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -136,12 +136,12 @@ public class SyntaxTreeGenerator{ //TODO: Error! Abstrakte Methode ohne abstrakt Keyword } }else{ - block = stmtGen.convert(body.block()); + block = stmtGen.convert(body.block(),true); } if(parentClass.equals(new JavaClassName(name))){ - return new Constructor(modifiers, name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); + return new Constructor(modifiers, name, retType, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); }else{ - return new Method(modifiers, name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); + return new Method(modifiers, name, retType, parameterList,block, gtvDeclarations, header.getStart()); } } @@ -221,10 +221,9 @@ public class SyntaxTreeGenerator{ */ private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); - int modifiers = 0; ParameterList params = new ParameterList(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset); - return new Constructor(Modifier.PUBLIC, className, classType, modifiers, params, block, classGenerics, offset, fieldInitializations); + return new Constructor(Modifier.PUBLIC, className, classType, params, block, classGenerics, offset, fieldInitializations); } private RefType convert(Java8Parser.SuperclassContext superclass) { diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index d54b682a..1dd259d0 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -40,7 +40,12 @@ public class TypeGenerator { public static RefTypeOrTPHOrWildcardOrGeneric convert(Java8Parser.UnannTypeContext unannTypeContext, JavaClassRegistry reg, GenericsRegistry genericsRegistry) { if(unannTypeContext.unannPrimitiveType()!=null){ - throw new NotImplementedException(); + if(unannTypeContext.unannPrimitiveType().getText().equals("boolean")){ + return new RefType(ASTFactory.createClass(Boolean.class).getClassName(), unannTypeContext.getStart()); + }else{ + Java8Parser.NumericTypeContext numericType = unannTypeContext.unannPrimitiveType().numericType(); + throw new NotImplementedException(); + } }else if(unannTypeContext.unannReferenceType().unannArrayType()!=null){ //System.out.println(unannTypeContext.getText()); diff --git a/src/de/dhbwstuttgart/sat/asp/Clingo.java b/src/de/dhbwstuttgart/sat/asp/Clingo.java new file mode 100644 index 00000000..85f6dc56 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/Clingo.java @@ -0,0 +1,46 @@ +package de.dhbwstuttgart.sat.asp; + + +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Clingo { + private final List input; + private static final List programFiles = new ArrayList<>(); + static{ + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/basis.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/subst.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce1.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/reduce2.lp")); + programFiles.add(new File("/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards/unify.lp")); + } + + public Clingo(List inputFiles){ + this.input = inputFiles; + } + + public String runClingo() throws IOException, InterruptedException { + String pathToClingo = + "/home/janulrich/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/clingo-5.2.1-linux-x86_64/clingo"; + List commands = new ArrayList<>(); + commands.add(pathToClingo); + commands.add("--outf=2"); //use JSON-Output + for(File file : input){ + commands.add(file.getPath()); + } + commands.addAll(programFiles.stream().map(f->f.getPath()).collect(Collectors.toList())); + Process clingo = new ProcessBuilder( commands.toArray(new String[0])).start(); + InputStream output = clingo.getInputStream(); + clingo.waitFor(); + String result = IOUtils.toString(output, StandardCharsets.UTF_8); + return result; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java new file mode 100644 index 00000000..41237547 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/model/ASPRule.java @@ -0,0 +1,24 @@ +package de.dhbwstuttgart.sat.asp.model; + +public enum ASPRule { + ASP_GENERIC_TYPE_NAME("genericType"), + ASP_PAIR_EQUALS_NAME("equals"), + ASP_PAIR_SMALLER_NAME("smaller"), + ASP_PAIR_SMALLER_DOT_NAME("smallerDot"), + ASP_PARAMLIST_NAME("param"), + ASP_PARAMLISTNUMERATION_NAME("paramNum"), + ASP_PARAMLIST_END_POINTER("null"), + ASP_TYPE("type") + ; + + private final String text; + + private ASPRule(final String text) { + this.text = text; + } + + @Override + public String toString() { + return text; + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/model/ASPType.java deleted file mode 100644 index e703596c..00000000 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPType.java +++ /dev/null @@ -1,4 +0,0 @@ -package de.dhbwstuttgart.sat.asp.model; - -public interface ASPType { -} diff --git a/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java new file mode 100644 index 00000000..c7be7dbc --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/ASPParser.java @@ -0,0 +1,17 @@ +package de.dhbwstuttgart.sat.asp.parser; + +import de.dhbwstuttgart.typeinference.result.ResultPair; +import de.dhbwstuttgart.typeinference.result.ResultSet; + +import java.util.HashSet; +import java.util.Set; + +public class ASPParser { + ResultSet parse(String result){ + Set ret = new HashSet<>(); + for(String pair : result.split(",")){ + + } + return new ResultSet(ret); + } +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java new file mode 100644 index 00000000..614bddb0 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/parser/model/ParsedASPStatement.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.sat.asp.parser.model; + +public class ParsedASPStatement { + public ParsedASPStatement(String statement){ + + } +} diff --git a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java similarity index 94% rename from src/de/dhbwstuttgart/sat/asp/ASPGenerator.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java index 1b942ad0..002e3c7f 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPGenerator.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPGenerator.java @@ -1,9 +1,8 @@ -package de.dhbwstuttgart.sat.asp; +package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.model.*; +import de.dhbwstuttgart.sat.asp.writer.model.*; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; @@ -11,13 +10,10 @@ import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.constraints.Pair; -import de.dhbwstuttgart.typeinference.unify.model.UnifyType; -import java.sql.Ref; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.Optional; public class ASPGenerator { ASPWriter writer = new ASPWriter(); diff --git a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java similarity index 67% rename from src/de/dhbwstuttgart/sat/asp/ASPWriter.java rename to src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java index 4d33cbbf..450950d9 100644 --- a/src/de/dhbwstuttgart/sat/asp/ASPWriter.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/ASPWriter.java @@ -1,8 +1,6 @@ -package de.dhbwstuttgart.sat.asp; +package de.dhbwstuttgart.sat.asp.writer; -import de.dhbwstuttgart.sat.asp.model.ASPRefType; -import de.dhbwstuttgart.sat.asp.model.ASPStatement; -import de.dhbwstuttgart.sat.asp.model.ASPType; +import de.dhbwstuttgart.sat.asp.writer.model.ASPStatement; import java.util.HashSet; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java similarity index 53% rename from src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java index 26e6ea02..57619edf 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPGenericType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPGenericType.java @@ -1,7 +1,8 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPGenericType implements ASPType{ - public static final String ASP_GENERIC_TYPE_NAME = "genericType"; private final String name; public ASPGenericType(String name){ @@ -9,6 +10,6 @@ public class ASPGenericType implements ASPType{ } public String toString(){ - return ASP_GENERIC_TYPE_NAME + "(" + name + ")"; + return ASPRule.ASP_GENERIC_TYPE_NAME + "(" + name + ")"; } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java similarity index 90% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPair.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java index bbd3ff3f..662f995c 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPair.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPair.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public abstract class ASPPair { public final ASPType leftSide; diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java similarity index 55% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java index b67920bd..8806f2ce 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairEquals.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairEquals.java @@ -1,13 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPPairEquals extends ASPPair{ - private final static String ASP_PAIR_EQUALS_NAME = "equals"; public ASPPairEquals(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_EQUALS_NAME; + return ASPRule.ASP_PAIR_EQUALS_NAME.toString(); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java similarity index 51% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java index 882054cc..f57170f6 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmaller.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmaller.java @@ -1,13 +1,16 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; + +import java.util.Map; public class ASPPairSmaller extends ASPPair{ - private final static String ASP_PAIR_SMALLER_NAME = "smaller"; public ASPPairSmaller(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_SMALLER_NAME; + return ASPRule.ASP_PAIR_SMALLER_NAME.toString(); } -} +} \ No newline at end of file diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java similarity index 55% rename from src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java index 0e6598c1..e8482bfc 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPPairSmallerDot.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPPairSmallerDot.java @@ -1,13 +1,14 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPPairSmallerDot extends ASPPair{ - private final static String ASP_PAIR_SMALLER_NAME = "smallerDot"; public ASPPairSmallerDot(ASPType ls, ASPType rs){ super(ls, rs); } @Override protected String getRuleName() { - return ASP_PAIR_SMALLER_NAME; + return ASPRule.ASP_PAIR_SMALLER_DOT_NAME.toString(); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java similarity index 61% rename from src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java index f0c06744..3c176e49 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPParameterList.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPParameterList.java @@ -1,24 +1,22 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; -import de.dhbwstuttgart.sat.asp.ASPGenerator; -import de.dhbwstuttgart.sat.asp.ASPWriter; +import de.dhbwstuttgart.sat.asp.model.ASPRule; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPWriter; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; -import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.Set; public class ASPParameterList { - private final static String ASP_PARAMLIST_NAME = "param"; - private final static String ASP_PARAMLIST_END_POINTER = "null"; public final String name; private final List types; public ASPParameterList(List types, ASPWriter writer){ + int paramNum = 0; this.types = types; if(types.size() == 0){ - name = ASP_PARAMLIST_END_POINTER; + name = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); }else{ name = newName(); String nextPointer = name; @@ -27,9 +25,11 @@ public class ASPParameterList { ASPType t = it.next(); String param = nextPointer + "," + t.toString() + ","; nextPointer = newName(); - if(! it.hasNext())nextPointer = ASP_PARAMLIST_END_POINTER; + if(! it.hasNext())nextPointer = ASPRule.ASP_PARAMLIST_END_POINTER.toString(); param += nextPointer; - writer.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLIST_NAME + "(" + param + ")")); + writer.add(new ASPStatement(ASPRule.ASP_PARAMLISTNUMERATION_NAME + "(" + name + "," +t + "," + paramNum + ")")); + paramNum++; //paramDefinitions.add(new ASPStatement(ASP_PARAMLIST_NAME + "(" + param + ")")); } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java similarity index 68% rename from src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java index ba318876..6491ff33 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPRefType.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPRefType.java @@ -1,7 +1,8 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; + +import de.dhbwstuttgart.sat.asp.model.ASPRule; public class ASPRefType implements ASPType { - public static final String ASP_TYPE = "type"; private final ASPParameterList params; private final String name; @@ -15,6 +16,6 @@ public class ASPRefType implements ASPType { } public String toString(){ - return ASP_TYPE + "(" + name +"," + params.name + ")"; + return ASPRule.ASP_TYPE + "(" + name +"," + params.name + ")"; } } diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java similarity index 91% rename from src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java index 8e4a1d35..76bb63e8 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPStatement.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPStatement.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public class ASPStatement { private final String stmt; diff --git a/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java new file mode 100644 index 00000000..da694218 --- /dev/null +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPType.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.sat.asp.writer.model; + +public interface ASPType { +} diff --git a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java similarity index 83% rename from src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java rename to src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java index e2f87636..4b07d30b 100644 --- a/src/de/dhbwstuttgart/sat/asp/model/ASPTypeVar.java +++ b/src/de/dhbwstuttgart/sat/asp/writer/model/ASPTypeVar.java @@ -1,4 +1,4 @@ -package de.dhbwstuttgart.sat.asp.model; +package de.dhbwstuttgart.sat.asp.writer.model; public class ASPTypeVar implements ASPType{ private final String name; diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index d771bd93..dfb8438c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -7,6 +7,7 @@ import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; import de.dhbwstuttgart.syntaxtree.type.*; +import javax.management.Notification; import java.lang.reflect.Modifier; import java.util.Iterator; @@ -61,7 +62,7 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } private void visitMethod(Method method){ - method.getType().accept(this); + method.getReturnType().accept(this); method.getParameterList().accept(this); if(method.block != null) method.block.accept(this); @@ -157,12 +158,13 @@ public abstract class AbstractASTWalker implements ASTVisitor{ @Override public void visit(ForStmt forStmt) { - + forStmt.body_Loop_block.accept(this); } @Override public void visit(IfStmt ifStmt) { - + ifStmt.then_block.accept(this); + ifStmt.else_block.accept(this); } @Override @@ -202,6 +204,11 @@ public abstract class AbstractASTWalker implements ASTVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + unaryExpr.expr.accept(this); + } + @Override public void visit(Return aReturn) { aReturn.retexpr.accept(this); @@ -227,19 +234,14 @@ public abstract class AbstractASTWalker implements ASTVisitor{ } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { - + whileStmt.loopBlock.accept(this); } @Override public void visit(DoStmt whileStmt) { - + whileStmt.loopBlock.accept(this); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index bb48be40..a99692bb 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -14,9 +14,9 @@ public class Constructor extends Method { //TODO: Constructor braucht ein super-Statement - public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, + public Constructor(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations) { - super(modifier, name, returnType, modifiers, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); + super(modifier, name, returnType, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations), gtvDeclarations, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/Field.java b/src/de/dhbwstuttgart/syntaxtree/Field.java index 421f0ff3..8a4230e3 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Field.java +++ b/src/de/dhbwstuttgart/syntaxtree/Field.java @@ -6,7 +6,8 @@ import org.antlr.v4.runtime.Token; import java.util.ArrayList; public class Field extends SyntaxTreeNode implements TypeScope{ - + + public final int modifier; private String name; private RefTypeOrTPHOrWildcardOrGeneric type; @@ -14,6 +15,7 @@ public class Field extends SyntaxTreeNode implements TypeScope{ super(offset); this.name = name; this.type = type; + this.modifier = modifier; } public String getName(){ diff --git a/src/de/dhbwstuttgart/syntaxtree/Method.java b/src/de/dhbwstuttgart/syntaxtree/Method.java index 3686fb4e..99cd3f91 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/de/dhbwstuttgart/syntaxtree/Method.java @@ -22,16 +22,22 @@ import de.dhbwstuttgart.syntaxtree.statement.Block; * @author janulrich * */ -public class Method extends Field implements IItemWithOffset +public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope { public final Block block; + public final int modifier; + public final String name; private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken()); private ExceptionList exceptionlist; private GenericDeclarationList generics; + private final RefTypeOrTPHOrWildcardOrGeneric returnType; - public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block block, + public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block, GenericDeclarationList gtvDeclarations, Token offset) { - super(name, returnType, modifiers, offset); + super(offset); + this.name = name; + this.modifier = modifier; + this.returnType = returnType; this.parameterlist = parameterList; this.block = block; this.generics = gtvDeclarations; @@ -47,11 +53,20 @@ public class Method extends Field implements IItemWithOffset @Override public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { - return this.getType(); + return this.returnType; } @Override public void accept(ASTVisitor visitor) { visitor.visit(this); } + + @Override + public Token getOffset() { + return null; + } + + public String getName() { + return name; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index 195db230..1c4e149c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -50,8 +50,6 @@ public interface StatementVisitor { void visit(This aThis); - void visit(UnaryPlus unaryPlus); - void visit(WhileStmt whileStmt); void visit(DoStmt whileStmt); @@ -67,4 +65,6 @@ public interface StatementVisitor { void visit(SuperCall superCall); void visit(ExpressionReceiver expressionReceiver); + + void visit(UnaryExpr unaryExpr); } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 046b97b8..b0683569 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -48,7 +48,7 @@ public class ASTFactory { if(superjreClass != null){ List params = new ArrayList<>(); for(TypeVariable tv : superjreClass.getTypeParameters()){ - params.add(new RefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); + params.add(new GenericRefType(new GenericTypeName(new GenericContext( name, null),tv.getName()), new NullToken())); } superClass = new RefType(new JavaClassName(superjreClass.getName()), params, new NullToken()); }else{//Jede Klasse und jedes Interface erbt von Object: (auch Object selbst!) @@ -88,7 +88,7 @@ public class ASTFactory { return null; } - return new de.dhbwstuttgart.syntaxtree.Constructor(constructor.getModifiers(), name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); + return new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name,returnType, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); } //private static RefType createType(Class classType) { @@ -109,9 +109,8 @@ public class ASTFactory { Block block = new Block(new ArrayList(), new NullToken()); GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod.getName()); Token offset = new NullToken(); - int modifier = jreMethod.getModifiers(); - return new Method(jreMethod.getModifiers(), name,returnType, modifier, parameterList, block, gtvDeclarations, offset); + return new Method(jreMethod.getModifiers(), name,returnType, parameterList, block, gtvDeclarations, offset); } public static GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName){ @@ -134,7 +133,7 @@ public class ASTFactory { } */ - public static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ + private static RefTypeOrTPHOrWildcardOrGeneric createType(java.lang.reflect.Type type, JavaClassName parentClass, String parentMethod){ if(type.getTypeName().equals("void")){ return new Void(new NullToken()); }else if(type.getTypeName().equals("int")){ diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 6e48c797..a962c2bc 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -6,6 +6,7 @@ import java.util.stream.Collectors; import de.dhbwstuttgart.exceptions.DebugException; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.FCGenerator; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.GenericTypeVar; @@ -25,7 +26,6 @@ public class UnifyTypeFactory { public static FiniteClosure generateFC(List fromClasses) throws ClassNotFoundException { /* - TODO: Generics werden zu TPHs Die transitive Hülle muss funktionieren. Man darf schreiben List extends AL und Vector extends List @@ -35,57 +35,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - HashSet pairs = new HashSet<>(); - for(ClassOrInterface cly : fromClasses){ - pairs.addAll(getSuperTypes(cly, fromClasses)); - } - return new FiniteClosure(pairs); - } - - /** - * Bildet eine Kette vom übergebenen Typ bis hin zum höchsten bekannten Typ - * Als Generics werden TPHs benutzt, welche der Unifikationsalgorithmus korrekt interpretieren muss. - * Die verwendeten TPHs werden in der Kette nach oben gereicht, so erhält der selbe GTV immer den selben TPH - * @param forType - * @return - */ - private static List getSuperTypes(ClassOrInterface forType, List availableClasses) throws ClassNotFoundException { - return getSuperTypes(forType, availableClasses, new HashMap<>()); - } - - private static List getSuperTypes(ClassOrInterface forType, List availableClasses, HashMap gtvs) throws ClassNotFoundException { - List params = new ArrayList<>(); - //Generics mit gleichem Namen müssen den selben TPH bekommen - for(GenericTypeVar gtv : forType.getGenerics()){ - if(!gtvs.containsKey(gtv.getParsedName())) - gtvs.put(gtv.getParsedName(), PlaceholderType.freshPlaceholder()); - params.add(gtvs.get(gtv.getParsedName())); - } - Optional hasSuperclass = availableClasses.stream().filter(cl -> forType.getSuperClass().getName().equals(cl.getClassName())).findAny(); - ClassOrInterface superClass; - if(!hasSuperclass.isPresent()) //TODO: Wenn es die Object-Klasse ist, dann ist es in Ordnung, ansonsten Fehler ausgeben: - { - superClass = ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(forType.getSuperClass().getName().toString())); - }else{ - superClass = hasSuperclass.get(); - } - List superTypes; - if(superClass.getClassName().equals(ASTFactory.createObjectClass().getClassName())){ - superTypes = Arrays.asList(generateSmallerPair(convert(ASTFactory.createObjectType()), convert(ASTFactory.createObjectType()))); - }else{ - superTypes = getSuperTypes(superClass, availableClasses, gtvs); - } - - TypeParams paramList = new TypeParams(params); - UnifyType t1 = new ReferenceType(forType.getClassName().toString(), paramList); - UnifyType t2 = superTypes.get(0).getLhsType(); - - UnifyPair ret = generateSmallerPair(t1, t2); - List retList = new ArrayList<>(); - retList.add(ret); - retList.addAll(superTypes); - - return retList; + return new FiniteClosure(FCGenerator.toFC(fromClasses)); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr){ diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 5d6a4658..4af1e770 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -11,11 +11,11 @@ import org.antlr.v4.runtime.Token; public class IfStmt extends Statement { public final Expression expr; - public final Block then_block; - public final Block else_block; + public final Statement then_block; + public final Statement else_block; public IfStmt(RefTypeOrTPHOrWildcardOrGeneric type, - Expression expr, Block thenBlock, Block elseBlock, Token offset) + Expression expr, Statement thenBlock, Statement elseBlock, Token offset) { super(type,offset); this.expr = expr; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java b/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java new file mode 100644 index 00000000..366c0602 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/JavaInternalExpression.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +import java.nio.charset.StandardCharsets; + +public abstract class JavaInternalExpression extends Statement{ + public JavaInternalExpression(RefTypeOrTPHOrWildcardOrGeneric retType, Token offset){ + super(retType, offset); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java deleted file mode 100644 index 67e71e95..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NegativeExpr.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import org.antlr.v4.runtime.Token; - - - -public class NegativeExpr extends UnaryExpr -{ - public NegativeExpr(Token offset) - { - super(offset); - } - private UnaryMinus neg; - public Expression expr; - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java deleted file mode 100644 index 0fcfd333..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/NotExpr.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class NotExpr extends UnaryExpr -{ - public NotExpr(Token offset) - { - super(offset); - } - private UnaryNot not; - public Expression expr; -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java deleted file mode 100644 index bec6ffbe..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PositivExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class PositivExpr extends UnaryExpr -{ - public PositivExpr(Token offset) - { - super(offset); - } - protected UnaryPlus plus; - public Expression expr; - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java deleted file mode 100644 index 18b56f96..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostDecExpr.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import de.dhbwstuttgart.parser.antlr.Java8Parser; - - - - -public class PostDecExpr extends PostIncExpr -{ - - public PostDecExpr(Expression expr) { - super(expr); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java deleted file mode 100644 index 1292192b..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PostIncExpr.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.exceptions.NotImplementedException; - - -public class PostIncExpr extends UnaryExpr -{ - public PostIncExpr(Expression expr) { - super(null); - } - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java deleted file mode 100644 index de0fb0b1..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java +++ /dev/null @@ -1,17 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; -import java.util.Hashtable; -import java.util.Iterator; - -import org.antlr.v4.runtime.Token; - - - - - -public class PreDecExpr extends UnaryExpr -{ - public PreDecExpr(Token offset) - { - super(offset); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java deleted file mode 100644 index 736dc311..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import org.antlr.v4.runtime.Token; - -public class PreIncExpr extends UnaryExpr -{ - public PreIncExpr(Token offset) - { - super(offset); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java index 2157560c..ca06146a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryExpr.java @@ -1,14 +1,37 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; -public abstract class UnaryExpr extends MethodCall +public class UnaryExpr extends JavaInternalExpression { + public enum Operation{ + NOT, + MINUS, + PREINCREMENT, + PREDECREMENT, + POSTINCREMENT, + PLUS, POSTDECREMENT + } + + + public final Operation operation; public Expression expr; - public UnaryExpr(Token offset) + public UnaryExpr(Operation operation, Expression argument, RefTypeOrTPHOrWildcardOrGeneric retType, Token offset) { - super(null,null,null,null,null); + super(retType, offset); + this.expr = argument; + this.operation = operation; } + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } + + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java deleted file mode 100644 index 6c100225..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryMinus.java +++ /dev/null @@ -1,12 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; - -public class UnaryMinus extends UnaryPlus -{ - - public UnaryMinus(Expression expression) { - super(expression); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java deleted file mode 100644 index 2dce91bd..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryNot.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - - -public class UnaryNot -{ - -} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java b/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java deleted file mode 100644 index b374166f..00000000 --- a/src/de/dhbwstuttgart/syntaxtree/statement/UnaryPlus.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.dhbwstuttgart.syntaxtree.statement; - -import de.dhbwstuttgart.syntaxtree.StatementVisitor; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.exceptions.NotImplementedException; - -public class UnaryPlus extends Expression -{ - public UnaryPlus(Expression expression){ - super(expression.getType(),expression.getOffset()); - - } - - @Override - public void accept(StatementVisitor visitor) { - visitor.visit(this); - } -} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index e760e458..879a4ba2 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -81,7 +81,7 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(Method method) { - method.getType().accept(this); + method.getReturnType().accept(this); out.append(" " + method.getName()); method.getParameterList().accept(this); if(method.block != null) @@ -296,11 +296,6 @@ public class OutputGenerator implements ASTVisitor{ out.append("this"); } - @Override - public void visit(UnaryPlus unaryPlus) { - - } - @Override public void visit(WhileStmt whileStmt) { out.append("while("); @@ -349,4 +344,27 @@ public class OutputGenerator implements ASTVisitor{ public void visit(ExpressionReceiver receiver) { receiver.expr.accept(this); } + + @Override + public void visit(UnaryExpr unaryExpr) { + if(unaryExpr.operation == UnaryExpr.Operation.MINUS){ + out.append("-"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PLUS){ + out.append("+"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT){ + out.append("--"); + } + if(unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){ + out.append("++"); + } + unaryExpr.expr.accept(this); + if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT){ + out.append("--"); + } + if(unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT){ + out.append("++"); + } + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java index dcefafa4..6f90051a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/TypeOutputGenerator.java @@ -214,11 +214,6 @@ public class TypeOutputGenerator extends OutputGenerator { aThis.getType().accept(this); } - @Override - public void visit(UnaryPlus unaryPlus) { - super.visit(unaryPlus); - } - @Override public void visit(WhileStmt whileStmt) { super.visit(whileStmt); diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 8dea5748..ad591ebe 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.typedeployment; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.JavaInternalExpression; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 2152fc65..ed5837ad 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -2,12 +2,14 @@ package de.dhbwstuttgart.typeinference.typeAlgo; //import com.sun.org.apache.xpath.internal.Arg; +import com.sun.org.apache.xpath.internal.operations.Bool; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.TypeinferenceException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.antlr.Java8Parser; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.statement.literal.Literal; import de.dhbwstuttgart.syntaxtree.statement.literal.Null; @@ -193,6 +195,23 @@ public class TYPEStmt implements StatementVisitor{ receiver.expr.accept(this); } + @Override + public void visit(UnaryExpr unaryExpr) { + if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT || + unaryExpr.operation == UnaryExpr.Operation.POSTINCREMENT || + unaryExpr.operation == UnaryExpr.Operation.PREDECREMENT || + unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){ + //@see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2 + //Expression muss zu Numeric Convertierbar sein. also von Numeric erben + RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), unaryExpr.getOffset()); + constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.EQUALSDOT)); + //The type of the postfix increment expression is the type of the variable + constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT)); + }else{ + throw new NotImplementedException(); + } + } + @Override public void visit(Return returnExpr) { returnExpr.retexpr.accept(this); @@ -241,14 +260,11 @@ public class TYPEStmt implements StatementVisitor{ }; } - @Override - public void visit(UnaryPlus unaryPlus) { - throw new NotImplementedException(); - } - @Override public void visit(WhileStmt whileStmt) { - throw new NotImplementedException(); + RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); + constraintsSet.addUndConstraint(new Pair(whileStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); + whileStmt.loopBlock.accept(this); } @Override @@ -345,7 +361,7 @@ public class TYPEStmt implements StatementVisitor{ for(Method m : cl.getMethods()){ if(m.getName().equals(name) && m.getParameterList().getFormalparalist().size() == numArgs){ - RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getType()); + RefTypeOrTPHOrWildcardOrGeneric retType = info.checkGTV(m.getReturnType()); ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(),info), createTypeScope(cl, m))); diff --git a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java index 39ea0eff..8c41660b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -305,7 +305,7 @@ public class RuleSet implements IRuleSet{ if(!rhsSType.getName().equals(lhsSType.getName())) return Optional.empty(); - if(!(lhsSType.getTypeParams().size()==rhsSType.getTypeParams().size()))throw new DebugException("Fehler in Unifizierung"); + if(!(lhsSType.getTypeParams().size()==rhsSType.getTypeParams().size()))throw new DebugException("Fehler in Unifizierung"+ " " + lhsSType.toString() + " " + rhsSType.toString()); //if(rhsSType.getTypeParams().size() != lhsSType.getTypeParams().size()) // return Optional.empty(); diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java index d24b6825..c6aa9b3e 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/Node.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/Node.java @@ -94,6 +94,6 @@ class Node { @Override public String toString() { - return "Node(" + content.toString() + ")"; + return "Node(" + content.toString() + ")\n"; } } diff --git a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java index 56718198..1080f03b 100644 --- a/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java +++ b/src/de/dhbwstuttgart/typeinference/unify/model/UnifyType.java @@ -96,4 +96,14 @@ public abstract class UnifyType { ret.addAll(typeParams.getInvolvedPlaceholderTypes()); return ret; } + + @Override + public int hashCode() { + return this.toString().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return this.toString().equals(obj.toString()); + } } \ No newline at end of file diff --git a/test/asp/ClingoTest.java b/test/asp/ClingoTest.java new file mode 100644 index 00000000..dbfa70d8 --- /dev/null +++ b/test/asp/ClingoTest.java @@ -0,0 +1,46 @@ +package asp; + +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; +import de.dhbwstuttgart.sat.asp.Clingo; +import de.dhbwstuttgart.syntaxtree.ClassOrInterface; +import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; +import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; +import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; +import de.dhbwstuttgart.typeinference.constraints.Pair; +import de.dhbwstuttgart.typeinference.unify.model.PairOperator; +import org.junit.Test; + +import java.io.*; +import java.util.*; + +public class ClingoTest { + public static final String rootDirectory = "~/Sync/HiwiJob/ResearchPapers/MasterarbeitStadelmeier/asp/unifyWithoutWildcards"; + public static final String tempDirectory = "/tmp/"; + + @Test + public void test() throws IOException, InterruptedException { + String content = ""; + content = new ASPGenerator(this.getPairs(), this.getFC()).getASP(); + + PrintWriter writer = new PrintWriter(tempDirectory + "test.lp", "UTF-8"); + writer.println(content); + writer.close(); + + Clingo clingo = new Clingo(Arrays.asList(new File(tempDirectory + "test.lp"))); + System.out.println(clingo.runClingo()); + } + + public Collection getFC() { + Set ret = new HashSet<>(); + ret.add(ASTFactory.createObjectClass()); + ret.add(ASTFactory.createClass(java.util.List.class)); + return ret; + } + + public ConstraintSet getPairs() { + ConstraintSet ret = new ConstraintSet<>(); + ret.addUndConstraint(new Pair(TypePlaceholder.fresh(new NullToken()), ASTFactory.createObjectType(), PairOperator.SMALLERDOT)); + return ret; + } +} diff --git a/test/asp/typeinference/ASPTest.java b/test/asp/typeinference/ASPTest.java index 5074e4f3..b41d9405 100644 --- a/test/asp/typeinference/ASPTest.java +++ b/test/asp/typeinference/ASPTest.java @@ -1,7 +1,7 @@ package asp.typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.sat.asp.ASPGenerator; +import de.dhbwstuttgart.sat.asp.writer.ASPGenerator; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; diff --git a/test/asp/unifywithoutwildcards/ASPTests.java b/test/asp/unifywithoutwildcards/ASPTests.java new file mode 100644 index 00000000..80e7bb78 --- /dev/null +++ b/test/asp/unifywithoutwildcards/ASPTests.java @@ -0,0 +1,10 @@ +package asp.unifywithoutwildcards; + +import org.junit.Test; + +public class ASPTests { + @Test + public void test(){ + + } +} diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav index e6aa84cd..e1183535 100644 --- a/test/javFiles/Matrix.jav +++ b/test/javFiles/Matrix.jav @@ -1,54 +1,26 @@ import java.util.Vector; +import java.lang.Integer; +import java.lang.Boolean; class Matrix extends Vector> { - - methode(Matrix m) { - Vector> i; - methode(i); - } - } - -/* -import java.util.Vector; - -class Matrix extends Vector> { - - Matrix mul_rec(Matrix m) { - auto v1 = new Vector(); - auto v2 = new Vector(); - auto i = 0; - while(i < m.size()) { - auto v; - v = m.elementAt(i); - v2.addElement(v.remove(v.size()-1)); - i++; - } - Matrix ret; - if (m.elementAt(0).size() > 0) { - ret = this.mul_rec(m); - } - else { - ret = new Matrix(); - i = 0; - while (i < this.size()) { - ret.addElement(new Vector()); - i++; - } - } - i = 0; - while (i < this.size()) { - int erg = 0; - auto j; - j = 0; - while (j < v2.size()) { - erg = erg + this.elementAt(i).elementAt(j).intValue() - * v2.elementAt(j).intValue(); - j++; - } - ret.elementAt(i).addElement(erg); - i++; - } - return ret; - } + mul(m) { + var ret = new Matrix(); + var i = 0; + while(i < 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++; } + return ret; + } } -*/ \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index fbbed9af..03452018 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -1,29 +1,16 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; -import de.dhbwstuttgart.parser.scope.JavaClassName; -import de.dhbwstuttgart.sat.asp.ASPGenerator; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.SourceFile; -import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; -import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; +import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; -import de.dhbwstuttgart.typeinference.constraints.Constraint; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.constraints.Pair; import de.dhbwstuttgart.typeinference.result.ResultSet; -import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; -import de.dhbwstuttgart.typeinference.unify.TypeUnify; -import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure; -import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import org.junit.Test; import java.io.File; import java.io.IOException; -import java.net.URL; -import java.net.URLClassLoader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -32,7 +19,6 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; public class JavaTXCompilerTest { @@ -66,6 +52,7 @@ public class JavaTXCompilerTest { for(File f : compiler.sourceFiles.keySet()){ SourceFile sf = compiler.sourceFiles.get(f); System.out.println(ASTTypePrinter.print(sf)); + System.out.println(ASTPrinter.print(sf)); //List results = compiler.typeInference(); PL 2017-10-03 vor die For-Schleife gezogen assert results.size()>0; Set insertedTypes = new HashSet<>(); diff --git a/test/typeinference/MatrixTest.java b/test/typeinference/MatrixTest.java new file mode 100644 index 00000000..8cc587c9 --- /dev/null +++ b/test/typeinference/MatrixTest.java @@ -0,0 +1,9 @@ +package typeinference; + +import java.io.File; + +public class MatrixTest extends JavaTXCompilerTest{ + public MatrixTest() { + this.fileToTest = new File(rootDirectory+"Matrix.jav"); + } +} \ No newline at end of file