From 725b071d28c0962577e84df3b3a4ade3495db5ac Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 7 Mar 2018 08:47:43 +0100 Subject: [PATCH] Problem mit BinaryExpr gefixt --- .../dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java | 10 +++++++--- test/javFiles/AddLong.jav | 9 +++++++++ test/typeinference/JavaTXCompilerTest.java | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/javFiles/AddLong.jav diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 1fc2c3d8..8d87218d 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -205,6 +205,7 @@ 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)|| @@ -216,9 +217,12 @@ public class TYPEStmt implements StatementVisitor{ //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)); + /* + 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 + Das bedeutet, dass Java die Typen je nach belieben castet, so lange sie nur von Number erben + */ + numeric.add(new Pair(binary.getType(), number, 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) diff --git a/test/javFiles/AddLong.jav b/test/javFiles/AddLong.jav new file mode 100644 index 00000000..ceb31228 --- /dev/null +++ b/test/javFiles/AddLong.jav @@ -0,0 +1,9 @@ +import java.lang.Integer; +import java.lang.Long; + +public class AddLong{ + add(Integer a, Long b) { + Long c = a+b; + return c; + } +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index bf48a537..87551476 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -72,6 +72,10 @@ public class JavaTXCompilerTest { public void expressions() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Expressions.jav")); } + @Test + public void addLong() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"AddLong.jav")); + } private static class TestResultSet{