This commit is contained in:
Pluemicke Martin 2017-06-30 13:09:59 +02:00
commit bd1cfe4e7a
55 changed files with 879 additions and 321 deletions

View File

@ -27,7 +27,7 @@ import java.util.*;
public class JavaTXCompiler {
private List<SourceFile> sourceFiles = new ArrayList<>();
protected List<SourceFile> sourceFiles = new ArrayList<>();
public List<TypeInsert> getTypeInserts(File forFile){
ResultSet result = typeInference();

View File

@ -4,8 +4,6 @@ import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.antlr.Java8Parser;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.operator.AndOp;
import de.dhbwstuttgart.syntaxtree.operator.OrOp;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.*;
import de.dhbwstuttgart.syntaxtree.type.RefType;
@ -505,7 +503,7 @@ public class StatementGenerator {
return convert(expression.conditionalAndExpression());
}else{
return new Binary(convert(expression.conditionalOrExpression()),
convert(expression.conditionalAndExpression()), new OrOp(null));
convert(expression.conditionalAndExpression()), Binary.Operator.OR);
}
}
@ -514,7 +512,7 @@ public class StatementGenerator {
return convert(expression.inclusiveOrExpression());
}else{
return new Binary(convert(expression.conditionalAndExpression()),
convert(expression.inclusiveOrExpression()), new AndOp(null));
convert(expression.inclusiveOrExpression()), Binary.Operator.AND);
}
}
@ -821,7 +819,7 @@ public class StatementGenerator {
funNParams.add(TypePlaceholder.fresh(expression.getStart())));
RefTypeOrTPHOrWildcardOrGeneric lambdaType = TypePlaceholder.fresh(expression.getStart());
//RefType lambdaType = new RefType(reg.getName("Fun"+params.getFormalparalist().size()),
//funNParams, expression.getStart());
//funNParams, name.getStart());
return new LambdaExpression(lambdaType, params, block, expression.getStart());
}
}

View File

