Boxed up control flow structures and preparation for edge labeling

This commit is contained in:
Carlos Galindo 2019-04-03 17:37:57 +02:00
parent 137d01938c
commit d7abcb7b55
Signed by untrusted user who does not match committer: kauron
GPG key ID: 83E68706DEE119A3
3 changed files with 148 additions and 28 deletions

View file

@ -1,22 +1,49 @@
package grafos;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.expr.BooleanLiteralExpr;
import com.github.javaparser.ast.stmt.*;
import java.util.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class CFG {
private final List<Node> nodes = new ArrayList<>();
private final List<Map.Entry<Node, Node>> edges = new LinkedList<>();
private final List<Node> nodes = new LinkedList<>();
private final LinkedList<Block> blocks = new LinkedList<>();
private Stack<Block> blockStack = new Stack<>();
private final List<Edge> edges = new LinkedList<>();
private Node beginNode, endNode;
private String nextLabel;
public void addNode(Node stmt) {
nodes.add(stmt);
if (!blockStack.isEmpty())
blockStack.peek().add(stmt);
System.out.println("NODO: " + node2str(stmt));
}
public void beginBlock(Node node) {
Block b = new Block(node, this);
if (blockStack.isEmpty())
blocks.addFirst(b);
else
blockStack.peek().addSubBlock(b);
blockStack.push(b);
}
public void endBlock() {
blockStack.pop();
}
public void setNextLabel(boolean b) {
nextLabel = String.valueOf(b);
}
public void connect(Node begin, Node end) {
edges.add(new HashMap.SimpleEntry<>(begin, end));
edges.add(new Edge(this, begin, end, nextLabel));
nextLabel = null;
}
public void connect(Collection<Node> begin, Node end) {
@ -38,18 +65,18 @@ public class CFG {
return endNode;
}
public List<String> toStringList() {
List<String> res = new LinkedList<String>();
for (Map.Entry<Node, Node> e : edges) {
res.add(edge2str(e.getKey(), e.getValue()));
public List<String> toStringList(GraphViz gv) {
List<String> res = new LinkedList<>();
res.add(gv.start_graph());
for (Block b : blocks)
res.addAll(b.toStringList(gv));
for (Edge e : edges) {
res.add(e.toString());
}
res.add(gv.end_graph());
return res;
}
private String edge2str(Node a, Node b) {
return node2str(a) + "->" + node2str(b);
}
private String node2str(Node s) {
if (s == beginNode)
return "Start";
@ -67,4 +94,92 @@ public class CFG {
return "\"(" + (index + 1) + ", " + s.getRange().get().begin.line + ") "
+ s.toString().replace("\"", "\\\"") + "\"";
}
static class Block extends LinkedList<Node> {
private static int clusterId = 0;
private final Node container;
private final CFG cfg;
private final List<Block> inners = new LinkedList<>();
Block(Node container, CFG cfg) {
this.container = container;
this.cfg = cfg;
}
void addSubBlock(Block block) {
inners.add(block);
}
List<String> toStringList(GraphViz gv) {
List<String> res = new LinkedList<>();
res.add(gv.start_subgraph(clusterId++));
res.add("label = \"" + getTitle() + "\"");
for (Block b : inners) {
res.addAll(b.toStringList(gv));
}
for (Node n : this) {
res.add(cfg.node2str(n));
}
res.add(gv.end_subgraph());
return res;
}
String getTitle() {
String template;
Object[] filler;
if (container instanceof IfStmt) {
template = "if (%s)";
filler = new Object[]{((IfStmt) container).getCondition()};
} else if (container instanceof WhileStmt) {
template = "while (%s)";
filler = new Object[]{((WhileStmt) container).getCondition()};
} else if (container instanceof DoStmt) {
template = "do-while (%s)";
filler = new Object[]{((DoStmt) container).getCondition()};
} else if (container instanceof ForStmt) {
template = "for (%s; %s; %s)";
filler = new Object[]{
((ForStmt) container).getInitialization(),
((ForStmt) container).getCompare().orElse(new BooleanLiteralExpr(true)),
((ForStmt) container).getUpdate()};
} else if (container instanceof ForeachStmt) {
template = "for (%s : %s)";
filler = new Object[]{
((ForeachStmt) container).getVariable(),
((ForeachStmt) container).getIterable()};
} else if (container instanceof BlockStmt) {
template = "";
filler = new Object[0];
} else if (container instanceof SwitchStmt) {
template = "switch (%s)";
filler = new Object[]{((SwitchStmt) container).getSelector()};
} else {
throw new RuntimeException("Unimplemented!!!");
// TODO: remove
}
return String.format(template, filler);
}
}
static class Edge {
private final CFG cfg;
private final Node origin, destination;
private final String label;
Edge(CFG cfg, Node origin, Node destination, String label) {
this.cfg = cfg;
this.origin = origin;
this.destination = destination;
this.label = label;
}
public String toString() {
String s = cfg.node2str(origin) + " -> " + cfg.node2str(destination);
if (label != null) {
s += " [ label = \"" + label + "\" ]";
}
return s;
}
}
}