From 8e1c1765ff34f468f067c376036dce30b593ac6c Mon Sep 17 00:00:00 2001 From: FelixKrespach Date: Wed, 20 Sep 2017 17:36:16 +0200 Subject: [PATCH] Many methods in statementgenerator implemented --- .../StatementGenerator.java | 66 +++++++++++++++---- .../SyntaxTreeGenerator/SyntacticSugar.java | 4 +- .../syntaxtree/statement/IfStmt.java | 9 +-- test/parser/WhileTest.jav | 10 +-- test/typeinference/JavaTXCompilerTest.java | 9 ++- 5 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 38cbd0cb..655da3b1 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -300,18 +300,24 @@ public class StatementGenerator { } private Statement convert(Java8Parser.IfThenStatementContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement thenBlock = convert(stmt.statement()); + Statement elseBlock = new EmptyStmt(stmt.getStart()); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement thenBlock = convert(stmt.statementNoShortIf()); + Statement elseBlock = convert(stmt.statement()); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement thenBlock = convert(stmt.statementNoShortIf().get(0)); + Statement elseBlock = convert(stmt.statementNoShortIf().get(1)); + return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } private Statement convert(Java8Parser.AssertStatementContext stmt){ @@ -341,8 +347,9 @@ public class StatementGenerator { } private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); + Expression expr = convert(stmt.expression()); + Statement block = convert(stmt.statementNoShortIf()); + return new WhileStmt(expr, block, stmt.getStart()); } private Statement convert(Java8Parser.DoStatementContext stmt){ @@ -368,13 +375,41 @@ public class StatementGenerator { } private Statement convert(Java8Parser.BasicForStatementContext stmt){ - //TODO - throw new NotImplementedException(); + List stateList = new ArrayList<>(); + List whileList = new ArrayList<>(); + if (stmt.forInit().localVariableDeclaration()==null) + { + whileList.addAll(convert(stmt.forInit().localVariableDeclaration())); + } + else + { + whileList.add(convert(stmt.forInit().statementExpressionList())); + } + Expression expr = convert(stmt.expression()); + stateList.add(convert(stmt.statement())); + stateList.add(convert(stmt.forUpdate())); + Block forBlock = new Block(stateList,stmt.getStart()); + whileList.add(new WhileStmt(expr,forBlock,stmt.getStart())); + return new Block(whileList,stmt.getStart()); } private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt){ - //TODO - throw new NotImplementedException(); + List stateList = new ArrayList<>(); + List whileList = new ArrayList<>(); + if (stmt.forInit().localVariableDeclaration()==null) + { + whileList.addAll(convert(stmt.forInit().localVariableDeclaration())); + } + else + { + whileList.add(convert(stmt.forInit().statementExpressionList())); + } + Expression expr = convert(stmt.expression()); + stateList.add(convert(stmt.statementNoShortIf())); + stateList.add(convert(stmt.forUpdate())); + Block forBlock = new Block(stateList,stmt.getStart()); + whileList.add(new WhileStmt(expr,forBlock,stmt.getStart())); + return new Block(whileList,stmt.getStart()); } private List convert(Java8Parser.ForInitContext stmt){ @@ -456,8 +491,11 @@ public class StatementGenerator { } private Statement convert(Java8Parser.BreakStatementContext stmt){ - //TODO - throw new NotImplementedException(); + if (stmt.Identifier()!=null) + { + new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier(),stmt.getStart()); + } + return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier(),stmt.getStart()); } private Statement convert(Java8Parser.ContinueStatementContext stmt){ diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java index d758f52b..7e573303 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -15,8 +15,8 @@ public class SyntacticSugar { //TODO //if (hasReturn(((WhileStmt) lastStmt).loopBlock)) return statements; } else if (lastStmt instanceof IfStmt) { - if (hasReturn(((IfStmt) lastStmt).then_block) - && hasReturn(((IfStmt) lastStmt).else_block)) return statements; + //if (hasReturn(((IfStmt) lastStmt).then_block) + //&& hasReturn(((IfStmt) lastStmt).else_block)) return statements; } else if (lastStmt instanceof ForStmt) { if (hasReturn(((ForStmt) lastStmt).body_Loop_block)) return statements; } else { diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index 5d6a4658..b40242bd 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -11,11 +11,11 @@ import org.antlr.v4.runtime.Token; public class IfStmt extends Statement { public final Expression expr; - public final Block then_block; - public final Block else_block; + public final Statement then_block; + public final Statement else_block; - public IfStmt(RefTypeOrTPHOrWildcardOrGeneric type, - Expression expr, Block thenBlock, Block elseBlock, Token offset) + public IfStmt(RefTypeOrTPHOrWildcardOrGeneric type, + Expression expr, Statement thenBlock, Statement elseBlock, Token offset) { super(type,offset); this.expr = expr; @@ -23,6 +23,7 @@ public class IfStmt extends Statement this.else_block = elseBlock; } + @Override public void accept(StatementVisitor visitor) { visitor.visit(this); diff --git a/test/parser/WhileTest.jav b/test/parser/WhileTest.jav index 2ce192bd..7269793d 100644 --- a/test/parser/WhileTest.jav +++ b/test/parser/WhileTest.jav @@ -1,9 +1,11 @@ -class WhileTest{ +class WhileTest +{ void methode(){ Boolean test; - do{ - test=test; - }while(test); + do + { + test=test; + }while(test); while(test){ diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index cd8a2d24..3306b306 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -20,14 +20,14 @@ import java.util.Set; public class JavaTXCompilerTest extends JavaTXCompiler { - private static final String rootDirectory = System.getProperty("user.dir")+"/test/javFiles/"; + private static final String rootDirectory = System.getProperty("user.dir")+"/test/parser/"; private static final List filesToTest = new ArrayList<>(); @Test public void test() throws IOException, java.lang.ClassNotFoundException { //filesToTest.add(new File(rootDirectory+"Faculty.jav")); //filesToTest.add(new File(rootDirectory+"mathStruc.jav")); - filesToTest.add(new File(rootDirectory+"test.jav")); + //filesToTest.add(new File(rootDirectory+"test.jav")); //filesToTest.add(new File(rootDirectory+"Lambda.jav")); //filesToTest.add(new File(rootDirectory+"Lambda2.jav")); //filesToTest.add(new File(rootDirectory+"Lambda3.jav")); @@ -36,9 +36,13 @@ public class JavaTXCompilerTest extends JavaTXCompiler { //filesToTest.add(new File(rootDirectory+"MethodsEasy.jav")); //filesToTest.add(new File(rootDirectory+"Matrix.jav")); //filesToTest.add(new File(rootDirectory+"Import.jav")); + filesToTest.add(new File(rootDirectory+"BreakTest.jav")); + filesToTest.add(new File(rootDirectory+"WhileTest.jav")); for(File f : filesToTest){ SourceFile sf = this.parse(f); System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1))); + + /* for(ResultSet resultSet : this.typeInference()){ Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); String content = readFile(f.getPath(), StandardCharsets.UTF_8); @@ -46,6 +50,7 @@ public class JavaTXCompilerTest extends JavaTXCompiler { System.out.println(tip.insert(content)); } } + */ } }