From 709bde80aff2518aaf84d4801b98517d6e5d173c Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Wed, 27 Mar 2019 01:57:03 +0100 Subject: [PATCH] Visitador: implemented forStmts --- src/main/java/grafos/Visitador.java | 46 ++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/grafos/Visitador.java b/src/main/java/grafos/Visitador.java index d3a5578..935a5e7 100755 --- a/src/main/java/grafos/Visitador.java +++ b/src/main/java/grafos/Visitador.java @@ -2,10 +2,8 @@ package grafos; import com.github.javaparser.ast.Node; import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.stmt.DoStmt; -import com.github.javaparser.ast.stmt.ExpressionStmt; -import com.github.javaparser.ast.stmt.IfStmt; -import com.github.javaparser.ast.stmt.WhileStmt; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.stmt.*; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; import java.util.Collections; @@ -90,6 +88,46 @@ public class Visitador extends VoidVisitorAdapter { prevNode = Collections.singletonList(n.getCondition()); } + @Override + public void visit(ForStmt n, CFG graph) { + // Initialization expressions + if (n.getInitialization() != null) { + for (Expression e : n.getInitialization()) { + graph.addNode(e); + graph.connect(prevNode, e); + prevNode = Collections.singletonList(e); + } + } + // Comparison expression + // TODO: shortcut conditions (||, &&) + if (n.getCompare().isPresent()) { + graph.addNode(n.getCompare().get()); + graph.connect(prevNode, n.getCompare().get()); + prevNode = Collections.singletonList(n.getCompare().get()); + } + // Loop body + n.getBody().accept(this, graph); + // Update expressions + if (n.getUpdate() != null) { + for (Expression e : n.getUpdate()) { + graph.addNode(e); + graph.connect(prevNode, e); + prevNode = Collections.singletonList(e); + } + } + // Connect to comparison expression + if (n.getCompare().isPresent()) + graph.connect(prevNode, n.getCompare().get()); + else { + // TODO: connect last update / end of loop with first element of loop body + } + // Set comparison as last possible statement + if (n.getCompare().isPresent()) + 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(); + } + // Visitador de expresiones // Cada expresión encontrada genera un nodo en el CFG @Override