Anpassung bei typisierungen in TYPEExpr, Anpassung Ausgabe

This commit is contained in:
Aldaron7 2018-03-23 21:41:31 +01:00
parent 52dd0e6276
commit ce2b4e0303
17 changed files with 255 additions and 156 deletions

View File

@ -7,10 +7,10 @@ import java.util.List;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.strucTypes.exception.IllegalInterfaceTypeException;
import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Constructor;
@ -42,15 +42,15 @@ public class Construct extends DefaultASTVisitor {
} }
} }
public InferredTypes getInferredTypes() {
return inferredTypes;
}
public List<ClassOrInterface> getConstructedInterfaces() { public List<ClassOrInterface> getConstructedInterfaces() {
this.newInterf.forEach(i -> i.accept(this)); this.newInterf.forEach(i -> i.accept(this));
return constructedInterfaces; return constructedInterfaces;
} }
public InferredTypes getInferredTypes() {
return inferredTypes;
}
public List<SubTypeConstraint> getSubTypeConstraints() { public List<SubTypeConstraint> getSubTypeConstraints() {
return subTypeConstraints; return subTypeConstraints;
} }
@ -70,12 +70,10 @@ public class Construct extends DefaultASTVisitor {
this.constructedInterfaces.add(this.constructInterface(typePlaceholder, name)); this.constructedInterfaces.add(this.constructInterface(typePlaceholder, name));
} }
// TODO check nur TPH in newInterf //check nur TPH in newInterf
@Override @Override
public void visit(RefType refType) { public void visit(RefType refType) {
// JavaClassName name = refType.getName(); throw new IllegalInterfaceTypeException(String.format("%s is not a valid type to generate an interface for.", refType));
// this.constructedInterfaces.add(this.constructInterface(refType,
// name));
} }
private ClassOrInterface constructInterface(TypePlaceholder i, JavaClassName name) { private ClassOrInterface constructInterface(TypePlaceholder i, JavaClassName name) {
@ -85,24 +83,20 @@ public class Construct extends DefaultASTVisitor {
List<RefTypeOrTPHOrWildcardOrGeneric> parameterInhTyterm = new ArrayList<>(); List<RefTypeOrTPHOrWildcardOrGeneric> parameterInhTyterm = new ArrayList<>();
final Token offset = new NullToken(); final Token offset = new NullToken();
// For über alle FieldConstraints mit ClassType i // Schleife über alle FieldConstraints mit ClassType i
this.constraintsSet.getFieldConstraints().stream().filter(fc -> fc.getClassType().equals(i)).forEach(fc -> { this.constraintsSet.getFieldConstraints().stream().filter(fc -> fc.getClassType().equals(i)).forEach(fc -> {
TypePlaceholder type = TypePlaceholder.fresh(offset); TypePlaceholder type = TypePlaceholder.fresh(offset);
parameterInhTyterm.add(fc.getFieldType()); parameterInhTyterm.add(fc.getFieldType());
// TODO generics.add(new GenericTypeVar(s, bounds, offset,
// endOffset)); mit type
generics.add(new GenericTypeVar( type.getName(), generics.add(new GenericTypeVar( type.getName(),
new ArrayList<>(), offset, offset)); new ArrayList<>(), offset, offset));
Field field = new Field(fc.getFieldName(), type, Modifier.PUBLIC, offset); Field field = new Field(fc.getFieldName(), type, Modifier.PUBLIC, offset);
fielddecl.add(field); fielddecl.add(field);
}); });
// For über alle MethodConstraints mit ClassType i // Schleife über alle MethodConstraints mit ClassType i
this.constraintsSet.getMethodConstraints().stream().filter(mc -> mc.getClassType().equals(i)).forEach(mc -> { this.constraintsSet.getMethodConstraints().stream().filter(mc -> mc.getClassType().equals(i)).forEach(mc -> {
TypePlaceholder returnType = TypePlaceholder.fresh(offset); TypePlaceholder returnType = TypePlaceholder.fresh(offset);
parameterInhTyterm.add(mc.getReturnType()); parameterInhTyterm.add(mc.getReturnType());
// TODO generics.add(new GenericTypeVar(s, bounds, offset,
// endOffset)); mit retrunType
generics.add(new GenericTypeVar(returnType.getName(), new ArrayList<>(), offset, offset)); generics.add(new GenericTypeVar(returnType.getName(), new ArrayList<>(), offset, offset));
Block block = new Block(new ArrayList<>(), offset); Block block = new Block(new ArrayList<>(), offset);
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), offset); GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), offset);
@ -111,8 +105,6 @@ public class Construct extends DefaultASTVisitor {
TypePlaceholder tph = TypePlaceholder.fresh(offset); TypePlaceholder tph = TypePlaceholder.fresh(offset);
params.add(new FormalParameter(tph.getName(), tph, offset)); params.add(new FormalParameter(tph.getName(), tph, offset));
parameterInhTyterm.add(supertype); parameterInhTyterm.add(supertype);
// TODO generics.add(new GenericTypeVar(s, bounds, offset,
// endOffset)); mit tph
generics.add(new GenericTypeVar(tph.getName(), new ArrayList<>(), offset, offset)); generics.add(new GenericTypeVar(tph.getName(), new ArrayList<>(), offset, offset));
}); });
ParameterList parameterList = new ParameterList(params, offset); ParameterList parameterList = new ParameterList(params, offset);
@ -134,8 +126,6 @@ public class Construct extends DefaultASTVisitor {
final List<RefType> implementedInterfaces = new ArrayList<>(); final List<RefType> implementedInterfaces = new ArrayList<>();
GenericDeclarationList genericClassParameters = new GenericDeclarationList(generics, i.getOffset()); GenericDeclarationList genericClassParameters = new GenericDeclarationList(generics, i.getOffset());
ClassOrInterface constructedInterface = new ClassOrInterface(modifiers, name, fielddecl, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); ClassOrInterface constructedInterface = new ClassOrInterface(modifiers, name, fielddecl, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset);
// new ClassOrInterface(modifiers, name, fielddecl, methods, constructors, genericClassParameters,
// superClass, isInterface, implementedInterfaces, offset);
return constructedInterface; return constructedInterface;
} }

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -12,6 +13,8 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
private Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>(); private Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>();
public static final InferredTypes EMPTY = new InferredTypes();
public void resolveTransitiveTypes() { public void resolveTransitiveTypes() {
Set<TypePlaceholder> keySet = this.inferredTypes.keySet(); Set<TypePlaceholder> keySet = this.inferredTypes.keySet();
for (TypePlaceholder key : keySet) { for (TypePlaceholder key : keySet) {
@ -82,4 +85,19 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
return inferredTypes.entrySet(); return inferredTypes.entrySet();
} }
@Override
public String toString() {
String s = "[";
Iterator<TypePlaceholder> iterator = this.inferredTypes.keySet().iterator();
while (iterator.hasNext()) {
TypePlaceholder tph = iterator.next();
s += tph + " -> " + inferredTypes.get(tph);
if (iterator.hasNext()) {
s += ", ";
}
}
s += "]";
return s;
}
} }

