From de91e49bcc7d40992b6fa5c5b66d0f86a41e41f1 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Wed, 29 Mar 2017 19:44:24 +0200 Subject: [PATCH] =?UTF-8?q?Fehlendes=20Return=20am=20Ende=20von=20Blocks?= =?UTF-8?q?=20beim=20Parsen=20einf=C3=BCgen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StatementGenerator.java | 4 +-- .../SyntaxTreeGenerator/SyntacticSugar.java | 30 +++++++++++++++---- .../syntaxtree/statement/EmptyStmt.java | 6 ++-- .../syntaxtree/statement/ForStmt.java | 2 +- .../syntaxtree/statement/IfStmt.java | 4 +-- .../syntaxtree/statement/Return.java | 1 - .../syntaxtree/statement/ReturnVoid.java | 9 ++++++ .../syntaxtree/statement/WhileStmt.java | 2 +- 8 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 51542c34..5f244256 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -129,7 +129,7 @@ public class StatementGenerator { if(stmt.block() != null){ return convert(stmt.block()); }else if(stmt.emptyStatement() != null){ - return new EmptyStmt(); + return new EmptyStmt(stmt.getStart()); }else if(stmt.expressionStatement() != null){ return convert(stmt.expressionStatement()); }else if(stmt.assertStatement() != null){ @@ -160,7 +160,7 @@ public class StatementGenerator { Statement stmt = convert(statementContext); statements.add(stmt); } - + statements = SyntacticSugar.addTrailingReturn(statements); return new Block(statements, block.getStart()); } diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java index 2602f58a..07b2d1fb 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntacticSugar.java @@ -1,16 +1,34 @@ package de.dhbwstuttgart.parser.SyntaxTreeGenerator; -import de.dhbwstuttgart.syntaxtree.statement.Return; -import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.syntaxtree.statement.*; import java.util.List; public class SyntacticSugar { - public List addTrailingReturn(List statements){ - Statement lastStmt = statements.get(statements.size()-1); - if(lastStmt instanceof Return)return statements; - + + public static List addTrailingReturn(List statements){ + if(statements.size()!=0) { + Statement lastStmt = statements.get(statements.size() - 1); + if (lastStmt instanceof Return) return statements; + if (lastStmt instanceof WhileStmt) { + if (hasReturn(((WhileStmt) lastStmt).loop_block)) return statements; + } else if (lastStmt instanceof IfStmt) { + 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 { + } + } + statements.add(new ReturnVoid(new NullToken())); return statements; } + private static boolean hasReturn(Block block){ + for(Statement s : block.getStatements()) + if(s instanceof Return)return true; + return false; + } + } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java index 164ff6d6..1ae2df74 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/EmptyStmt.java @@ -1,15 +1,17 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; +import org.antlr.v4.runtime.Token; public class EmptyStmt extends Statement { - public EmptyStmt() + public EmptyStmt(Token offset) { - super(null,null); + super(new Void(offset),offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java index 086427d9..8e8791ab 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ForStmt.java @@ -15,7 +15,7 @@ public class ForStmt extends Statement private Expression head_Initializer; private Expression head_Condition; private Expression head_Loop_expr; - private Statement body_Loop_block; + public Block body_Loop_block; public ForStmt(int offset, int variableLength) { diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java index d253172f..ec4145b9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/IfStmt.java @@ -14,8 +14,8 @@ public class IfStmt extends Statement public boolean hamaDebug = true; //hama: Debug Ausgaben von mir ein- bzw. ausschalten public Expression expr; - public Statement then_block; - public Statement else_block; + public Block then_block; + public Block else_block; @Override public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java index 2f8c9eb3..b2a754ca 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/Return.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/Return.java @@ -17,7 +17,6 @@ public class Return extends Statement this.retexpr = retExpr; } - @Override public ConstraintSet getConstraints(TypeInferenceBlockInformation info) { ConstraintSet ret = retexpr.getConstraints(info); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java b/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java new file mode 100644 index 00000000..1b5eae2d --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/ReturnVoid.java @@ -0,0 +1,9 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import org.antlr.v4.runtime.Token; + +public class ReturnVoid extends Return{ + public ReturnVoid(Token offset) { + super(new EmptyStmt(offset), offset); + } +} diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java index c271efc3..1070639d 100755 --- a/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/WhileStmt.java @@ -14,7 +14,7 @@ public class WhileStmt extends Statement } public Expression expr; - public Statement loop_block; + public Block loop_block; /** *
Author: Martin Pl�micke