From b88b9e2534f6bcfc2ec59c15c3680071a7d2f0ab Mon Sep 17 00:00:00 2001 From: FelixKrespach Date: Thu, 21 Sep 2017 22:36:13 +0200 Subject: [PATCH] methods in statementgenerator implemented Tests + visit --- .../StatementGenerator.java | 18 +++++++++++--- .../syntaxtree/statement/AssertStmt.java | 22 +++++++++++++++++ .../statement/CatchesStatement.java | 9 +++++++ .../syntaxtree/visual/OutputGenerator.java | 6 ++++- test/parser/IfTest.jav | 24 +++++++++++++++++++ test/typeinference/JavaTXCompilerTest.java | 3 ++- 6 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java create mode 100644 test/parser/IfTest.jav diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 655da3b1..45965d6f 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; +import com.sun.javafx.fxml.expression.UnaryExpression; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.antlr.Java8Parser; @@ -278,6 +279,7 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { + Expression expr = convert(stmt.postfixExpression()); //TODO throw new NotImplementedException(); } @@ -320,9 +322,19 @@ public class StatementGenerator { return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } - private Statement convert(Java8Parser.AssertStatementContext stmt){ - //TODO - throw new NotImplementedException(); + private Statement convert(Java8Parser.AssertStatementContext stmt) + { + if (stmt.expression().size()<2) + { + Expression expr = convert(stmt.expression(0)); + return new AssertStmt(expr,stmt.getStart()); + } + else { + Expression expr1 = convert(stmt.expression(0)); + Expression expr2 = convert(stmt.expression(1)); + return new AssertStmt(expr1,expr2,stmt.getStart()); + } + //TODO: Check Felix_K } private Statement convert(Java8Parser.SwitchStatementContext stmt){ diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java new file mode 100644 index 00000000..8603045b --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java @@ -0,0 +1,22 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +/** + * Created by Felix_K on 21.09.2017. + */ +public class AssertStmt extends Statement +{ + final Expression expr; + public AssertStmt(RefTypeOrTPHOrWildcardOrGeneric type, Expression expr, Token offset){ + super(type, offset); + this.expr=expr; + } + + @Override + public void accept(StatementVisitor visitor) { + + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java b/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java new file mode 100644 index 00000000..88e286f9 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java @@ -0,0 +1,9 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +/** + * Created by Felix_K on 21.09.2017. + */ +public class CatchesStatement +{ + +} diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 0befcb2b..0a17df62 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -232,7 +232,11 @@ public class OutputGenerator implements ASTVisitor { @Override public void visit(IfStmt ifStmt) { - + out.append("if"); + ifStmt.expr.accept(this); + ifStmt.then_block.accept(this); + out.append("\nelse"); + ifStmt.else_block.accept(this); } @Override diff --git a/test/parser/IfTest.jav b/test/parser/IfTest.jav new file mode 100644 index 00000000..8c21cdca --- /dev/null +++ b/test/parser/IfTest.jav @@ -0,0 +1,24 @@ +class IfTest +{ + + public void IfStatementTest(); + + public void ifThenNoShortIfTest() + { + if(y) + { + } + } + + public void ifThenElseTest() + { + if(z) + { + + } + else + { + + } + } +} \ No newline at end of file diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 3306b306..ec5b6709 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -38,10 +38,11 @@ public class JavaTXCompilerTest extends JavaTXCompiler { //filesToTest.add(new File(rootDirectory+"Import.jav")); filesToTest.add(new File(rootDirectory+"BreakTest.jav")); filesToTest.add(new File(rootDirectory+"WhileTest.jav")); + filesToTest.add(new File(rootDirectory+"IfTest.jav")); for(File f : filesToTest){ SourceFile sf = this.parse(f); System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1))); - + System.out.println("-----------------------------------------------------------------------------------"); /* for(ResultSet resultSet : this.typeInference()){ Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet);