2019-03-26 20:11:14 +01:00
|
|
|
|
package grafos;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import com.github.javaparser.ast.NodeList;
|
|
|
|
|
import com.github.javaparser.ast.body.MethodDeclaration;
|
|
|
|
|
import com.github.javaparser.ast.stmt.BlockStmt;
|
|
|
|
|
import com.github.javaparser.ast.stmt.ExpressionStmt;
|
|
|
|
|
import com.github.javaparser.ast.stmt.Statement;
|
|
|
|
|
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Visitador extends VoidVisitorAdapter<List<String>>
|
2019-03-26 20:25:29 +01:00
|
|
|
|
{
|
|
|
|
|
//********************************************************/
|
|
|
|
|
//********************** Atributos ***********************/
|
|
|
|
|
//********************************************************/
|
2019-03-26 20:11:14 +01:00
|
|
|
|
|
|
|
|
|
// Usamos un contador para numerar las instrucciones
|
2019-03-26 20:25:29 +01:00
|
|
|
|
private int contador=1;
|
|
|
|
|
private String nodoAnterior = "Start";
|
|
|
|
|
private String nodoActual = "";
|
2019-03-26 20:11:14 +01:00
|
|
|
|
|
2019-03-26 20:25:29 +01:00
|
|
|
|
//********************************************************/
|
|
|
|
|
//*********************** Metodos ************************/
|
|
|
|
|
//********************************************************/
|
2019-03-26 20:11:14 +01:00
|
|
|
|
|
|
|
|
|
// Visitador de métodos
|
|
|
|
|
// Este visitador añade el nodo final al CFG
|
|
|
|
|
@Override
|
|
|
|
|
public void visit(MethodDeclaration methodDeclaration, List<String>collector)
|
|
|
|
|
{
|
|
|
|
|
// Visitamos el método
|
|
|
|
|
super.visit(methodDeclaration, collector);
|
|
|
|
|
|
|
|
|
|
// Añadimos el nodo final al CFG
|
|
|
|
|
collector.add(nodoAnterior+"-> Stop;");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Visitador de expresiones
|
|
|
|
|
// Cada expresión encontrada genera un nodo en el CFG
|
|
|
|
|
@Override
|
|
|
|
|
public void visit(ExpressionStmt es, List<String>collector)
|
|
|
|
|
{
|
|
|
|
|
// Creamos el nodo actual
|
|
|
|
|
nodoActual = crearNodo(es);
|
|
|
|
|
|
|
|
|
|
crearArcos(collector);
|
|
|
|
|
|
|
|
|
|
nodoAnterior = nodoActual;
|
|
|
|
|
|
|
|
|
|
// Seguimos visitando...
|
|
|
|
|
super.visit(es, collector);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Añade un arco desde el último nodo hasta el nodo actual (se le pasa como parametro)
|
|
|
|
|
private void añadirArcoSecuencialCFG(List<String>collector)
|
|
|
|
|
{
|
|
|
|
|
System.out.println("NODO: " + nodoActual);
|
|
|
|
|
|
|
|
|
|
String arco = nodoAnterior + "->" + nodoActual + ";";
|
|
|
|
|
collector.add(arco);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crear arcos
|
|
|
|
|
private void crearArcos(List<String>collector)
|
|
|
|
|
{
|
|
|
|
|
añadirArcoSecuencialCFG(collector);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crear nodo
|
|
|
|
|
// Añade un arco desde el nodo actual hasta el último control
|
|
|
|
|
private String crearNodo(Object objeto)
|
|
|
|
|
{
|
|
|
|
|
return "\"("+ contador++ +") "+quitarComillas(objeto.toString())+"\"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sustituye " por \" en un string: Sirve para eliminar comillas.
|
|
|
|
|
private static String quitarComillas(String texto)
|
|
|
|
|
{
|
|
|
|
|
return texto.replace("\"", "\\\"");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Dada una sentencia,
|
|
|
|
|
// Si es una <20>nica instrucci<63>n, devuelve un bloque equivalente
|
|
|
|
|
// Si es un bloque, lo devuelve
|
|
|
|
|
private BlockStmt convertirEnBloque(Statement statement)
|
|
|
|
|
{
|
|
|
|
|
if (statement instanceof BlockStmt)
|
|
|
|
|
return (BlockStmt) statement;
|
|
|
|
|
|
|
|
|
|
BlockStmt block = new BlockStmt();
|
|
|
|
|
NodeList<Statement> blockStmts = new NodeList<Statement>();
|
|
|
|
|
blockStmts.add(statement);
|
|
|
|
|
|
|
|
|
|
block.setStatements(blockStmts);
|
|
|
|
|
|
|
|
|
|
return block;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|