Fehlendes Return am Ende von Blocks beim Parsen einfügen

This commit is contained in:
JanUlrich 2017-03-29 19:44:24 +02:00
parent 4ecf526b14
commit de91e49bcc
8 changed files with 43 additions and 15 deletions

View File

@ -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());
}

View File

@ -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<Statement> addTrailingReturn(List<Statement> statements){
Statement lastStmt = statements.get(statements.size()-1);
if(lastStmt instanceof Return)return statements;
public static List<Statement> addTrailingReturn(List<Statement> 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;
}
}

View File

@ -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);
}

View File

@ -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)
{

View File

@ -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) {

View File

@ -17,7 +17,6 @@ public class Return extends Statement
this.retexpr = retExpr;
}
@Override
public ConstraintSet getConstraints(TypeInferenceBlockInformation info) {
ConstraintSet ret = retexpr.getConstraints(info);

View File

@ -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);
}
}

View File

@ -14,7 +14,7 @@ public class WhileStmt extends Statement
}
public Expression expr;
public Statement loop_block;
public Block loop_block;
/**
* <br/>Author: Martin Pl�micke