LocalVar bekommt den Typ beim Parsen

This commit is contained in:
JanUlrich 2017-05-19 17:40:47 +02:00
parent 4a52bc0e6e
commit 810164ac17
8 changed files with 90 additions and 26 deletions

View File

@ -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<String> localVars = new HashSet<>();
private Map<String, RefTypeOrTPHOrWildcardOrGeneric> 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());

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

43
test/javFiles/Matrix.jav Normal file
View File

@ -0,0 +1,43 @@
class Matrix extends Menge<Menge<Integer>> {
Matrix mul_rec(Matrix m) {
v1;
v1 = new Menge<Integer>();
v2;
v2 = new Menge<Integer>();
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<Integer>());
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;
}
}

View File

@ -10,7 +10,7 @@ class Methods {
}
class Test {
f;
java.lang.Object f;
add(){}
add(b){return b;}
sub(b){}

11
test/javFiles/Vector.jav Normal file
View File

@ -0,0 +1,11 @@
class Vector{
id(x){
return id2(x);
}
id2(x){
return id(x);
}
}

View File

@ -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<TypeInsertPoint> result = compiler.getTypeInserts(new File(rootDirectory+"Methods.jav"));
String content = readFile(rootDirectory+"Methods.jav", StandardCharsets.UTF_8);
List<TypeInsertPoint> 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));
}