TypeStmt für +,-,/,*,% einführen

This commit is contained in:
JanUlrich 2018-02-28 14:50:16 +01:00
parent 77aaa0ecb6
commit f023754328
2 changed files with 45 additions and 17 deletions

View File

@ -13,16 +13,17 @@ public class BinaryExpr extends Expression
}
public enum Operator{
ADD,
SUB,
MUL,
AND,
OR,
DIV,
LESSTHAN,
BIGGERTHAN,
LESSEQUAL,
BIGGEREQUAL
ADD, // +
SUB, // -
MUL, // *
MOD, // Modulo Operator %
AND, // &&
OR, // ||
DIV, // /
LESSTHAN, // <
BIGGERTHAN, // >
LESSEQUAL, // <=
BIGGEREQUAL // >=
}
public final Operator operation;

View File

@ -81,11 +81,6 @@ public class TYPEStmt implements StatementVisitor{
assign.rightSide.getType(), assign.lefSide.getType(), PairOperator.SMALLERDOT));
}
@Override
public void visit(BinaryExpr binary) {
//TODO
}
@Override
public void visit(Block block) {
for(Statement stmt : block.getStatements()){
@ -190,6 +185,8 @@ public class TYPEStmt implements StatementVisitor{
receiver.expr.accept(this);
}
private final RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), new NullToken());
private final RefType string = new RefType(ASTFactory.createClass(String.class).getClassName(), new NullToken());
@Override
public void visit(UnaryExpr unaryExpr) {
if(unaryExpr.operation == UnaryExpr.Operation.POSTDECREMENT ||
@ -198,7 +195,6 @@ public class TYPEStmt implements StatementVisitor{
unaryExpr.operation == UnaryExpr.Operation.PREINCREMENT){
//@see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.14.2
//Expression muss zu Numeric Convertierbar sein. also von Numeric erben
RefType number = new RefType(ASTFactory.createClass(Number.class).getClassName(), unaryExpr.getOffset());
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.SMALLERDOT));
//The type of the postfix increment expression is the type of the variable
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT));
@ -207,9 +203,40 @@ public class TYPEStmt implements StatementVisitor{
}
}
@Override
public void visit(BinaryExpr binary) {
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));
//The type of a multiplicative expression is the promoted type of its operands.
numeric.add(new Pair(binary.rexpr.getType(), binary.getType(), PairOperator.SMALLERDOT));
numeric.add(new Pair(binary.lexpr.getType(), binary.getType(), PairOperator.SMALLERDOT));
numericAdditionOrStringConcatenation.add(numeric);
if(binary.operation.equals(BinaryExpr.Operator.ADD)) {
//Dann kann der Ausdruck auch das aneinanderfügen zweier Strings sein: ("a" + "b") oder (1 + 2)
Constraint<Pair> stringConcat = new Constraint<>();
stringConcat.add(new Pair(binary.lexpr.getType(), string, PairOperator.EQUALSDOT));
stringConcat.add(new Pair(binary.rexpr.getType(), string, PairOperator.EQUALSDOT));
stringConcat.add(new Pair(binary.getType(), string, PairOperator.EQUALSDOT));
numericAdditionOrStringConcatenation.add(stringConcat);
}
constraintsSet.addOderConstraint(numericAdditionOrStringConcatenation);
}else {
throw new NotImplementedException();
}
}
@Override
public void visit(Literal literal) {
//Nothing to do here. Literale kriegen beim parsen den korrekten Typ.
//Nothing to do here. Literale erzeugen keine Constraints
}
@Override