View File

@ -21,7 +21,7 @@ public class StrucTYPE extends DefaultASTVisitor {
} }
public ConstraintsSet getConstraints() { public ConstraintsSet getConstraints() {
for (ClassOrInterface cls : sourceFile.getClasses()) { for (ClassOrInterface cls : this.sourceFile.getClasses()) {
TYPEExpr typeExpr = new TYPEExpr(); TYPEExpr typeExpr = new TYPEExpr();
cls.accept(typeExpr); cls.accept(typeExpr);
cls.getMethods().forEach(m -> m.accept(this)); cls.getMethods().forEach(m -> m.accept(this));
@ -54,9 +54,9 @@ public class StrucTYPE extends DefaultASTVisitor {
private void evaluateTypeExpr(TYPEExpr typeExpr) { private void evaluateTypeExpr(TYPEExpr typeExpr) {
this.inferredTypes.putAll(typeExpr.getInferredTypes()); this.inferredTypes.putAll(typeExpr.getInferredTypes());
this.inferredTypes.resolveTransitiveTypes(); this.inferredTypes.resolveTransitiveTypes();
this.constraintsSet.addConstraintsSet(typeExpr.getConstraints()); this.constraintsSet.addConstraintsSet(typeExpr.getConstraints());
// TODO infer types in constraints this.constraintsSet.inferTypes(this.inferredTypes);
this.constraintsSet.inferTypes(inferredTypes);
} }
} }

View File

@ -68,8 +68,8 @@ public class TYPEExpr extends DefaultASTVisitor {
} }
if (!fieldTypeVisitor.isTypeVariable() && fieldTypeVisitor.getField(fieldVar.fieldVarName).isPresent()) { if (!fieldTypeVisitor.isTypeVariable() && fieldTypeVisitor.getField(fieldVar.fieldVarName).isPresent()) {
fieldTypeVisitor.getField(fieldVar.fieldVarName).ifPresent( fieldTypeVisitor.getField(fieldVar.fieldVarName)
f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), (RefType) f.getType())); .ifPresent(f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), f.getType()));
} // keine neuen Constraints } // keine neuen Constraints
else { else {
FieldConstraint fieldConstraint = new FieldConstraint(fieldVar.receiver.getType(), fieldVar.fieldVarName, FieldConstraint fieldConstraint = new FieldConstraint(fieldVar.receiver.getType(), fieldVar.fieldVarName,
@ -78,6 +78,13 @@ public class TYPEExpr extends DefaultASTVisitor {
} }
} }
/**
* Löst die Typenzugehörigkeit zwischen fieldVar mit receiver this und dem
* korrespondierenden Feld in der Instanz auf.
*
* @param fieldVar
* @return true, wenn ein passendes Feld in der Instanz gefungen wird.
*/
private boolean inferFieldVarTypes(FieldVar fieldVar) { private boolean inferFieldVarTypes(FieldVar fieldVar) {
if (fieldVar.receiver instanceof This) { if (fieldVar.receiver instanceof This) {
this.aThis.getFieldDecl().stream().filter(f -> f.getName().equals(fieldVar.fieldVarName)) this.aThis.getFieldDecl().stream().filter(f -> f.getName().equals(fieldVar.fieldVarName))
@ -111,7 +118,7 @@ public class TYPEExpr extends DefaultASTVisitor {
this.constraints.addConstraint(new SubTypeConstraint(arguments.get(i).getType(), this.constraints.addConstraint(new SubTypeConstraint(arguments.get(i).getType(),
m.getParameterList().getParameterAt(i).getType())); m.getParameterList().getParameterAt(i).getType()));
} }
this.inferredTypes.put((TypePlaceholder) methodCall.getType(), (RefType) m.getReturnType()); this.inferredTypes.put((TypePlaceholder) methodCall.getType(), m.getReturnType());
}); });
} else { } else {
MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name, MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name,
@ -129,11 +136,10 @@ public class TYPEExpr extends DefaultASTVisitor {
@Override @Override
public void visit(CastExpr castExpr) { public void visit(CastExpr castExpr) {
castExpr.expr.accept(this); castExpr.expr.accept(this);
// TODO RefType equals implementieren
if (((RefType) castExpr.getType()).getName().equals(this.aThis.getClassName())) { if (((RefType) castExpr.getType()).getName().equals(this.aThis.getClassName())) {
// keine neuen Constraints // keine neuen Constraints
} else { } else {
// TODO implement generics // implement generics nicht nötig
} }
} }
@ -142,21 +148,20 @@ public class TYPEExpr extends DefaultASTVisitor {
newClass.getArgumentList().accept(this); newClass.getArgumentList().accept(this);
RefType type = (RefType) newClass.getType(); RefType type = (RefType) newClass.getType();
TypeExtract typeExtract = new TypeExtract(); TypeExtract typeExtract = new TypeExtract();
// TODO RefType equals implementieren
if (type.getName().equals(this.aThis.getClassName())) { if (type.getName().equals(this.aThis.getClassName())) {
this.aThis.accept(typeExtract); this.aThis.accept(typeExtract);
} else { } else {
type.accept(typeExtract); type.accept(typeExtract);
// TODO implement generics // implement generics nicht nötig
} }
this.createNewClassSubTypeConstraints(newClass, typeExtract); this.createNewClassSubTypeConstraints(newClass, typeExtract);
} }
private void createNewClassSubTypeConstraints(NewClass newClass, TypeExtract typeExtract) { private void createNewClassSubTypeConstraints(NewClass newClass, TypeExtract typeExtract) {
List<Expression> arguments = newClass.getArgumentList().getArguments(); final List<Expression> arguments = newClass.getArgumentList().getArguments();
List<Field> fields = typeExtract.getFields(); final List<Field> fields = typeExtract.getFields();
int argumentsSize = arguments.size(); final int argumentsSize = arguments.size();
int fieldsSize = fields.size(); final int fieldsSize = fields.size();
if (argumentsSize != fieldsSize) { if (argumentsSize != fieldsSize) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(String.format(
"The number of fields (%d) in %s doesn't match the number of arguments (%d) passed to its constructor.", "The number of fields (%d) in %s doesn't match the number of arguments (%d) passed to its constructor.",
@ -171,10 +176,11 @@ public class TYPEExpr extends DefaultASTVisitor {
@Override @Override
public void visit(This aThis) { public void visit(This aThis) {
//TODO TPH von This mit Klasse in inferredTypes verknüpfen // TPH von This mit Klasse in inferredTypes verknüpfen
// if (this.aThis != null) { if (this.aThis != null) {
// this.inferredTypes.put((TypePlaceholder) aThis.getType(), (RefType) this.aThis.getType()); this.inferredTypes.put((TypePlaceholder) aThis.getType(), ClassOrInterface
// } .generateTypeOfClass(this.aThis.getClassName(), this.aThis.getGenerics(), this.aThis.getOffset()));
}
} }

View File

@ -81,13 +81,11 @@ public class TypeExtract extends DefaultASTVisitor {
@Override @Override
public void visit(Field field) { public void visit(Field field) {
// if (field.getType() instanceof RefType)
this.fields.add(field); this.fields.add(field);
} }
@Override @Override
public void visit(Method method) { public void visit(Method method) {
if (method.getReturnType() instanceof RefType)
this.methods.add(method); this.methods.add(method);
} }

View File

@ -42,5 +42,10 @@ public class FieldConstraint {
this.fieldType = inferredTypes.get(fieldType); this.fieldType = inferredTypes.get(fieldType);
} }
} }
@Override
public String toString() {
return String.format("F(%s, %s, %s)", this.classType, this.fieldName, this.fieldType);
}
} }

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.strucTypes.constraint; package de.dhbwstuttgart.strucTypes.constraint;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
@ -71,4 +72,39 @@ public class MethodConstraint {
arguments.forEach(c -> c.inferTypes(inferredTypes)); arguments.forEach(c -> c.inferTypes(inferredTypes));
} }
@Override
public String toString() {
StringBuilder sb = new StringBuilder("M(");
sb.append(this.classType);
sb.append(", ");
sb.append(this.methodName);
sb.append(", [");
List<SubTypeConstraint> arguments = this.arguments;
if (!arguments.isEmpty()) {
Iterator<SubTypeConstraint> iterator = arguments.iterator();
while (iterator.hasNext()) {
RefTypeOrTPHOrWildcardOrGeneric subtype = iterator.next().getSubtype();
sb.append(subtype.toString());
if (iterator.hasNext()) {
sb.append(", ");
}
}
}
sb.append("],(");
sb.append(this.returnType);
sb.append(", [");
if (!arguments.isEmpty()) {
Iterator<SubTypeConstraint> iterator = arguments.iterator();
while (iterator.hasNext()) {
RefTypeOrTPHOrWildcardOrGeneric supertype = iterator.next().getSupertype();
sb.append(supertype.toString());
if (iterator.hasNext()) {
sb.append(", ");
}
}
}
sb.append("]))");
return sb.toString();
}
} }

