Added support for any continue statements, fixed empty for loops (set condition to true if empty)
This commit is contained in:
parent
96847b7a8a
commit
daa1c2d90a
3 changed files with 82 additions and 20 deletions
|
@ -3,6 +3,7 @@ package grafos;
|
|||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.NodeList;
|
||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
|
||||
import com.github.javaparser.ast.expr.Expression;
|
||||
import com.github.javaparser.ast.stmt.*;
|
||||
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
||||
|
@ -53,7 +54,9 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
@Override
|
||||
public void visit(LabeledStmt n, CFG graph) {
|
||||
graph.beginBreakSection(n.getLabel());
|
||||
graph.beginContinueSection(n.getLabel());
|
||||
super.visit(n, graph);
|
||||
graph.endContinueSection(n.getLabel());
|
||||
graph.endBreakSection(n.getLabel());
|
||||
}
|
||||
|
||||
|
@ -62,11 +65,13 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
Node whileStart = n.getCondition();
|
||||
graph.beginBlock(n, whileStart);
|
||||
graph.beginBreakSection();
|
||||
graph.beginContinueSection(whileStart);
|
||||
graph.connectTo(whileStart);
|
||||
graph.setNextLabel(true);
|
||||
n.getBody().accept(this, graph);
|
||||
graph.connectTo(whileStart);
|
||||
graph.setNextLabel(false);
|
||||
graph.endContinueSection();
|
||||
graph.endBreakSection();
|
||||
graph.endBlock();
|
||||
}
|
||||
|
@ -76,10 +81,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
Node condition = n.getCondition();
|
||||
graph.beginBlock(n, condition);
|
||||
graph.beginBreakSection();
|
||||
graph.beginContinueSection(condition);
|
||||
graph.appendNode(condition);
|
||||
n.getBody().accept(this, graph);
|
||||
graph.connectTo(condition);
|
||||
graph.setNextLabel(false);
|
||||
graph.endContinueSection();
|
||||
graph.endBreakSection();
|
||||
graph.endBlock();
|
||||
}
|
||||
|
@ -96,10 +103,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
}
|
||||
// Comparison expression
|
||||
// TODO: shortcut conditions (||, &&)
|
||||
if (n.getCompare().isPresent()) {
|
||||
graph.connectTo(n.getCompare().get());
|
||||
graph.setNextLabel(true);
|
||||
}
|
||||
assert n.getTokenRange().isPresent();
|
||||
if (!n.getCompare().isPresent())
|
||||
n.setCompare(new BooleanLiteralExpr(n.getTokenRange().get(), true));
|
||||
graph.beginContinueSection(n.getCompare().get());
|
||||
graph.connectTo(n.getCompare().get());
|
||||
graph.setNextLabel(true);
|
||||
// Loop body
|
||||
n.getBody().accept(this, graph);
|
||||
// Update expressions
|
||||
|
@ -108,14 +117,9 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
graph.connectTo(e);
|
||||
// Connect to comparison expression
|
||||
// Set comparison as last possible statement
|
||||
if (n.getCompare().isPresent()) {
|
||||
graph.connectTo(n.getCompare().get());
|
||||
graph.setNextLabel(false);
|
||||
} else {
|
||||
// TODO: connect last update / end of loop with first element of loop body
|
||||
// There is no comparison, can't exit the loop TODO implement break and continue as way to exit
|
||||
graph.clearNode();
|
||||
}
|
||||
graph.connectTo(n.getCompare().get());
|
||||
graph.setNextLabel(false);
|
||||
graph.endContinueSection();
|
||||
graph.endBreakSection();
|
||||
graph.endBlock();
|
||||
}
|
||||
|
@ -125,11 +129,13 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt());
|
||||
graph.beginBlock(n, copy);
|
||||
graph.beginBreakSection();
|
||||
graph.beginContinueSection(copy);
|
||||
graph.connectTo(copy);
|
||||
graph.setNextLabel(true);
|
||||
n.getBody().accept(this, graph);
|
||||
graph.connectTo(copy);
|
||||
graph.setNextLabel(false);
|
||||
graph.endContinueSection();
|
||||
graph.endBreakSection();
|
||||
graph.endBlock();
|
||||
}
|
||||
|
@ -168,9 +174,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
|
||||
@Override
|
||||
public void visit(BreakStmt n, CFG graph) {
|
||||
graph.connectTo(n, false);
|
||||
graph.clearNode();
|
||||
graph.registerBreak(n);
|
||||
graph.connectBreak(n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ContinueStmt n, CFG graph) {
|
||||
graph.connectContinue(n);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue