Complete API change
This commit is contained in:
parent
f24b00c255
commit
e77fe8f772
2 changed files with 115 additions and 131 deletions
|
@ -4,34 +4,90 @@ import com.github.javaparser.ast.Node;
|
|||
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
|
||||
import com.github.javaparser.ast.stmt.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
import java.util.*;
|
||||
|
||||
public class CFG {
|
||||
private final List<Node> nodes = new LinkedList<>();
|
||||
private final LinkedList<Block> blocks = new LinkedList<>();
|
||||
private Stack<Block> blockStack = new Stack<>();
|
||||
private final Stack<Block> blockStack = new Stack<>();
|
||||
private final List<Edge> edges = new LinkedList<>();
|
||||
private Node beginNode, endNode;
|
||||
private final Node beginNode = new EmptyStmt(), endNode = new EmptyStmt();
|
||||
private final List<Node> nodeList = new LinkedList<>();
|
||||
|
||||
private String nextLabel;
|
||||
|
||||
public void addNode(Node stmt) {
|
||||
private void registerNode(Node stmt) {
|
||||
nodes.add(stmt);
|
||||
if (!blockStack.isEmpty())
|
||||
blockStack.peek().add(stmt);
|
||||
System.out.println("NODO: " + node2str(stmt));
|
||||
}
|
||||
|
||||
public void beginBlock(Node node, Node condition) {
|
||||
beginBlock(node, condition, true);
|
||||
public void setNode(Node node) {
|
||||
clearNode();
|
||||
appendNode(node);
|
||||
}
|
||||
|
||||
public void beginBlock(Node node, Node condition, boolean addNow) {
|
||||
public void clearNode() {
|
||||
nodeList.clear();
|
||||
}
|
||||
|
||||
public void connectTo(Node node) {
|
||||
connectTo(node, true);
|
||||
}
|
||||
|
||||
public void connectTo(Node node, boolean overwrite) {
|
||||
if (!nodesContains(node))
|
||||
registerNode(node);
|
||||
for (Node begin : nodeList)
|
||||
connect(begin, node);
|
||||
if (overwrite)
|
||||
setNode(node);
|
||||
}
|
||||
|
||||
private boolean nodesContains(Object object) {
|
||||
for (Node n : nodes)
|
||||
if (n == object)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void removeNode(Node node) {
|
||||
nodeList.remove(node);
|
||||
}
|
||||
|
||||
public void appendNode(Node node) {
|
||||
nodeList.add(node);
|
||||
}
|
||||
|
||||
public void appendNodes(Collection<? extends Node> nodes) {
|
||||
nodeList.addAll(nodes);
|
||||
}
|
||||
|
||||
public List<Node> getNodeList() {
|
||||
return Collections.unmodifiableList(nodeList);
|
||||
}
|
||||
|
||||
public void setNodeList(List<Node> nodes) {
|
||||
nodeList.clear();
|
||||
nodeList.addAll(nodes);
|
||||
}
|
||||
|
||||
public void beginMethod() {
|
||||
nodeList.clear();
|
||||
nodeList.add(beginNode);
|
||||
}
|
||||
|
||||
public void finishMethod() {
|
||||
for (Node begin : nodeList)
|
||||
if (begin != endNode)
|
||||
connect(begin, endNode);
|
||||
}
|
||||
|
||||
public void beginBlock(Node node, Node condition) {
|
||||
Block b = new Block(node, condition);
|
||||
if (condition != null && addNow)
|
||||
addNode(condition);
|
||||
if (condition != null)
|
||||
registerNode(condition);
|
||||
if (blockStack.isEmpty())
|
||||
blocks.addFirst(b);
|
||||
else
|
||||
|
@ -47,27 +103,12 @@ public class CFG {
|
|||
nextLabel = String.valueOf(b);
|
||||
}
|
||||
|
||||
public void connect(Node begin, Node end) {
|
||||
private void connect(Node begin, Node end) {
|
||||
edges.add(new Edge(begin, end, nextLabel));
|
||||
nextLabel = null;
|
||||
}
|
||||
|
||||
public void connect(Collection<Node> begin, Node end) {
|
||||
for (Node n : begin)
|
||||
connect(n, end);
|
||||
}
|
||||
|
||||
public Node beginNode() {
|
||||
if (beginNode == null) {
|
||||
beginNode = new EmptyStmt();
|
||||
}
|
||||
return beginNode;
|
||||
}
|
||||
|
||||
public Node endNode() {
|
||||
if (endNode == null) {
|
||||
endNode = new EmptyStmt();
|
||||
}
|
||||
public Node getEndNode() {
|
||||
return endNode;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue