From 07303ffa71fc401b17b44b07aa0eb3f745d468e2 Mon Sep 17 00:00:00 2001 From: FelixKrespach Date: Fri, 22 Sep 2017 18:19:34 +0200 Subject: [PATCH] Implemented Continue, Catches, Break visit ... --- .../StatementGenerator.java | 32 +++++++++---------- .../dhbwstuttgart/syntaxtree/ASTVisitor.java | 2 ++ .../syntaxtree/AbstractASTWalker.java | 4 +++ .../syntaxtree/StatementVisitor.java | 2 ++ .../syntaxtree/statement/BreakStmt.java | 5 +-- .../statement/CatchesStatement.java | 8 ++++- .../syntaxtree/statement/ContinueStmt.java | 14 ++++++++ .../syntaxtree/statement/PreDecExpr.java | 4 ++- .../syntaxtree/statement/PreIncExpr.java | 4 ++- .../syntaxtree/visual/OutputGenerator.java | 8 ++++- .../typedeployment/TypeInsertPlacer.java | 3 ++ .../typeinference/typeAlgo/TYPEStmt.java | 2 ++ test/parser/BreakTest.jav | 4 +-- test/parser/WhileTest.jav | 5 ++- test/typeinference/JavaTXCompilerTest.java | 8 +++-- 15 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/ContinueStmt.java diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index c40b7789..4447827c 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -269,24 +269,19 @@ public class StatementGenerator { } private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new PreIncExpr(convert(stmt.unaryExpression()),stmt.getStart()); } private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new PreDecExpr(convert(stmt.unaryExpression()),stmt.getStart()); } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - Expression expr = convert(stmt.postfixExpression()); - //TODO - throw new NotImplementedException(); + return new PostIncExpr(convert(stmt.postfixExpression())); } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { - //TODO - throw new NotImplementedException(); + return new PostDecExpr(convert(stmt.postfixExpression())); } private Statement convert(Java8Parser.AssignmentContext stmt) { @@ -341,7 +336,7 @@ public class StatementGenerator { } private Statement convert(Java8Parser.SwitchStatementContext stmt){ - //TODO + //TODO Felix K throw new NotImplementedException(); } @@ -506,16 +501,20 @@ public class StatementGenerator { } private Statement convert(Java8Parser.BreakStatementContext stmt){ - if (stmt.Identifier()!=null) + if (stmt.Identifier()==null) { - new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier(),stmt.getStart()); + String indentifier = ""; + return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),indentifier,stmt.getStart()); } - return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier(),stmt.getStart()); + return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier().getText(),stmt.getStart()); } private Statement convert(Java8Parser.ContinueStatementContext stmt){ - //TODO - throw new NotImplementedException(); + if (stmt.Identifier()==null){ + String indentifier = ""; + return new ContinueStmt(TypePlaceholder.fresh(stmt.getStart()),indentifier, stmt.getStart()); + } + return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier().getText(),stmt.getStart()); } private Statement convert(Java8Parser.ReturnStatementContext stmt){ @@ -541,7 +540,8 @@ public class StatementGenerator { throw new NotImplementedException(); } - private Statement convert(Java8Parser.CatchesContext stmt){ + private Statement convert(Java8Parser.CatchesContext stmt) + { //TODO throw new NotImplementedException(); } diff --git a/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java b/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java index 9d3a7f6d..f0030595 100644 --- a/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/ASTVisitor.java @@ -35,4 +35,6 @@ public interface ASTVisitor extends StatementVisitor{ void visit(ExtendsWildcardType extendsWildcardType); void visit(GenericRefType genericRefType); + + void visit(BreakStmt breakStmt); } diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index cd3027f5..a82e0a8e 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -268,4 +268,8 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(SuperCall superCall) { this.visit((MethodCall)superCall); } + + @Override + public void visit(BreakStmt breakStmt) { + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index b63e0438..673c6e47 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -18,6 +18,8 @@ public interface StatementVisitor { void visit(Block block); + void visit(BreakStmt breakStmt); + void visit(CastExpr castExpr); void visit(EmptyStmt emptyStmt); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/BreakStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/BreakStmt.java index 7dcb05be..38a1b2c3 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/BreakStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/BreakStmt.java @@ -11,14 +11,15 @@ import org.antlr.v4.runtime.tree.TerminalNode; */ public class BreakStmt extends Statement { - public final TerminalNode indentifier; + public final String indentifier; - public BreakStmt(RefTypeOrTPHOrWildcardOrGeneric type, TerminalNode Indentifier, Token offset) + public BreakStmt(RefTypeOrTPHOrWildcardOrGeneric type, String Indentifier, Token offset) { super(type, offset); this.indentifier=Indentifier; } @Override public void accept(StatementVisitor visitor) { + visitor.visit(this); } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java b/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java index 88e286f9..9d61b94c 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/CatchesStatement.java @@ -1,9 +1,15 @@ package de.dhbwstuttgart.syntaxtree.statement; +import java.util.List; + /** * Created by Felix_K on 21.09.2017. */ public class CatchesStatement { - + final List catchClauses; + public CatchesStatement(List catches) + { + catchClauses = catches; + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ContinueStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ContinueStmt.java new file mode 100644 index 00000000..abe99717 --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ContinueStmt.java @@ -0,0 +1,14 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +/** + * Created by Felix_K on 22.09.2017. + */ +public class ContinueStmt extends BreakStmt { + public ContinueStmt(RefTypeOrTPHOrWildcardOrGeneric type, String indent, Token offset){ + super(type, indent, offset); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java index e1d311b8..b9089f74 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/PreDecExpr.java @@ -12,8 +12,10 @@ import org.apache.bcel.generic.InstructionList; public class PreDecExpr extends UnaryExpr { - public PreDecExpr(Token offset) + Expression expr; + public PreDecExpr(Expression expr, Token offset) { super(offset); + this.expr=expr; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java b/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java index 736dc311..79007a75 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/PreIncExpr.java @@ -5,8 +5,10 @@ import org.antlr.v4.runtime.Token; public class PreIncExpr extends UnaryExpr { - public PreIncExpr(Token offset) + final Expression expr; + public PreIncExpr(Expression expr,Token offset) { super(offset); + this.expr=expr; } } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index a014fb68..409745f1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -209,6 +209,12 @@ public class OutputGenerator implements ASTVisitor { out.append("}"); } + @Override + public void visit(BreakStmt breakStmt) + { + out.append("break" + breakStmt.indentifier+";"); + } + @Override public void visit(CastExpr castExpr) { @@ -235,7 +241,7 @@ public class OutputGenerator implements ASTVisitor { out.append("if"); ifStmt.expr.accept(this); ifStmt.then_block.accept(this); - out.append("\nelse"); + out.append("else"); ifStmt.else_block.accept(this); //TODO Felix_K diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 8dea5748..a4775a8c 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -1,6 +1,8 @@ package de.dhbwstuttgart.typedeployment; +import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; +import de.dhbwstuttgart.syntaxtree.statement.BreakStmt; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -70,4 +72,5 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ public void visit(LambdaExpression lambdaExpression) { //Lambda-Ausdrücke brauchen keine Typeinsetzungen } + } diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 4a693141..4e49ac55 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -87,6 +87,8 @@ public class TYPEStmt implements StatementVisitor{ stmt.accept(this); } } + @Override + public void visit(BreakStmt breakStmt){throw new NotImplementedException();} @Override public void visit(CastExpr castExpr) { diff --git a/test/parser/BreakTest.jav b/test/parser/BreakTest.jav index 0fefbfc1..c66114bd 100644 --- a/test/parser/BreakTest.jav +++ b/test/parser/BreakTest.jav @@ -2,8 +2,6 @@ class BreakTest { void methode() { - - break; - + break BREAKTEST; } } \ No newline at end of file diff --git a/test/parser/WhileTest.jav b/test/parser/WhileTest.jav index e8dc16e7..d7ad4065 100644 --- a/test/parser/WhileTest.jav +++ b/test/parser/WhileTest.jav @@ -2,11 +2,10 @@ class WhileTest { void methode() { - Boolean test; + Boolean BoolTrue=true; do { - test=test; - }while(test); + }while(testTrue); while(test) diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index ec5b6709..7f259d34 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -2,7 +2,9 @@ package typeinference; import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.SourceFile; +import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; +import de.dhbwstuttgart.syntaxtree.visual.OutputGenerator; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; import de.dhbwstuttgart.typeinference.result.ResultSet; @@ -36,12 +38,12 @@ 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+"BreakTest.jav")); filesToTest.add(new File(rootDirectory+"WhileTest.jav")); - filesToTest.add(new File(rootDirectory+"IfTest.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(ASTPrinter.print(this.sourceFiles.get(sourceFiles.size()-1))); System.out.println("-----------------------------------------------------------------------------------"); /* for(ResultSet resultSet : this.typeInference()){