Anpassung bei typisierungen in TYPEExpr, Anpassung Ausgabe
This commit is contained in:
parent
52dd0e6276
commit
ce2b4e0303
@ -7,10 +7,10 @@ import java.util.List;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import de.dhbwstuttgart.parser.NullToken;
|
||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
|
||||
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
|
||||
import de.dhbwstuttgart.strucTypes.exception.IllegalInterfaceTypeException;
|
||||
import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException;
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.Constructor;
|
||||
@ -42,15 +42,15 @@ public class Construct extends DefaultASTVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
public InferredTypes getInferredTypes() {
|
||||
return inferredTypes;
|
||||
}
|
||||
|
||||
public List<ClassOrInterface> getConstructedInterfaces() {
|
||||
this.newInterf.forEach(i -> i.accept(this));
|
||||
return constructedInterfaces;
|
||||
}
|
||||
|
||||
public InferredTypes getInferredTypes() {
|
||||
return inferredTypes;
|
||||
}
|
||||
|
||||
public List<SubTypeConstraint> getSubTypeConstraints() {
|
||||
return subTypeConstraints;
|
||||
}
|
||||
@ -70,12 +70,10 @@ public class Construct extends DefaultASTVisitor {
|
||||
this.constructedInterfaces.add(this.constructInterface(typePlaceholder, name));
|
||||
}
|
||||
|
||||
// TODO check nur TPH in newInterf
|
||||
//check nur TPH in newInterf
|
||||
@Override
|
||||
public void visit(RefType refType) {
|
||||
// JavaClassName name = refType.getName();
|
||||
// this.constructedInterfaces.add(this.constructInterface(refType,
|
||||
// name));
|
||||
throw new IllegalInterfaceTypeException(String.format("%s is not a valid type to generate an interface for.", refType));
|
||||
}
|
||||
|
||||
private ClassOrInterface constructInterface(TypePlaceholder i, JavaClassName name) {
|
||||
@ -85,24 +83,20 @@ public class Construct extends DefaultASTVisitor {
|
||||
List<RefTypeOrTPHOrWildcardOrGeneric> parameterInhTyterm = new ArrayList<>();
|
||||
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 -> {
|
||||
TypePlaceholder type = TypePlaceholder.fresh(offset);
|
||||
parameterInhTyterm.add(fc.getFieldType());
|
||||
// TODO generics.add(new GenericTypeVar(s, bounds, offset,
|
||||
// endOffset)); mit type
|
||||
generics.add(new GenericTypeVar( type.getName(),
|
||||
new ArrayList<>(), offset, offset));
|
||||
Field field = new Field(fc.getFieldName(), type, Modifier.PUBLIC, offset);
|
||||
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 -> {
|
||||
TypePlaceholder returnType = TypePlaceholder.fresh(offset);
|
||||
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));
|
||||
Block block = new Block(new ArrayList<>(), offset);
|
||||
GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), offset);
|
||||
@ -111,8 +105,6 @@ public class Construct extends DefaultASTVisitor {
|
||||
TypePlaceholder tph = TypePlaceholder.fresh(offset);
|
||||
params.add(new FormalParameter(tph.getName(), tph, offset));
|
||||
parameterInhTyterm.add(supertype);
|
||||
// TODO generics.add(new GenericTypeVar(s, bounds, offset,
|
||||
// endOffset)); mit tph
|
||||
generics.add(new GenericTypeVar(tph.getName(), new ArrayList<>(), offset, offset));
|
||||
});
|
||||
ParameterList parameterList = new ParameterList(params, offset);
|
||||
@ -134,8 +126,6 @@ public class Construct extends DefaultASTVisitor {
|
||||
final List<RefType> implementedInterfaces = new ArrayList<>();
|
||||
GenericDeclarationList genericClassParameters = new GenericDeclarationList(generics, i.getOffset());
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@ -12,6 +13,8 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
||||
|
||||
private Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>();
|
||||
|
||||
public static final InferredTypes EMPTY = new InferredTypes();
|
||||
|
||||
public void resolveTransitiveTypes() {
|
||||
Set<TypePlaceholder> keySet = this.inferredTypes.keySet();
|
||||
for (TypePlaceholder key : keySet) {
|
||||
@ -82,4 +85,19 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ public class StrucTYPE extends DefaultASTVisitor {
|
||||
}
|
||||
|
||||
public ConstraintsSet getConstraints() {
|
||||
for (ClassOrInterface cls : sourceFile.getClasses()) {
|
||||
for (ClassOrInterface cls : this.sourceFile.getClasses()) {
|
||||
TYPEExpr typeExpr = new TYPEExpr();
|
||||
cls.accept(typeExpr);
|
||||
cls.getMethods().forEach(m -> m.accept(this));
|
||||
@ -54,9 +54,9 @@ public class StrucTYPE extends DefaultASTVisitor {
|
||||
private void evaluateTypeExpr(TYPEExpr typeExpr) {
|
||||
this.inferredTypes.putAll(typeExpr.getInferredTypes());
|
||||
this.inferredTypes.resolveTransitiveTypes();
|
||||
|
||||
this.constraintsSet.addConstraintsSet(typeExpr.getConstraints());
|
||||
// TODO infer types in constraints
|
||||
this.constraintsSet.inferTypes(inferredTypes);
|
||||
this.constraintsSet.inferTypes(this.inferredTypes);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -68,8 +68,8 @@ public class TYPEExpr extends DefaultASTVisitor {
|
||||
}
|
||||
|
||||
if (!fieldTypeVisitor.isTypeVariable() && fieldTypeVisitor.getField(fieldVar.fieldVarName).isPresent()) {
|
||||
fieldTypeVisitor.getField(fieldVar.fieldVarName).ifPresent(
|
||||
f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), (RefType) f.getType()));
|
||||
fieldTypeVisitor.getField(fieldVar.fieldVarName)
|
||||
.ifPresent(f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), f.getType()));
|
||||
} // keine neuen Constraints
|
||||
else {
|
||||
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) {
|
||||
if (fieldVar.receiver instanceof This) {
|
||||
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(),
|
||||
m.getParameterList().getParameterAt(i).getType()));
|
||||
}
|
||||
this.inferredTypes.put((TypePlaceholder) methodCall.getType(), (RefType) m.getReturnType());
|
||||
this.inferredTypes.put((TypePlaceholder) methodCall.getType(), m.getReturnType());
|
||||
});
|
||||
} else {
|
||||
MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name,
|
||||
@ -129,11 +136,10 @@ public class TYPEExpr extends DefaultASTVisitor {
|
||||
@Override
|
||||
public void visit(CastExpr castExpr) {
|
||||
castExpr.expr.accept(this);
|
||||
// TODO RefType equals implementieren
|
||||
if (((RefType) castExpr.getType()).getName().equals(this.aThis.getClassName())) {
|
||||
// keine neuen Constraints
|
||||
} else {
|
||||
// TODO implement generics
|
||||
// implement generics nicht nötig
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,21 +148,20 @@ public class TYPEExpr extends DefaultASTVisitor {
|
||||
newClass.getArgumentList().accept(this);
|
||||
RefType type = (RefType) newClass.getType();
|
||||
TypeExtract typeExtract = new TypeExtract();
|
||||
// TODO RefType equals implementieren
|
||||
if (type.getName().equals(this.aThis.getClassName())) {
|
||||
this.aThis.accept(typeExtract);
|
||||
} else {
|
||||
type.accept(typeExtract);
|
||||
// TODO implement generics
|
||||
// implement generics nicht nötig
|
||||
}
|
||||
this.createNewClassSubTypeConstraints(newClass, typeExtract);
|
||||
}
|
||||
|
||||
private void createNewClassSubTypeConstraints(NewClass newClass, TypeExtract typeExtract) {
|
||||
List<Expression> arguments = newClass.getArgumentList().getArguments();
|
||||
List<Field> fields = typeExtract.getFields();
|
||||
int argumentsSize = arguments.size();
|
||||
int fieldsSize = fields.size();
|
||||
final List<Expression> arguments = newClass.getArgumentList().getArguments();
|
||||
final List<Field> fields = typeExtract.getFields();
|
||||
final int argumentsSize = arguments.size();
|
||||
final int fieldsSize = fields.size();
|
||||
if (argumentsSize != fieldsSize) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"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
|
||||
public void visit(This aThis) {
|
||||
//TODO TPH von This mit Klasse in inferredTypes verknüpfen
|
||||
// if (this.aThis != null) {
|
||||
// this.inferredTypes.put((TypePlaceholder) aThis.getType(), (RefType) this.aThis.getType());
|
||||
// }
|
||||
// TPH von This mit Klasse in inferredTypes verknüpfen
|
||||
if (this.aThis != null) {
|
||||
this.inferredTypes.put((TypePlaceholder) aThis.getType(), ClassOrInterface
|
||||
.generateTypeOfClass(this.aThis.getClassName(), this.aThis.getGenerics(), this.aThis.getOffset()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -81,13 +81,11 @@ public class TypeExtract extends DefaultASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(Field field) {
|
||||
// if (field.getType() instanceof RefType)
|
||||
this.fields.add(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Method method) {
|
||||
if (method.getReturnType() instanceof RefType)
|
||||
this.methods.add(method);
|
||||
}
|
||||
|
||||
|
@ -42,5 +42,10 @@ public class FieldConstraint {
|
||||
this.fieldType = inferredTypes.get(fieldType);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("F(%s, %s, %s)", this.classType, this.fieldName, this.fieldType);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.dhbwstuttgart.strucTypes.constraint;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.antlr.v4.runtime.Token;
|
||||
@ -71,4 +72,39 @@ public class MethodConstraint {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -62,4 +62,9 @@ public class SubTypeConstraint {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s <* %s", this.subtype, this.supertype);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
}
|
@ -2,16 +2,12 @@ package de.dhbwstuttgart.strucTypes.printutils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import de.dhbwstuttgart.strucTypes.DefaultASTVisitor;
|
||||
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
|
||||
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
|
||||
import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint;
|
||||
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) {
|
||||
printSubTypeConstraints(constraintsSet.getSubTypeConstraints());
|
||||
@ -21,67 +17,59 @@ public class PrintConstraints extends DefaultASTVisitor {
|
||||
|
||||
public void printSubTypeConstraints(List<SubTypeConstraint> constraints) {
|
||||
System.out.println("\n SubTypeConstraints:");
|
||||
constraints.forEach(c -> {
|
||||
c.getSubtype().accept(this);
|
||||
System.out.print(" <* ");
|
||||
c.getSupertype().accept(this);
|
||||
System.out.println();
|
||||
});
|
||||
constraints.forEach(System.out::println);
|
||||
// constraints.forEach(c -> System.out
|
||||
// .println(String.format("%s <* %s", c.getSubtype().toString(),
|
||||
// c.getSupertype().toString())));
|
||||
}
|
||||
|
||||
public void printFieldConstraints(List<FieldConstraint> constraints) {
|
||||
System.out.println("\n FieldConstraints:");
|
||||
constraints.forEach(c -> {
|
||||
System.out.print("F(");
|
||||
c.getClassType().accept(this);
|
||||
System.out.print(", ");
|
||||
System.out.print(c.getFieldName());
|
||||
System.out.print(", ");
|
||||
c.getFieldType().accept(this);
|
||||
System.out.println(")");
|
||||
});
|
||||
constraints.forEach(System.out::println);
|
||||
// constraints.forEach(c -> System.out.println(String.format("F(%s, %s,
|
||||
// %s)", c.getClassType().toString(),
|
||||
// c.getFieldName(), c.getFieldType().toString())));
|
||||
}
|
||||
|
||||
public void printMethodConstraints(List<MethodConstraint> constraints) {
|
||||
System.out.println("\n MethodConstraints:");
|
||||
constraints.forEach(c -> {
|
||||
System.out.print("M(");
|
||||
c.getClassType().accept(this);
|
||||
System.out.print(", ");
|
||||
System.out.print(c.getMethodName() + ", [");
|
||||
c.getArguments().forEach(a -> {
|
||||
a.getSubtype().accept(this);
|
||||
System.out.print(", ");
|
||||
});
|
||||
System.out.print("],(");
|
||||
c.getReturnType().accept(this);
|
||||
System.out.print(", [");
|
||||
c.getArguments().forEach(a -> {
|
||||
a.getSupertype().accept(this);
|
||||
System.out.print(", ");
|
||||
});
|
||||
System.out.print("]))");
|
||||
System.out.println();
|
||||
});
|
||||
constraints.forEach(System.out::println);
|
||||
// constraints.forEach(c -> {
|
||||
// StringBuilder sb = new StringBuilder("M(");
|
||||
// sb.append(c.getClassType().toString());
|
||||
// sb.append(", ");
|
||||
// sb.append(c.getMethodName());
|
||||
// sb.append(", [");
|
||||
// List<SubTypeConstraint> arguments = c.getArguments();
|
||||
// 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(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);
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package de.dhbwstuttgart.strucTypes.printutils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
||||
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
|
||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||
import de.dhbwstuttgart.strucTypes.InferredTypes;
|
||||
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
|
||||
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
|
||||
import de.dhbwstuttgart.syntaxtree.Constructor;
|
||||
@ -55,6 +57,16 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||
|
||||
public class SyntaxTreePrinter implements ASTVisitor {
|
||||
|
||||
private InferredTypes inferredTypes;
|
||||
|
||||
public SyntaxTreePrinter() {
|
||||
this(InferredTypes.EMPTY);
|
||||
}
|
||||
|
||||
public SyntaxTreePrinter(InferredTypes inferredTypes) {
|
||||
this.inferredTypes = inferredTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SourceFile sourceFile) {
|
||||
sourceFile.KlassenVektor.forEach(cl -> cl.accept(this));
|
||||
@ -69,13 +81,17 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
||||
Collection<RefType> implementedInterfaces = classOrInterface.getSuperInterfaces();
|
||||
GenericDeclarationList generics = classOrInterface.getGenerics();
|
||||
|
||||
System.out.print("class: " + className);
|
||||
if (generics.iterator().hasNext()) {
|
||||
System.out.print("\nclass: " + className);
|
||||
Iterator<GenericTypeVar> iterator = generics.iterator();
|
||||
if (iterator.hasNext()) {
|
||||
System.out.print(" <");
|
||||
generics.forEach(g -> {
|
||||
while (iterator.hasNext()) {
|
||||
GenericTypeVar g = iterator.next();
|
||||
g.accept(this);
|
||||
System.out.print(", ");
|
||||
});
|
||||
if (iterator.hasNext()) {
|
||||
System.out.print(", ");
|
||||
}
|
||||
}
|
||||
System.out.print(">");
|
||||
}
|
||||
System.out.println();
|
||||
@ -165,7 +181,9 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
||||
@Override
|
||||
public void visit(Return aReturn) {
|
||||
RefTypeOrTPHOrWildcardOrGeneric type = aReturn.getType();
|
||||
System.out.println("returnType: " + type);
|
||||
System.out.print("returnType: ");
|
||||
type.accept(this);
|
||||
System.out.println();
|
||||
aReturn.retexpr.accept(this);
|
||||
}
|
||||
|
||||
@ -232,22 +250,16 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
||||
|
||||
@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(">");
|
||||
}
|
||||
System.out.print(refType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(TypePlaceholder typePlaceholder) {
|
||||
String name = typePlaceholder.getName();
|
||||
System.out.print("TPH " + name);
|
||||
if (inferredTypes.get(typePlaceholder) != null) {
|
||||
inferredTypes.get(typePlaceholder).accept(this);
|
||||
} else {
|
||||
System.out.print(typePlaceholder);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -271,7 +283,6 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(LambdaExpression lambdaExpression) {
|
||||
throw new NotImplementedException();
|
||||
@ -314,7 +325,6 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
||||
System.out.println("empty statement");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(Literal literal) {
|
||||
RefTypeOrTPHOrWildcardOrGeneric type = literal.getType();
|
||||
@ -345,9 +355,10 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
||||
@Override
|
||||
public void visit(ExpressionReceiver expressionReceiver) {
|
||||
Expression expr = expressionReceiver.expr;
|
||||
RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType();
|
||||
System.out.print("expressionReceiverType: " + type);
|
||||
System.out.print(" expressionReceiver: ");
|
||||
// RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType();
|
||||
// System.out.print("expressionReceiverType: ");
|
||||
// type.accept(this);
|
||||
// System.out.print(" expressionReceiver: ");
|
||||
expr.accept(this);
|
||||
|
||||
}
|
||||
|
@ -37,29 +37,33 @@ public class TestConstruct {
|
||||
String name = f.getName();
|
||||
System.out.println("Filename: " + name);
|
||||
SourceFile sourceFile = compiler.sourceFiles.get(f);
|
||||
//Print SourceFile Infos
|
||||
// Print SourceFile Infos
|
||||
sourceFile.accept(new SyntaxTreePrinter());
|
||||
|
||||
|
||||
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);
|
||||
|
||||
InferredTypes inferredTypes = strucTYPE.getInferredTypes();
|
||||
PrintInferredTypes.print(inferredTypes);
|
||||
|
||||
Construct construct = new Construct(constraints, inferredTypes);
|
||||
|
||||
List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
|
||||
PrintInferredTypes.print(inferredTypesType);
|
||||
|
||||
Construct construct = new Construct(constraints, inferredTypesType);
|
||||
|
||||
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
|
||||
final List<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
|
||||
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
|
||||
final SyntaxTreePrinter syntaxTreePrinterInferred = new SyntaxTreePrinter(inferredTypesConstruct);
|
||||
|
||||
System.out.println("\n--Construct--");
|
||||
System.out.println("\nConstructed Interfaces:");
|
||||
constructedInterfaces.forEach(i-> i.accept(new SyntaxTreePrinter()));
|
||||
|
||||
List<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
|
||||
constructedInterfaces.forEach(i -> i.accept(syntaxTreePrinterInferred));
|
||||
printConstraints.printSubTypeConstraints(subTypeConstraints);
|
||||
|
||||
inferredTypes = construct.getInferredTypes();
|
||||
PrintInferredTypes.print(inferredTypes);
|
||||
|
||||
PrintInferredTypes.print(inferredTypesConstruct);
|
||||
System.out.println("\n--Inferred SysntaxTree--");
|
||||
sourceFile.accept(syntaxTreePrinterInferred);
|
||||
|
||||
System.out.println("____________________________________________________________________________");
|
||||
}
|
||||
}
|
||||
|
@ -20,13 +20,13 @@ public class TestStrucType {
|
||||
@org.junit.Test
|
||||
public void test() throws ClassNotFoundException, IOException {
|
||||
ArrayList<File> files = new ArrayList<>();
|
||||
// files.add(new File(rootDirectory + "testLocalVar.jav"));
|
||||
// files.add(new File(rootDirectory + "testCast.jav"));
|
||||
// files.add(new File(rootDirectory + "testNew.jav"));
|
||||
files.add(new File(rootDirectory + "testLocalVar.jav"));
|
||||
files.add(new File(rootDirectory + "testCast.jav"));
|
||||
files.add(new File(rootDirectory + "testNew.jav"));
|
||||
files.add(new File(rootDirectory + "testFieldVar.jav"));
|
||||
// files.add(new File(rootDirectory + "testFieldMethod.jav"));
|
||||
// files.add(new File(rootDirectory + "testMethod.jav"));
|
||||
// files.add(new File(rootDirectory + "testPaperExample.jav"));
|
||||
files.add(new File(rootDirectory + "testFieldMethod.jav"));
|
||||
files.add(new File(rootDirectory + "testMethod.jav"));
|
||||
files.add(new File(rootDirectory + "testPaperExample.jav"));
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(files);
|
||||
for (File f : compiler.sourceFiles.keySet()) {
|
||||
String name = f.getName();
|
||||
|
@ -15,18 +15,18 @@ public class TestSyntaxTreePrinter {
|
||||
@org.junit.Test
|
||||
public void test() throws ClassNotFoundException, IOException {
|
||||
ArrayList<File> files = new ArrayList<>();
|
||||
// files.add(new File(rootDirectory + "testLocalVar.jav"));
|
||||
// files.add(new File(rootDirectory + "testCast.jav"));
|
||||
files.add(new File(rootDirectory + "testLocalVar.jav"));
|
||||
files.add(new File(rootDirectory + "testCast.jav"));
|
||||
files.add(new File(rootDirectory + "testNew.jav"));
|
||||
// files.add(new File(rootDirectory + "testFieldVar.jav"));
|
||||
// files.add(new File(rootDirectory + "testFieldMethod.jav"));
|
||||
// files.add(new File(rootDirectory + "testPaperExample.jav"));
|
||||
files.add(new File(rootDirectory + "testFieldVar.jav"));
|
||||
files.add(new File(rootDirectory + "testFieldMethod.jav"));
|
||||
files.add(new File(rootDirectory + "testPaperExample.jav"));
|
||||
JavaTXCompiler compiler = new JavaTXCompiler(files);
|
||||
compiler.sourceFiles.keySet().forEach(f->{
|
||||
String name = f.getName();
|
||||
System.out.println("Filename: " + name);
|
||||
compiler.sourceFiles.get(f).accept(new SyntaxTreePrinter());
|
||||
System.out.println();
|
||||
System.out.println("________________________________________");
|
||||
});
|
||||
// this.printSyntaxTree(compiler);
|
||||
System.out.println("test end");
|
||||
|
@ -1,10 +1,13 @@
|
||||
package strucType.input;
|
||||
import strucType.typedtestclasses.A;
|
||||
import strucType.typedtestclasses.A2;
|
||||
import strucType.typedtestclasses.B;
|
||||
import strucType.typedtestclasses.C;
|
||||
|
||||
class C
|
||||
class Cast
|
||||
{
|
||||
mA(A x){return (A2)x; }
|
||||
mB(c) { return (B)c; }
|
||||
mA(A x){return (A2) x; }
|
||||
|
||||
mC(a) { return (C) mC2(a); }
|
||||
|
||||
mC2(c) { return (C) c; }
|
||||
}
|
@ -6,9 +6,11 @@ class M
|
||||
{
|
||||
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; }
|
||||
|
||||
|
||||
}
|
@ -1,10 +1,11 @@
|
||||
package strucType.input;
|
||||
import strucType.typedtestclasses.A;
|
||||
import strucType.typedtestclasses.B;
|
||||
class Neu
|
||||
class N
|
||||
{
|
||||
a;
|
||||
|
||||
mA(){ return new A(a); }
|
||||
mB(x){ return new B(x,a); }
|
||||
mN(a) {return new N(a); }
|
||||
}
|
Loading…
Reference in New Issue
Block a user