View File

@ -62,4 +62,9 @@ public class SubTypeConstraint {
return null; return null;
} }
@Override
public String toString() {
return String.format("%s <* %s", this.subtype, this.supertype);
}
} }

View File

@ -0,0 +1,32 @@
package de.dhbwstuttgart.strucTypes.exception;
public class IllegalInterfaceTypeException extends RuntimeException {
private static final long serialVersionUID = 1L;
public IllegalInterfaceTypeException() {
// TODO Auto-generated constructor stub
}
public IllegalInterfaceTypeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public IllegalInterfaceTypeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public IllegalInterfaceTypeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public IllegalInterfaceTypeException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -2,16 +2,12 @@ package de.dhbwstuttgart.strucTypes.printutils;
import java.util.List; import java.util.List;
import de.dhbwstuttgart.strucTypes.DefaultASTVisitor;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint; import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint; import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class PrintConstraints extends DefaultASTVisitor { public class PrintConstraints {
public void print(ConstraintsSet constraintsSet) { public void print(ConstraintsSet constraintsSet) {
printSubTypeConstraints(constraintsSet.getSubTypeConstraints()); printSubTypeConstraints(constraintsSet.getSubTypeConstraints());
@ -21,67 +17,59 @@ public class PrintConstraints extends DefaultASTVisitor {
public void printSubTypeConstraints(List<SubTypeConstraint> constraints) { public void printSubTypeConstraints(List<SubTypeConstraint> constraints) {
System.out.println("\n SubTypeConstraints:"); System.out.println("\n SubTypeConstraints:");
constraints.forEach(c -> { constraints.forEach(System.out::println);
c.getSubtype().accept(this); // constraints.forEach(c -> System.out
System.out.print(" <* "); // .println(String.format("%s <* %s", c.getSubtype().toString(),
c.getSupertype().accept(this); // c.getSupertype().toString())));
System.out.println();
});
} }
public void printFieldConstraints(List<FieldConstraint> constraints) { public void printFieldConstraints(List<FieldConstraint> constraints) {
System.out.println("\n FieldConstraints:"); System.out.println("\n FieldConstraints:");
constraints.forEach(c -> { constraints.forEach(System.out::println);
System.out.print("F("); // constraints.forEach(c -> System.out.println(String.format("F(%s, %s,
c.getClassType().accept(this); // %s)", c.getClassType().toString(),
System.out.print(", "); // c.getFieldName(), c.getFieldType().toString())));
System.out.print(c.getFieldName());
System.out.print(", ");
c.getFieldType().accept(this);
System.out.println(")");
});
} }
public void printMethodConstraints(List<MethodConstraint> constraints) { public void printMethodConstraints(List<MethodConstraint> constraints) {
System.out.println("\n MethodConstraints:"); System.out.println("\n MethodConstraints:");
constraints.forEach(c -> { constraints.forEach(System.out::println);
System.out.print("M("); // constraints.forEach(c -> {
c.getClassType().accept(this); // StringBuilder sb = new StringBuilder("M(");
System.out.print(", "); // sb.append(c.getClassType().toString());
System.out.print(c.getMethodName() + ", ["); // sb.append(", ");
c.getArguments().forEach(a -> { // sb.append(c.getMethodName());
a.getSubtype().accept(this); // sb.append(", [");
System.out.print(", "); // List<SubTypeConstraint> arguments = c.getArguments();
}); // if (!arguments.isEmpty()) {
System.out.print("],("); // Iterator<SubTypeConstraint> iterator = arguments.iterator();
c.getReturnType().accept(this); // while (iterator.hasNext()) {
System.out.print(", ["); // RefTypeOrTPHOrWildcardOrGeneric subtype =
c.getArguments().forEach(a -> { // iterator.next().getSubtype();
a.getSupertype().accept(this); // sb.append(subtype.toString());
System.out.print(", "); // if (iterator.hasNext()) {
}); // sb.append(", ");
System.out.print("]))"); // }
System.out.println(); // }
}); // }
// sb.append("],(");
// sb.append(c.getReturnType().toString());
// sb.append(", [");
// if (!arguments.isEmpty()) {
// Iterator<SubTypeConstraint> iterator = arguments.iterator();
// while (iterator.hasNext()) {
// RefTypeOrTPHOrWildcardOrGeneric supertype =
// iterator.next().getSupertype();
// sb.append(supertype.toString());
// if (iterator.hasNext()) {
// sb.append(", ");
// }
// }
// }
// sb.append("]))");
// System.out.println(sb.toString());
// });
} }
@Override
public void visit(RefType refType) {
List<RefTypeOrTPHOrWildcardOrGeneric> paraList = refType.getParaList();
System.out.print(refType.getName());
if (!paraList.isEmpty()) {
System.out.print(" <");
paraList.forEach(p -> {
p.accept(this);
System.out.print(", ");
});
System.out.print(">");
}
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
System.out.print(typePlaceholder);
}
} }

View File

@ -1,11 +1,13 @@
package de.dhbwstuttgart.strucTypes.printutils; package de.dhbwstuttgart.strucTypes.printutils;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.List; import java.util.List;
import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.strucTypes.InferredTypes;
import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor; import de.dhbwstuttgart.syntaxtree.Constructor;
@ -55,6 +57,16 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class SyntaxTreePrinter implements ASTVisitor { public class SyntaxTreePrinter implements ASTVisitor {
private InferredTypes inferredTypes;
public SyntaxTreePrinter() {
this(InferredTypes.EMPTY);
}
public SyntaxTreePrinter(InferredTypes inferredTypes) {
this.inferredTypes = inferredTypes;
}
@Override @Override
public void visit(SourceFile sourceFile) { public void visit(SourceFile sourceFile) {
sourceFile.KlassenVektor.forEach(cl -> cl.accept(this)); sourceFile.KlassenVektor.forEach(cl -> cl.accept(this));
@ -69,13 +81,17 @@ public class SyntaxTreePrinter implements ASTVisitor {
Collection<RefType> implementedInterfaces = classOrInterface.getSuperInterfaces(); Collection<RefType> implementedInterfaces = classOrInterface.getSuperInterfaces();
GenericDeclarationList generics = classOrInterface.getGenerics(); GenericDeclarationList generics = classOrInterface.getGenerics();
System.out.print("class: " + className); System.out.print("\nclass: " + className);
if (generics.iterator().hasNext()) { Iterator<GenericTypeVar> iterator = generics.iterator();
if (iterator.hasNext()) {
System.out.print(" <"); System.out.print(" <");
generics.forEach(g -> { while (iterator.hasNext()) {
GenericTypeVar g = iterator.next();
g.accept(this); g.accept(this);
System.out.print(", "); if (iterator.hasNext()) {
}); System.out.print(", ");
}
}
System.out.print(">"); System.out.print(">");
} }
System.out.println(); System.out.println();
@ -165,7 +181,9 @@ public class SyntaxTreePrinter implements ASTVisitor {
@Override @Override
public void visit(Return aReturn) { public void visit(Return aReturn) {
RefTypeOrTPHOrWildcardOrGeneric type = aReturn.getType(); RefTypeOrTPHOrWildcardOrGeneric type = aReturn.getType();
System.out.println("returnType: " + type); System.out.print("returnType: ");
type.accept(this);
System.out.println();
aReturn.retexpr.accept(this); aReturn.retexpr.accept(this);
} }
@ -232,22 +250,16 @@ public class SyntaxTreePrinter implements ASTVisitor {
@Override @Override
public void visit(RefType refType) { public void visit(RefType refType) {
List<RefTypeOrTPHOrWildcardOrGeneric> paraList = refType.getParaList(); System.out.print(refType);
System.out.print(refType.getName());
if (!paraList.isEmpty()) {
System.out.print(" <");
paraList.forEach(p -> {
p.accept(this);
System.out.print(", ");
});
System.out.print(">");
}
} }
@Override @Override
public void visit(TypePlaceholder typePlaceholder) { public void visit(TypePlaceholder typePlaceholder) {
String name = typePlaceholder.getName(); if (inferredTypes.get(typePlaceholder) != null) {
System.out.print("TPH " + name); inferredTypes.get(typePlaceholder).accept(this);
} else {
System.out.print(typePlaceholder);
}
} }
@ -271,7 +283,6 @@ public class SyntaxTreePrinter implements ASTVisitor {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override @Override
public void visit(LambdaExpression lambdaExpression) { public void visit(LambdaExpression lambdaExpression) {
throw new NotImplementedException(); throw new NotImplementedException();
@ -314,7 +325,6 @@ public class SyntaxTreePrinter implements ASTVisitor {
System.out.println("empty statement"); System.out.println("empty statement");
} }
@Override @Override
public void visit(Literal literal) { public void visit(Literal literal) {
RefTypeOrTPHOrWildcardOrGeneric type = literal.getType(); RefTypeOrTPHOrWildcardOrGeneric type = literal.getType();
@ -345,9 +355,10 @@ public class SyntaxTreePrinter implements ASTVisitor {
@Override @Override
public void visit(ExpressionReceiver expressionReceiver) { public void visit(ExpressionReceiver expressionReceiver) {
Expression expr = expressionReceiver.expr; Expression expr = expressionReceiver.expr;
RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType(); // RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType();
System.out.print("expressionReceiverType: " + type); // System.out.print("expressionReceiverType: ");
System.out.print(" expressionReceiver: "); // type.accept(this);
// System.out.print(" expressionReceiver: ");
expr.accept(this); expr.accept(this);
} }

View File

@ -37,29 +37,33 @@ public class TestConstruct {
String name = f.getName(); String name = f.getName();
System.out.println("Filename: " + name); System.out.println("Filename: " + name);
SourceFile sourceFile = compiler.sourceFiles.get(f); SourceFile sourceFile = compiler.sourceFiles.get(f);
//Print SourceFile Infos // Print SourceFile Infos
sourceFile.accept(new SyntaxTreePrinter()); sourceFile.accept(new SyntaxTreePrinter());
StrucTYPE strucTYPE = new StrucTYPE(sourceFile); StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
ConstraintsSet constraints = strucTYPE.getConstraints(); final ConstraintsSet constraints = strucTYPE.getConstraints();
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
System.out.println("\n--StrucTYPE--");
printConstraints.print(constraints); printConstraints.print(constraints);
PrintInferredTypes.print(inferredTypesType);
InferredTypes inferredTypes = strucTYPE.getInferredTypes();
PrintInferredTypes.print(inferredTypes); Construct construct = new Construct(constraints, inferredTypesType);
Construct construct = new Construct(constraints, inferredTypes); final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
final List<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces(); final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
final SyntaxTreePrinter syntaxTreePrinterInferred = new SyntaxTreePrinter(inferredTypesConstruct);
System.out.println("\n--Construct--");
System.out.println("\nConstructed Interfaces:"); System.out.println("\nConstructed Interfaces:");
constructedInterfaces.forEach(i-> i.accept(new SyntaxTreePrinter())); constructedInterfaces.forEach(i -> i.accept(syntaxTreePrinterInferred));
List<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
printConstraints.printSubTypeConstraints(subTypeConstraints); printConstraints.printSubTypeConstraints(subTypeConstraints);
PrintInferredTypes.print(inferredTypesConstruct);
inferredTypes = construct.getInferredTypes(); System.out.println("\n--Inferred SysntaxTree--");
PrintInferredTypes.print(inferredTypes); sourceFile.accept(syntaxTreePrinterInferred);
System.out.println("____________________________________________________________________________"); System.out.println("____________________________________________________________________________");
} }
} }

View File

@ -20,13 +20,13 @@ public class TestStrucType {
@org.junit.Test @org.junit.Test
public void test() throws ClassNotFoundException, IOException { public void test() throws ClassNotFoundException, IOException {
ArrayList<File> files = new ArrayList<>(); ArrayList<File> files = new ArrayList<>();
// files.add(new File(rootDirectory + "testLocalVar.jav")); files.add(new File(rootDirectory + "testLocalVar.jav"));
// files.add(new File(rootDirectory + "testCast.jav")); files.add(new File(rootDirectory + "testCast.jav"));
// files.add(new File(rootDirectory + "testNew.jav")); files.add(new File(rootDirectory + "testNew.jav"));
files.add(new File(rootDirectory + "testFieldVar.jav")); files.add(new File(rootDirectory + "testFieldVar.jav"));
// files.add(new File(rootDirectory + "testFieldMethod.jav")); files.add(new File(rootDirectory + "testFieldMethod.jav"));
// files.add(new File(rootDirectory + "testMethod.jav")); files.add(new File(rootDirectory + "testMethod.jav"));
// files.add(new File(rootDirectory + "testPaperExample.jav")); files.add(new File(rootDirectory + "testPaperExample.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(files); JavaTXCompiler compiler = new JavaTXCompiler(files);
for (File f : compiler.sourceFiles.keySet()) { for (File f : compiler.sourceFiles.keySet()) {
String name = f.getName(); String name = f.getName();

View File

@ -15,18 +15,18 @@ public class TestSyntaxTreePrinter {
@org.junit.Test @org.junit.Test
public void test() throws ClassNotFoundException, IOException { public void test() throws ClassNotFoundException, IOException {
ArrayList<File> files = new ArrayList<>(); ArrayList<File> files = new ArrayList<>();
// files.add(new File(rootDirectory + "testLocalVar.jav")); files.add(new File(rootDirectory + "testLocalVar.jav"));
// files.add(new File(rootDirectory + "testCast.jav")); files.add(new File(rootDirectory + "testCast.jav"));
files.add(new File(rootDirectory + "testNew.jav")); files.add(new File(rootDirectory + "testNew.jav"));
// files.add(new File(rootDirectory + "testFieldVar.jav")); files.add(new File(rootDirectory + "testFieldVar.jav"));
// files.add(new File(rootDirectory + "testFieldMethod.jav")); files.add(new File(rootDirectory + "testFieldMethod.jav"));
// files.add(new File(rootDirectory + "testPaperExample.jav")); files.add(new File(rootDirectory + "testPaperExample.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(files); JavaTXCompiler compiler = new JavaTXCompiler(files);
compiler.sourceFiles.keySet().forEach(f->{ compiler.sourceFiles.keySet().forEach(f->{
String name = f.getName(); String name = f.getName();
System.out.println("Filename: " + name); System.out.println("Filename: " + name);
compiler.sourceFiles.get(f).accept(new SyntaxTreePrinter()); compiler.sourceFiles.get(f).accept(new SyntaxTreePrinter());
System.out.println(); System.out.println("________________________________________");
}); });
// this.printSyntaxTree(compiler); // this.printSyntaxTree(compiler);
System.out.println("test end"); System.out.println("test end");

View File

@ -1,10 +1,13 @@
package strucType.input; package strucType.input;
import strucType.typedtestclasses.A; import strucType.typedtestclasses.A;
import strucType.typedtestclasses.A2; import strucType.typedtestclasses.A2;
import strucType.typedtestclasses.B; import strucType.typedtestclasses.C;
class C class Cast
{ {
mA(A x){return (A2)x; } mA(A x){return (A2) x; }
mB(c) { return (B)c; }
mC(a) { return (C) mC2(a); }
mC2(c) { return (C) c; }
} }

View File

@ -6,9 +6,11 @@ class M
{ {
mM(x) { return mA(x); } mM(x) { return mA(x); }
A mA(x) { return x.getA(); } mA(x) { return x.getA(); }
A2 m(A a) { return a; } m(A a) { return a; }
A m2(a) { return a; }
} }

View File

@ -1,10 +1,11 @@
package strucType.input; package strucType.input;
import strucType.typedtestclasses.A; import strucType.typedtestclasses.A;
import strucType.typedtestclasses.B; import strucType.typedtestclasses.B;
class Neu class N
{ {
a; a;
mA(){ return new A(a); } mA(){ return new A(a); }
mB(x){ return new B(x,a); } mB(x){ return new B(x,a); }
mN(a) {return new N(a); }
} }