@ -180,7 +180,7 @@ public class Java8Parser extends Parser {
"methodInvocation_lf_primary", "methodInvocation_lfno_primary", "argumentList",
"methodReference", "methodReference_lf_primary", "methodReference_lfno_primary",
"arrayCreationExpression", "dimExprs", "dimExpr", "constantExpression",
"expression", "lambdaExpression", "lambdaParameters", "inferredFormalParameterList",
"name", "lambdaExpression", "lambdaParameters", "inferredFormalParameterList",
"lambdaBody", "assignmentExpression", "assignment", "leftHandSide", "assignmentOperator",
"conditionalExpression", "conditionalOrExpression", "conditionalAndExpression",
"inclusiveOrExpression", "exclusiveOrExpression", "andExpression", "equalityExpression",

View File

@ -0,0 +1,37 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
public interface ASTVisitor extends StatementVisitor{
void visit(SourceFile sourceFile);
void visit(ArgumentList argumentList);
void visit(GenericTypeVar genericTypeVar);
void visit(FormalParameter formalParameter);
void visit(GenericDeclarationList genericTypeVars);
void visit(Field field);
void visit(Method field);
void visit(ParameterList formalParameters);
void visit(ClassOrInterface classOrInterface);
void visit(RefType refType);
void visit(SuperWildcardType superWildcardType);
void visit(TypePlaceholder typePlaceholder);
void visit(ExtendsWildcardType extendsWildcardType);
void visit(GenericRefType genericRefType);
}

View File

@ -0,0 +1,145 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public abstract class AbstractASTWalker implements ASTVisitor{
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()){
cl.accept(this);
}
}
@Override
public void visit(ArgumentList argumentList) {
for(Expression expr : argumentList.getArguments()){
expr.accept(this);
}
}
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
}
}
}
@Override
public void visit(Field field) {
field.getType().accept(this);
}
@Override
public void visit(Method method) {
method.getType().accept(this);
method.getParameterList().accept(this);
method.block.accept(this);
}
@Override
public void visit(ParameterList formalParameters) {
Iterator<FormalParameter> genericIterator = formalParameters.getFormalparalist().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
}
}
}
@Override
public void visit(ClassOrInterface classOrInterface) {
classOrInterface.getGenerics().accept(this);
for(Field f : classOrInterface.getFieldDecl()){
f.accept(this);
}
for(Method m : classOrInterface.getMethods()){
m.accept(this);
}
}
@Override
public void visit(RefType refType) {
Iterator<RefTypeOrTPHOrWildcardOrGeneric> genericIterator = refType.getParaList().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
}
}
}
@Override
public void visit(SuperWildcardType superWildcardType) {
superWildcardType.getInnerType().accept(this);
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
extendsWildcardType.getInnerType().accept(this);
}
@Override
public void visit(LambdaExpression lambdaExpression) {
lambdaExpression.params.accept(this);
lambdaExpression.methodBody.accept(this);
}
@Override
public void visit(Assign assign) {
assign.lefSide.accept(this);
assign.rightSide.accept(this);
}
@Override
public void visit(Block block) {
for(Statement stmt : block.getStatements()){
stmt.accept(this);
}
}
@Override
public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
localVarDecl.getType().accept(this);
}
@Override
public void visit(MethodCall methodCall) {
methodCall.receiver.accept(this);
methodCall.getArgumentList().accept(this);
}
@Override
public void visit(NewClass methodCall) {
visit((MethodCall) methodCall);
}
@Override
public void visit(Receiver receiver) {
receiver.expr.accept(this);
}
@Override
public void visit(Return aReturn) {
aReturn.retexpr.accept(this);
}
}

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.core.IItemWithOffset;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.typecheck.JavaClassName;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
@ -15,13 +16,12 @@ import java.util.List;
/**
* Stellt jede Art von Klasse dar. Auch abstrakte Klassen und Interfaces
*/
public class ClassOrInterface implements IItemWithOffset{
public class ClassOrInterface extends SyntaxTreeNode {
protected int modifiers;
protected JavaClassName name;
private List<Field> fields = new ArrayList<>();
private List<Method> methods = new ArrayList<>();
private GenericDeclarationList genericClassParameters;
private Token offset;
private RefTypeOrTPHOrWildcardOrGeneric superClass;
protected boolean isInterface;
private List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces;
@ -29,12 +29,11 @@ public class ClassOrInterface implements IItemWithOffset{
public ClassOrInterface(int modifiers, JavaClassName name, List<Field> fielddecl, List<Method> methods, List<Constructor> constructors, GenericDeclarationList genericClassParameters,
RefTypeOrTPHOrWildcardOrGeneric superClass, Boolean isInterface, List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces, Token offset){
this.offset = offset;
super(offset);
this.modifiers = modifiers;
this.name = name;
this.fields = fielddecl;
this.genericClassParameters = genericClassParameters;
this.offset = offset;
this.superClass = superClass;
this.isInterface = isInterface;
this.implementedInterfaces = implementedInterfaces;
@ -88,7 +87,7 @@ public class ClassOrInterface implements IItemWithOffset{
}
@Override
public Token getOffset() {
return offset;
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -24,5 +24,10 @@ public class Field extends SyntaxTreeNode{
public RefTypeOrTPHOrWildcardOrGeneric getType() {
return type;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -21,4 +21,9 @@ public class FormalParameter extends SyntaxTreeNode
public String getName() {
return name;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -26,4 +26,9 @@ public class GenericDeclarationList extends SyntaxTreeNode implements Iterable<G
public Iterator<GenericTypeVar> iterator() {
return gtvs.iterator();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -51,4 +51,9 @@ public class GenericTypeVar extends SyntaxTreeNode
public GenericTypeName getName() {
return name;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -23,7 +23,7 @@ import de.dhbwstuttgart.syntaxtree.statement.Block;
*/
public class Method extends Field implements IItemWithOffset, TypeScope
{
private Block block;
public final Block block;
private ParameterList parameterlist = new ParameterList(new ArrayList<>(), new NullToken());
private ExceptionList exceptionlist;
private GenericDeclarationList generics;
@ -56,4 +56,9 @@ public class Method extends Field implements IItemWithOffset, TypeScope
public RefTypeOrTPHOrWildcardOrGeneric getReturnType() {
return this.getType();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -32,6 +32,11 @@ public class ParameterList extends SyntaxTreeNode implements Iterable<FormalPara
@Override
public Iterator<FormalParameter> iterator() {
return null;
return formalparameter.iterator();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -62,4 +62,9 @@ public class SourceFile extends SyntaxTreeNode{
public File getFile() {
return file;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -27,8 +27,6 @@ public interface StatementVisitor {
void visit(InstanceOf instanceOf);
void visit(InstVar instVar);
void visit(LocalVar localVar);
void visit(LocalVarDecl localVarDecl);

View File

@ -19,4 +19,5 @@ public abstract class SyntaxTreeNode implements IItemWithOffset{
return offset;
}
public abstract void accept(ASTVisitor visitor);
}

View File

@ -1,23 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.Constants;
import org.apache.bcel.generic.ArithmeticInstruction;
import org.apache.bcel.generic.DUP;
import org.apache.bcel.generic.IADD;
import org.apache.bcel.generic.InstructionList;
import org.apache.bcel.generic.InvokeInstruction;
import org.apache.bcel.generic.ObjectType;
public abstract class AddOp extends Operator
{
public AddOp(Token offset) {
super(offset);
}
}

View File

@ -1,18 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.generic.InstructionList;
public class AndOp extends LogOp
{
public AndOp(Token offset)
{
super(offset);
}
}

View File

@ -1,12 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class DivideOp extends MulOp
{
public DivideOp(Token offset) {
super(offset);
}
}

View File

@ -1,15 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
import org.apache.bcel.generic.BranchInstruction;
import org.apache.bcel.generic.IF_ICMPNE;
public class EqualOp extends RelOp
{
public EqualOp(Token offset) {
super(offset);
}
}

View File

@ -1,13 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class GreaterEquOp extends RelOp
{
public GreaterEquOp(Token offset) {
super(offset);
}
}

View File

@ -1,14 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class GreaterOp extends RelOp
{
public GreaterOp(Token offset) {
super(offset);
}
}

View File

@ -1,15 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class LessEquOp extends RelOp
{
public LessEquOp(Token offset) {
super(offset);
}
}

View File

@ -1,14 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class LessOp extends RelOp
{
public LessOp(Token offset)
{
super(offset);
}
}

View File

@ -1,18 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
public abstract class LogOp extends Operator
{
public LogOp(Token offset)
{
super(offset);
}
}

View File

@ -1,11 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class MinusOp extends AddOp
{
public MinusOp(Token offset) {
super(offset);
}
}

View File

@ -1,15 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class ModuloOp extends MulOp
{
public ModuloOp(Token offset)
{
super(offset);
}
}

View File

@ -1,10 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public abstract class MulOp extends Operator
{
public MulOp(Token offset) {
super(offset);
}
}

View File

@ -1,11 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class NotEqualOp extends RelOp
{
public NotEqualOp(Token offset) {
super(offset);
}
}

View File

@ -1,13 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public abstract class Operator extends SyntaxTreeNode
{
public Operator(Token offset) {
super(offset);
}
}

View File

@ -1,15 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class OrOp extends LogOp
{
public OrOp(Token offset)
{
super(offset);
}
}

View File

@ -1,12 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class PlusOp extends AddOp
{
public PlusOp(Token offset)
{
super(offset);
}
}

View File

@ -1,11 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public abstract class RelOp extends Operator
{
public RelOp(Token offset) {
super(offset);
}
}

View File

@ -1,11 +0,0 @@
package de.dhbwstuttgart.syntaxtree.operator;
import org.antlr.v4.runtime.Token;
public class TimesOp extends MulOp
{
public TimesOp(Token offset)
{
super(offset);
}
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
@ -19,4 +20,8 @@ public class ArgumentList extends SyntaxTreeNode
return expr;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -6,12 +6,16 @@ import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import de.dhbwstuttgart.syntaxtree.operator.Operator;
// neu von Felix
public class Binary extends Expression
{
public enum Operator{
ADD,
SUB,
MUL,
AND, OR, DIV
}
public Binary(Expression expr1, Expression expr2, Operator op){
super(null, null);
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -24,5 +25,11 @@ public abstract class Expression extends SyntaxTreeNode
public abstract void accept(StatementVisitor visitor);
@Override
public void accept(ASTVisitor visitor) {
this.accept((StatementVisitor)visitor);
}
public String toString() { return this.getClass().getName() + ":" + type.toString(); }
}

View File

@ -1,24 +0,0 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
public class InstVar extends Expression
{
private Expression expr;
public InstVar(Expression instanz, String name, Token offset)
{
super(instanz.getType(),offset);
expr = instanz;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,28 +1,23 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;
import de.dhbwstuttgart.typeinference.constraints.ConstraintSet;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation;
import org.antlr.v4.runtime.Token;
public class LocalVar extends Statement{
protected final String expression;
public final String name;
public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset)
{
super(type,offset);
this.expression = n;
this.name = n;
}
public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access)
{
super(type,e1.getOffset());
this.expression = access;
this.name = access;
}
@Override

View File

@ -31,7 +31,7 @@ public class NewClass extends MethodCall
* @param start
*/
public NewClass(RefType newClass, ArgumentList args, Token start) {
super(newClass, new Receiver(new EmptyStmt(start)), "new "+newClass.getName().toString(), args, start);
super(newClass, new Receiver(new EmptyStmt(start)), newClass.getName().toString(), args, start);
}
@Override

View File

@ -1,7 +1,6 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.operator.Operator;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation;

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import org.antlr.v4.runtime.Token;
/**
@ -33,4 +34,8 @@ public class ExtendsWildcardType extends WildcardType{
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typecheck.GenericTypeName;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.antlr.v4.runtime.Token;
@ -20,5 +21,10 @@ public class GenericRefType extends RefTypeOrTPHOrWildcardOrGeneric
public String getUniqueIdentifier(){
return name.toString();
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.typecheck.JavaClassName;
import org.antlr.v4.runtime.Token;
@ -100,5 +101,9 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,10 +1,14 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import org.antlr.v4.runtime.Token;
public class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
public abstract class RefTypeOrTPHOrWildcardOrGeneric extends SyntaxTreeNode{
public RefTypeOrTPHOrWildcardOrGeneric(Token offset) {
super(offset);
}
@Override
public abstract void accept(ASTVisitor visitor);
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import org.antlr.v4.runtime.Token;
/**
@ -42,4 +43,9 @@ public class SuperWildcardType extends WildcardType{
public boolean isSuper() {
return true;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree.type;
import java.util.Hashtable;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
import org.antlr.v4.runtime.Token;
@ -65,4 +66,9 @@ public class TypePlaceholder extends RefTypeOrTPHOrWildcardOrGeneric
public String getName() {
return name;
}
@Override
public void accept(ASTVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,13 @@
package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.syntaxtree.*;
public class ASTPrinter {
public static String print(SourceFile toPrint){
StringBuilder output = new StringBuilder();
new OutputGenerator(output).visit(toPrint);
return output.toString();
}
}

View File

@ -0,0 +1,20 @@
package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public class ASTTypePrinter extends ASTPrinter{
public static String print(SourceFile toPrint){
StringBuilder output = new StringBuilder();
new TypeOutputGenerator(output).visit(toPrint);
return output.toString();
}
}

View File

@ -0,0 +1,307 @@
package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public class OutputGenerator implements ASTVisitor {
private static final String TAB = " ";
String tabs = "";
protected final StringBuilder out;
public OutputGenerator(StringBuilder out){
this.out = out;
}
public void tab() {
tabs += TAB;
}
public void untab() {
tabs = tabs.substring(0,tabs.length()-TAB.length());
}
@Override
public void visit(SourceFile sourceFile) {
for(ClassOrInterface cl : sourceFile.getClasses()){
cl.accept(this);
}
}
@Override
public void visit(ArgumentList argumentList) {
out.append("(");
Iterator<Expression> expressionIterator = argumentList.getArguments().iterator();
while(expressionIterator.hasNext()){
expressionIterator.next().accept(this);
if(expressionIterator.hasNext())out.append(", ");
}
out.append(")");
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
out.append(genericTypeVar.getName().toString());
}
@Override
public void visit(FormalParameter formalParameter) {
formalParameter.getType().accept(this);
out.append(" ");
out.append(formalParameter.getName());
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
Iterator<GenericTypeVar> genericIterator = genericTypeVars.iterator();
if(genericIterator.hasNext()){
out.append("<");
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
if(genericIterator.hasNext())out.append(", ");
}
out.append(">");
}
}
@Override
public void visit(Field field) {
field.getType().accept(this);
out.append(" ");
out.append(field.getName());
out.append(";");
}
@Override
public void visit(Method method) {
method.getType().accept(this);
out.append(" " + method.getName());
method.getParameterList().accept(this);
method.block.accept(this);
out.append("\n");
}
@Override
public void visit(ParameterList formalParameters) {
out.append("(");
Iterator<FormalParameter> genericIterator = formalParameters.getFormalparalist().iterator();
if(genericIterator.hasNext()){
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
if(genericIterator.hasNext())out.append(", ");
}
}
out.append(")");
}
@Override
public void visit(ClassOrInterface classOrInterface) {
if((Modifier.INTERFACE & classOrInterface.getModifiers()) == 1){
out.append("interface ");
}else{
out.append("class ");
}
out.append(classOrInterface.getClassName().toString());
classOrInterface.getGenerics().accept(this);
out.append(" {\n\n");
tab();
for(Field f : classOrInterface.getFieldDecl()){
out.append(tabs);
f.accept(this);
out.append("\n");
}
for(Method m : classOrInterface.getMethods()){
out.append(tabs);
m.accept(this);
out.append("\n");
}
untab();
out.append("}");
}
@Override
public void visit(RefType refType) {
out.append(refType.getName().toString());
Iterator<RefTypeOrTPHOrWildcardOrGeneric> genericIterator = refType.getParaList().iterator();
if(genericIterator.hasNext()){
out.append("<");
while(genericIterator.hasNext()){
genericIterator.next().accept(this);
if(genericIterator.hasNext())out.append(", ");
}
out.append(">");
}
}
@Override
public void visit(SuperWildcardType superWildcardType) {
out.append("? super ");
superWildcardType.getInnerType().accept(this);
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
out.append("TPH "+ typePlaceholder.getName());
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
out.append("? extends ");
extendsWildcardType.getInnerType().accept(this);
}
@Override
public void visit(GenericRefType genericRefType) {
out.append(genericRefType.getName().toString());
}
@Override
public void visit(LambdaExpression lambdaExpression) {
lambdaExpression.params.accept(this);
out.append(" -> ");
lambdaExpression.methodBody.accept(this);
}
@Override
public void visit(Assign assign) {
assign.lefSide.accept(this);
out.append(" = ");
assign.rightSide.accept(this);
}
@Override
public void visit(Binary binary) {
}
@Override
public void visit(Block block) {
tab();
out.append("{\n");
for(Statement stmt : block.getStatements()){
out.append(tabs);
stmt.accept(this);
out.append(";\n");
}
untab();
out.append(tabs);
out.append("}");
}
@Override
public void visit(CastExpr castExpr) {
}
@Override
public void visit(EmptyStmt emptyStmt) {
}
@Override
public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this);
out.append("." + fieldVar.fieldVarName);
}
@Override
public void visit(ForStmt forStmt) {
}
@Override
public void visit(IfStmt ifStmt) {
}
@Override
public void visit(InstanceOf instanceOf) {
}
@Override
public void visit(LocalVar localVar) {
out.append(localVar.name);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
localVarDecl.getType().accept(this);
out.append(" " + localVarDecl.getName());
}
@Override
public void visit(MethodCall methodCall) {
methodCall.receiver.accept(this);
out.append("."+methodCall.name);
methodCall.getArgumentList().accept(this);
}
@Override
public void visit(NewClass methodCall) {
out.append("new ");
out.append(methodCall.name);
methodCall.getArgumentList().accept(this);
}
@Override
public void visit(NewArray newArray) {
}
@Override
public void visit(Receiver receiver) {
receiver.expr.accept(this);
}
@Override
public void visit(Return aReturn) {
out.append("return ");
aReturn.retexpr.accept(this);
}
@Override
public void visit(ReturnVoid aReturn) {
out.append("return");
}
@Override
public void visit(StaticClassName staticClassName) {
}
@Override
public void visit(Super aSuper) {
}
@Override
public void visit(This aThis) {
out.append("this");
}
@Override
public void visit(UnaryPlus unaryPlus) {
}
@Override
public void visit(WhileStmt whileStmt) {
}
@Override
public void visit(Null aNull) {
}
@Override
public void visit(Literal literal) {
}
}

View File

@ -0,0 +1,236 @@
package de.dhbwstuttgart.syntaxtree.visual;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.*;
import java.lang.reflect.Modifier;
import java.util.Iterator;
public class TypeOutputGenerator extends OutputGenerator {
TypeOutputGenerator(StringBuilder out){
super(out);
}
@Override
public void visit(SourceFile sourceFile) {
super.visit(sourceFile);
}
@Override
public void visit(ArgumentList argumentList) {
super.visit(argumentList);
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
super.visit(genericTypeVar);
}
@Override
public void visit(FormalParameter formalParameter) {
super.visit(formalParameter);
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
super.visit(genericTypeVars);
}
@Override
public void visit(Field field) {
super.visit(field);
}
@Override
public void visit(Method method) {
super.visit(method);
}
@Override
public void visit(ParameterList formalParameters) {
super.visit(formalParameters);
}
@Override
public void visit(ClassOrInterface classOrInterface) {
super.visit(classOrInterface);
}
@Override
public void visit(RefType refType) {
super.visit(refType);
}
@Override
public void visit(SuperWildcardType superWildcardType) {
super.visit(superWildcardType);
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
super.visit(typePlaceholder);
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
super.visit(extendsWildcardType);
}
@Override
public void visit(GenericRefType genericRefType) {
super.visit(genericRefType);
}
@Override
public void visit(LambdaExpression lambdaExpression) {
out.append("(");
super.visit(lambdaExpression);
out.append(")");
this.out.append("::");
lambdaExpression.getType().accept(this);
}
@Override
public void visit(Assign assign) {
super.visit(assign);
}
@Override
public void visit(Binary binary) {
super.visit(binary);
}
@Override
public void visit(Block block) {
out.append("(");
super.visit(block);
out.append(")");
this.out.append("::");
block.getType().accept(this);
}
@Override
public void visit(CastExpr castExpr) {
super.visit(castExpr);
}
@Override
public void visit(EmptyStmt emptyStmt) {
super.visit(emptyStmt);
}
@Override
public void visit(FieldVar fieldVar) {
out.append("(");
super.visit(fieldVar);
out.append(")");
this.out.append("::");
fieldVar.getType().accept(this);
}
@Override
public void visit(ForStmt forStmt) {
super.visit(forStmt);
}
@Override
public void visit(IfStmt ifStmt) {
super.visit(ifStmt);
}
@Override
public void visit(InstanceOf instanceOf) {
super.visit(instanceOf);
}
@Override
public void visit(LocalVar localVar) {
out.append("(");
super.visit(localVar);
out.append(")");
this.out.append("::");
localVar.getType().accept(this);
}
@Override
public void visit(LocalVarDecl localVarDecl) {
super.visit(localVarDecl);
}
@Override
public void visit(MethodCall methodCall) {
out.append("(");
super.visit(methodCall);
out.append(")");
this.out.append("::");
methodCall.getType().accept(this);
}
@Override
public void visit(NewClass methodCall) {
super.visit(methodCall);
}
@Override
public void visit(NewArray newArray) {
super.visit(newArray);
}
@Override
public void visit(Receiver receiver) {
super.visit(receiver);
}
@Override
public void visit(Return aReturn) {
super.visit(aReturn);
}
@Override
public void visit(ReturnVoid aReturn) {
super.visit(aReturn);
}
@Override
public void visit(StaticClassName staticClassName) {
super.visit(staticClassName);
}
@Override
public void visit(Super aSuper) {
super.visit(aSuper);
}
@Override
public void visit(This aThis) {
out.append("(");
super.visit(aThis);
out.append(")");
this.out.append("::");
aThis.getType().accept(this);
}
@Override
public void visit(UnaryPlus unaryPlus) {
super.visit(unaryPlus);
}
@Override
public void visit(WhileStmt whileStmt) {
super.visit(whileStmt);
}
@Override
public void visit(Null aNull) {
super.visit(aNull);
}
@Override
public void visit(Literal literal) {
super.visit(literal);
}
}

View File

@ -128,10 +128,11 @@ public class TypeInsertFactory {
//Offset zum Einstzen bestimmen:
Token offset;
String insert = "";
String end =" ";
String end;
if(cl.getGenerics().iterator().hasNext()){
offset = cl.getGenerics().iterator().next().getOffset();
insert+=",";
//offset = cl.getGenerics().iterator().next().getOffset();
offset = cl.getGenerics().getOffset();
end=",";
}else{
offset = cl.getGenerics().getOffset();
insert += "<";
@ -154,6 +155,7 @@ public class TypeInsertFactory {
//String zum Einsetzen (Generics mit bounds) generieren:
Iterator<TypePlaceholder> it = genericsAndBounds.keySet().iterator();
if(! it.hasNext())return new TypeInsertPoint(offset, "");
while(it.hasNext()){
TypePlaceholder tph = it.next();
insert += tph.getName();

View File

@ -11,7 +11,7 @@ public class TypeInsertPoint {
public TypeInsertPoint(Token point, String toInsert){
this.point = point;
this.insertString = toInsert + " ";
this.insertString = (toInsert.length()>1) ? toInsert + " " : toInsert;
}
public String insert(String intoSource, List<TypeInsertPoint> additionalOffset){

View File

@ -118,11 +118,6 @@ public class TYPE implements StatementVisitor{
throw new NotImplementedException();
}
@Override
public void visit(InstVar instVar) {
throw new NotImplementedException();
}
@Override
public void visit(LocalVar localVar) {
// Es werden nur bei Feldvariablen Constraints generiert. Lokale Variablen sind eindeutig

View File

@ -1,9 +1,9 @@
class Faculty {
Integer mul(Integer x, Integer y) {
Integer mul(Integer x, Integer y) {
return x;
}
m () {
auto fact = (Integer x) -> {
return mul(x, fact.apply(x));

View File

@ -3,6 +3,10 @@ package typeinference;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.ClassNotFoundException;
import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator;
import de.dhbwstuttgart.typedeployment.TypeInsert;
import de.dhbwstuttgart.typedeployment.TypeInsertPoint;
import de.dhbwstuttgart.typeinference.ResultSet;
@ -19,7 +23,7 @@ import java.util.List;
import static org.junit.Assert.*;
public class JavaTXCompilerTest {
public class JavaTXCompilerTest extends JavaTXCompiler {
private static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/";
private static final List<File> filesToTest = new ArrayList<>();
@ -35,10 +39,10 @@ public class JavaTXCompilerTest {
//filesToTest.add(new File(rootDirectory+"Generics.jav"));
//filesToTest.add(new File(rootDirectory+"MethodsEasy.jav"));
//filesToTest.add(new File(rootDirectory+"Matrix.jav"));
JavaTXCompiler compiler = new JavaTXCompiler();
for(File f : filesToTest){
compiler.parse(f);
List<TypeInsert> result = compiler.getTypeInserts(f);
this.parse(f);
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
List<TypeInsert> result = this.getTypeInserts(f);
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
for(TypeInsert tip : result){
System.out.println(tip.insert(content));
@ -53,4 +57,5 @@ public class JavaTXCompilerTest {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
}