From f023754328320f0a46a6725b0b14716541757612 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 28 Feb 2018 14:50:16 +0100 Subject: [PATCH] =?UTF-8?q?TypeStmt=20f=C3=BCr=20+,-,/,*,%=20einf=C3=BChre?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../syntaxtree/statement/BinaryExpr.java | 21 +++++----- .../typeinference/typeAlgo/TYPEStmt.java | 41 +++++++++++++++---- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java index f7db0481..bb631e04 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/BinaryExpr.java @@ -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; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 3733b7dc..e6b82a10 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -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 numericAdditionOrStringConcatenation = new HashSet<>(); + Constraint 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 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