From 8759a9cc5d32d0f183d76a29a2f61638d26d8a6f Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Thu, 30 Aug 2018 00:02:17 +0200 Subject: [PATCH] IfStmt Constraints erstellen --- .../SyntaxTreeGenerator/SyntaxTreeGenerator.java | 4 +--- .../syntaxtree/visual/OutputGenerator.java | 15 ++++++++++++++- .../typeinference/typeAlgo/TYPEStmt.java | 15 ++++++++++++++- test/bytecode/javFiles/IfTest.jav | 6 ++++-- test/javFiles/IfTest.jav | 15 +++++++++++++++ test/typeinference/JavaTXCompilerTest.java | 4 ++++ 6 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 test/javFiles/IfTest.jav diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 981bf7b9..73b0e55f 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -349,9 +349,7 @@ public class SyntaxTreeGenerator{ if(varCtx.variableInitializer() != null){ initializeField(varCtx, fieldType, generics); } - else{ - ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); - } + ret.add(new Field(fieldName,fieldType,modifiers,varCtx.getStart())); } return ret; } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 0c623548..3dfca38a 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -231,7 +231,20 @@ public class OutputGenerator implements ASTVisitor{ @Override public void visit(IfStmt ifStmt) { - + out.append("if("); + ifStmt.expr.accept(this); + out.append(")\n"); + tab(); + out.append(tabs); + ifStmt.then_block.accept(this); + untab(); + if(ifStmt.else_block != null){ + out.append("\n" + tabs + "else\n"); + tab(); + out.append(tabs); + ifStmt.else_block.accept(this); + untab(); + } } @Override diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 32a39b7d..32dc72ef 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -122,7 +122,20 @@ public class TYPEStmt implements StatementVisitor{ @Override public void visit(IfStmt ifStmt) { - throw new NotImplementedException(); + RefType booleanType = new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()); + //Expression inferieren: + ifStmt.expr.accept(this); + //Expression muss boolean sein: + constraintsSet.addUndConstraint(new Pair(ifStmt.expr.getType(), booleanType, PairOperator.EQUALSDOT)); + //Blöcke inferieren: + ifStmt.then_block.accept(this); + //Beide Blöcke müssen den gleichen Supertyp haben, welcher den Rückgabetyp des If-Stmts darstellt + constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT)); + if(ifStmt.else_block != null){ + ifStmt.else_block.accept(this); + constraintsSet.addUndConstraint(new Pair(ifStmt.else_block.getType(), ifStmt.getType(), PairOperator.SMALLERDOT)); + } + } @Override diff --git a/test/bytecode/javFiles/IfTest.jav b/test/bytecode/javFiles/IfTest.jav index 33f9ecba..bbcda96c 100644 --- a/test/bytecode/javFiles/IfTest.jav +++ b/test/bytecode/javFiles/IfTest.jav @@ -1,13 +1,15 @@ import java.lang.Integer; import java.lang.Boolean; +import java.lang.String; public class IfTest{ Integer m1(Boolean b) { Integer i; + String b; if(b) { return i; + }else{ + return b; } - - return i; } } \ No newline at end of file diff --git a/test/javFiles/IfTest.jav b/test/javFiles/IfTest.jav new file mode 100644 index 00000000..3a09707b --- /dev/null +++ b/test/javFiles/IfTest.jav @@ -0,0 +1,15 @@ +import java.lang.Integer; +import java.lang.Boolean; +import java.lang.Object; + +public class IfTest{ + Object m1(Boolean b) { + Integer i; + Boolean b; + if(b) { + return i; + }else{ + return b; + } + } +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index d3401a0b..df181338 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -92,6 +92,10 @@ public class JavaTXCompilerTest { public void fields() throws IOException, ClassNotFoundException { execute(new File(rootDirectory+"Fields.jav")); } + @Test + public void ifStatement() throws IOException, ClassNotFoundException { + execute(new File(rootDirectory+"IfTest.jav")); + } private static class TestResultSet{