diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 9ad4f227..409b4d4f 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -17,15 +17,12 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.TerminalNode; import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; public class StatementGenerator { private JavaClassRegistry reg; - private Set localVars = new HashSet<>(); + private Map localVars = new HashMap<>(); private GenericsRegistry generics; public StatementGenerator(JavaClassRegistry reg, GenericsRegistry generics){ @@ -56,7 +53,7 @@ public class StatementGenerator { type = TypePlaceholder.fresh(fp.getStart()); } ret.add(new FormalParameter(paramName, type, fp.getStart())); - localVars.add(paramName); + localVars.put(paramName, type); } return new ParameterList(ret, ret.get(0).getOffset()); } @@ -232,8 +229,8 @@ public class StatementGenerator { String[] parts = expression.split("\\."); if(parts.length < 2){ //Check for localVar: - if(localVars.contains(expression)){ - return new LocalVar(expression, offset); + if(localVars.get(expression) != null){ + return new LocalVar(expression, localVars.get(expression), offset); }else{ //throw new NotImplementedException(); //Dann Muss es ein Feld sein! @@ -295,12 +292,12 @@ public class StatementGenerator { } private Statement convert(Java8Parser.AssignmentContext stmt) { - LocalVar leftHandSide = convert(stmt.leftHandSide()); + Expression leftHandSide = convert(stmt.leftHandSide()); return new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart()); } - private LocalVar convert(Java8Parser.LeftHandSideContext leftHandSide) { - return new LocalVar(leftHandSide.toString(), leftHandSide.getStart()); + private Expression convert(Java8Parser.LeftHandSideContext leftHandSide) { + return generateLocalOrFieldVarOrClassName(leftHandSide.getText(), leftHandSide.getStart()); } private Statement convert(Java8Parser.IfThenStatementContext stmt){ @@ -401,7 +398,7 @@ public class StatementGenerator { for(Java8Parser.VariableDeclaratorContext varDecl : declaration.variableDeclaratorList().variableDeclarator()){ TerminalNode name = varDecl.variableDeclaratorId().Identifier(); declarations.add(new LocalVarDecl(name.getText(), type, name.getSymbol())); - this.localVars.add(name.getText()); + this.localVars.put(name.getText(), type); } if(declarations.size()==1)return declarations.get(0); return new LocalVarBunchDeclaration(declarations, declaration.getStart()); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java index e5827e02..471093fb 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Assign.java @@ -12,9 +12,9 @@ import org.antlr.v4.runtime.Token; public class Assign extends Statement { public final Expression rightSide; - public final LocalVar lefSide; + public final Expression lefSide; - public Assign(LocalVar leftHandSide, Expression value, Token offset) { + public Assign(Expression leftHandSide, Expression value, Token offset) { super(leftHandSide.getType(), offset); this.rightSide = value; this.lefSide = leftHandSide; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java index ea2b5bea..3c703a6f 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/LocalVar.java @@ -2,6 +2,7 @@ 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; @@ -12,15 +13,15 @@ public class LocalVar extends Statement{ protected final String expression; - public LocalVar(String n, Token offset) + public LocalVar(String n, RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { - super(TypePlaceholder.fresh(offset),offset); + super(type,offset); this.expression = n; } - public LocalVar(Expression e1, String access) + public LocalVar(Expression e1, RefTypeOrTPHOrWildcardOrGeneric type, String access) { - super(TypePlaceholder.fresh(e1.getOffset()),e1.getOffset()); + super(type,e1.getOffset()); this.expression = access; } diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 9933195f..4d4b2161 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -1,9 +1,7 @@ package de.dhbwstuttgart.typedeployment; import de.dhbwstuttgart.exceptions.NotImplementedException; -import de.dhbwstuttgart.syntaxtree.ClassOrInterface; -import de.dhbwstuttgart.syntaxtree.Field; -import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; @@ -24,6 +22,19 @@ public class TypeInsertFactory { ret.add(new TypeInsertPoint(field.getType().getOffset(), toInsert)); } } + + for(Method m : cl.getMethods()){ + RefTypeOrTPHOrWildcardOrGeneric resolved = withResults.resolveType(m.getReturnType()); + String toInsert = getString(resolved) + " "; + ret.add(new TypeInsertPoint(m.getReturnType().getOffset(), toInsert)); + + for(FormalParameter param : m.getParameterList().getFormalparalist()){ + resolved = withResults.resolveType(param.getType()); + toInsert = getString(resolved) + " "; + ret.add(new TypeInsertPoint(param.getType().getOffset(), toInsert)); + + } + } } return ret; } diff --git a/test/javFiles/Matrix.jav b/test/javFiles/Matrix.jav new file mode 100644 index 00000000..7096ba0c --- /dev/null +++ b/test/javFiles/Matrix.jav @@ -0,0 +1,43 @@ +class Matrix extends Menge> { + + Matrix mul_rec(Matrix m) { + v1; + v1 = new Menge(); + v2; + v2 = new Menge(); + i; + i = 0; + while(i < m.size()) { + v; + v = m.elementAt(i); + v2.addElement(v.remove(v.size()-1)); + i++; + } + Matrix ret; + if (m.elementAt(0).size() > 0) { + ret = this.mul_rec(m); + } + else { + ret = new Matrix(); + i = 0; + while (i < this.size()) { + ret.addElement(new Menge()); + i++; + } + } + i = 0; + while (i < this.size()) { + int erg = 0; + j; + j = 0; + while (j < v2.size()) { + erg = erg + this.elementAt(i).elementAt(j).intValue() + * v2.elementAt(j).intValue(); + j++; + } + ret.elementAt(i).addElement(erg); + i++; + } + return ret; + } +} \ No newline at end of file diff --git a/test/javFiles/Methods.jav b/test/javFiles/Methods.jav index 7ecd4d3f..e45d9830 100644 --- a/test/javFiles/Methods.jav +++ b/test/javFiles/Methods.jav @@ -10,7 +10,7 @@ class Methods { } class Test { - f; + java.lang.Object f; add(){} add(b){return b;} sub(b){} diff --git a/test/javFiles/Vector.jav b/test/javFiles/Vector.jav new file mode 100644 index 00000000..f2a9cdae --- /dev/null +++ b/test/javFiles/Vector.jav @@ -0,0 +1,11 @@ + +class Vector{ + +id(x){ + return id2(x); +} + +id2(x){ +return id(x); +} +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index f6b46a0c..b65828e3 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -25,13 +25,14 @@ public class JavaTXCompilerTest { @Test public void test() throws IOException, ClassNotFoundException { JavaTXCompiler compiler = new JavaTXCompiler(); - compiler.parse(new File(rootDirectory+"Methods.jav")); + compiler.parse(new File(rootDirectory+"Vector.jav")); //compiler.parse(new File(rootDirectory+"Generics.jav")); - compiler.parse(new File(rootDirectory+"MethodsEasy.jav")); + //compiler.parse(new File(rootDirectory+"MethodsEasy.jav")); //compiler.parse(new File(rootDirectory+"Lambda.jav")); + //compiler.parse(new File(rootDirectory+"Matrix.jav")); - List result = compiler.getTypeInserts(new File(rootDirectory+"Methods.jav")); - String content = readFile(rootDirectory+"Methods.jav", StandardCharsets.UTF_8); + List result = compiler.getTypeInserts(new File(rootDirectory+"Vector.jav")); + String content = readFile(rootDirectory+"Vector.jav", StandardCharsets.UTF_8); for(TypeInsertPoint tip : result){ System.out.println(tip.insert(content)); }