Added treatment of ifElse statements

This commit is contained in:
Carlos Galindo 2019-03-26 23:26:01 +01:00
parent 5dd9e8e35b
commit c768ffb7d3
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3

View file

@ -3,9 +3,11 @@ package grafos;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ExpressionStmt; import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
public class Visitador extends VoidVisitorAdapter<CFG> { public class Visitador extends VoidVisitorAdapter<CFG> {
@ -35,6 +37,33 @@ public class Visitador extends VoidVisitorAdapter<CFG> {
graph.connect(n, end); graph.connect(n, end);
} }
@Override
public void visit(IfStmt n, CFG graph) {
Node ifStart = n.getCondition();
graph.addNode(ifStart);
graph.connect(prevNode, ifStart);
// TODO: shortcut conditions (||, &&)
prevNode = Collections.singletonList(ifStart);
List<Node> prevNodeBegin = prevNode;
n.getThenStmt().accept(this, graph);
List<Node> newPrev = new LinkedList<>();
if (prevNode == prevNodeBegin)
newPrev.add(ifStart);
else newPrev.addAll(prevNode);
prevNode = Collections.singletonList(ifStart);
if (n.getElseStmt().isPresent()) {
n.getElseStmt().get().accept(this, graph);
if (prevNode == prevNodeBegin)
newPrev.add(ifStart);
else newPrev.addAll(prevNode);
} else {
newPrev.add(ifStart);
}
prevNode = newPrev;
}
// Visitador de expresiones // Visitador de expresiones
// Cada expresión encontrada genera un nodo en el CFG // Cada expresión encontrada genera un nodo en el CFG
@Override @Override