package vaporize.library; import cs132.vapor.ast.*; import misc.*; import java.util.ArrayList; public class ControlFlowGraph { private ArrayList nodes; private CFGNode start; protected ControlFlowGraph() { this.nodes = new ArrayList<>(); this.start = null; } protected CFGNode getNode(Object a) { CFGNode ret = null; for (CFGNode n : this.nodes) { if (n.equals(a)) { ret = n; break; } } if (ret == null) { String str = (a instanceof Node) ? ((Node) a).sourcePos.toString() : a.toString(); MinimalLogger.severe(String.format("Could not find a node matching %s", str)); } return ret; } protected void addNode(CFGNode node) { this.nodes.add(node); } protected String addEdge(CFGNode source, CFGNode dest) { /** * Iff the CFGNodes are different, construct an edge between them. * Returns a string capable of being manipulated by graphviz. */ String ret = ""; if (!source.equals(dest)) { ret = String.format("%d -> %d", source.getInstruction().sourcePos.line, dest.getInstruction().sourcePos.line); MinimalLogger.info(String.format("Edge %s", ret)); source.addDest(dest); dest.addSource(source); ret += ";"; } return ret; } protected void setStart(CFGNode start) { this.start = start; } protected ArrayList getNodes() { return this.nodes; } }