modified: ../../src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java

modified:   ../../test/typeinference/UnifyTest.java
This commit is contained in:
Martin Plümicke 2018-05-09 16:24:28 +02:00
parent 1c22fc7d57
commit da4c59f2cf
2 changed files with 44 additions and 8 deletions

View File

@ -184,7 +184,13 @@ public class TYPEStmt implements StatementVisitor{
receiver.expr.accept(this);
}
private final RefType number = new RefType(ASTFactory.createClass(Integer.class).getClassName(), new NullToken());
private final RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), new NullToken());
private final RefType longg = new RefType(ASTFactory.createClass(Long.class).getClassName(), new NullToken());
private final RefType integer = new RefType(ASTFactory.createClass(Integer.class).getClassName(), new NullToken());
private final RefType shortt = new RefType(ASTFactory.createClass(Short.class).getClassName(), new NullToken());
private final RefType bytee = new RefType(ASTFactory.createClass(Byte.class).getClassName(), new NullToken());
private final RefType floatt = new RefType(ASTFactory.createClass(Float.class).getClassName(), new NullToken());
private final RefType doublee = new RefType(ASTFactory.createClass(Double.class).getClassName(), new NullToken());
private final RefType string = new RefType(ASTFactory.createClass(String.class).getClassName(), new NullToken());
private final RefType bool = new RefType(ASTFactory.createClass(Boolean.class).getClassName(), new NullToken());
@Override
@ -205,18 +211,47 @@ public class TYPEStmt implements StatementVisitor{
@Override
public void visit(BinaryExpr binary) {
binary.lexpr.accept(this);
binary.rexpr.accept(this);
if(binary.operation.equals(BinaryExpr.Operator.DIV) ||
binary.operation.equals(BinaryExpr.Operator.MUL)||
binary.operation.equals(BinaryExpr.Operator.MOD)||
binary.operation.equals(BinaryExpr.Operator.ADD)){
Set<Constraint> numericAdditionOrStringConcatenation = new HashSet<>();
Constraint<Pair> numeric = new Constraint<>();
//Zuerst der Fall für Numerische AusdrücPairOpnumericeratorke, das sind Mul, Mod und Div immer:
//see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17
//Expression muss zu Numeric Convertierbar sein. also von Numeric erben
numeric.add(new Pair(binary.lexpr.getType(), number, PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.rexpr.getType(), number, PairOperator.SMALLERDOT));
Constraint<Pair> numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), bytee, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.rexpr.getType(), bytee, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(numeric);
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), shortt, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.rexpr.getType(), shortt, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(numeric);
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), integer, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.rexpr.getType(), integer, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.getType(), integer, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(numeric);
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), longg, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.rexpr.getType(), longg, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.getType(), longg, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(numeric);
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), floatt, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.rexpr.getType(), floatt, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.getType(), floatt, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(numeric);
numeric = new Constraint<>();
numeric.add(new Pair(binary.lexpr.getType(), doublee, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.rexpr.getType(), doublee, PairOperator.EQUALSDOT));
numeric.add(new Pair(binary.getType(), doublee, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(numeric);
/*
In Java passiert bei den binären Operatoren eine sogenannte Type Promotion:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2

View File

@ -51,11 +51,12 @@ public class UnifyTest {
execute(new File(rootDirectory+"Generics.jav"));
}
*/
/*
@Test
public void faculty() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Faculty.jav"));
}
*/
/*
@Test
public void facultyTyped() throws IOException, ClassNotFoundException {
@ -63,14 +64,14 @@ public class UnifyTest {
}
*/
/*
@Test
public void matrix() throws IOException, ClassNotFoundException {
execute(new File(rootDirectory+"Matrix.jav"));
//JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"Matrix.jav"));
//compiler.generateBytecode();
}
*/
/*
@Test
public void vector() throws IOException, ClassNotFoundException {