Boxed up control flow structures and preparation for edge labeling
This commit is contained in:
parent
137d01938c
commit
d7abcb7b55
3 changed files with 148 additions and 28 deletions
|
@ -52,6 +52,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
|
||||
@Override
|
||||
public void visit(IfStmt n, CFG graph) {
|
||||
graph.beginBlock(n);
|
||||
Node ifStart = n.getCondition();
|
||||
graph.addNode(ifStart);
|
||||
graph.connect(prevNode, ifStart);
|
||||
|
@ -75,10 +76,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
newPrev.add(ifStart);
|
||||
}
|
||||
prevNode = newPrev;
|
||||
graph.endBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(WhileStmt n, CFG graph) {
|
||||
graph.beginBlock(n);
|
||||
Node whileStart = n.getCondition();
|
||||
graph.addNode(whileStart);
|
||||
graph.connect(prevNode, whileStart);
|
||||
|
@ -89,20 +92,25 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
n.getBody().accept(this, graph);
|
||||
graph.connect(prevNode, whileStart);
|
||||
prevNode = Collections.singletonList(whileStart);
|
||||
graph.endBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DoStmt n, CFG graph) {
|
||||
graph.beginBlock(n);
|
||||
Node condition = n.getCondition();
|
||||
prevNode = new LinkedList<>(prevNode);
|
||||
prevNode.add(n.getCondition());
|
||||
prevNode.add(condition);
|
||||
n.getBody().accept(this, graph);
|
||||
graph.addNode(n.getCondition());
|
||||
graph.connect(prevNode, n.getCondition());
|
||||
prevNode = Collections.singletonList(n.getCondition());
|
||||
graph.addNode(condition);
|
||||
graph.connect(prevNode, condition);
|
||||
prevNode = Collections.singletonList(condition);
|
||||
graph.endBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ForStmt n, CFG graph) {
|
||||
graph.beginBlock(n);
|
||||
// Initialization expressions
|
||||
if (n.getInitialization() != null) {
|
||||
for (Expression e : n.getInitialization()) {
|
||||
|
@ -139,10 +147,12 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
prevNode = Collections.singletonList(n.getCompare().get());
|
||||
else // There is no comparison, can't exit the loop TODO implement break and continue as way to exit
|
||||
prevNode = Collections.emptyList();
|
||||
graph.endBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ForeachStmt n, CFG graph) {
|
||||
graph.beginBlock(n);
|
||||
ForeachStmt copy = new ForeachStmt(n.getTokenRange().orElse(null), n.getVariable(), n.getIterable(), new EmptyStmt());
|
||||
graph.addNode(copy);
|
||||
graph.connect(prevNode, copy);
|
||||
|
@ -150,6 +160,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
n.getBody().accept(this, graph);
|
||||
graph.connect(prevNode, copy);
|
||||
prevNode = Collections.singletonList(copy);
|
||||
graph.endBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -165,8 +176,9 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
|
||||
@Override
|
||||
public void visit(SwitchStmt n, CFG graph) {
|
||||
graph.beginBlock(n);
|
||||
// Link previous statement to the switch's selector
|
||||
Node selectorNode = new SwitchStmt(n.getTokenRange().orElse(null), n.getSelector(), new NodeList<>());
|
||||
Node selectorNode = n.getSelector();
|
||||
graph.addNode(selectorNode);
|
||||
graph.connect(prevNode, selectorNode);
|
||||
// Analyze switch's cases
|
||||
|
@ -191,6 +203,7 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
|
|||
prevNode.addAll(brokenStmts); // 1
|
||||
if (n.getEntries().get(n.getEntries().size() - 1).getLabel().isPresent())
|
||||
prevNode.add(selectorNode); // 2
|
||||
graph.endBlock();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue