From c768ffb7d36cd7ae140d9a0958a4b8897aaa6006 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Tue, 26 Mar 2019 23:26:01 +0100 Subject: [PATCH] Added treatment of ifElse statements --- src/main/java/grafos/Visitador.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/grafos/Visitador.java b/src/main/java/grafos/Visitador.java index 8508b7c..11dea37 100755 --- a/src/main/java/grafos/Visitador.java +++ b/src/main/java/grafos/Visitador.java @@ -3,9 +3,11 @@ package grafos; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.body.MethodDeclaration; import com.github.javaparser.ast.stmt.ExpressionStmt; +import com.github.javaparser.ast.stmt.IfStmt; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import java.util.Collections; +import java.util.LinkedList; import java.util.List; public class Visitador extends VoidVisitorAdapter { @@ -35,6 +37,33 @@ public class Visitador extends VoidVisitorAdapter { 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 prevNodeBegin = prevNode; + n.getThenStmt().accept(this, graph); + List 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 // Cada expresión encontrada genera un nodo en el CFG @Override