From 29fb671e69f90ad3b04c6fa8d746caf857f89e77 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 22 Sep 2026 11:25:05 -0700 Subject: Add shared graph viewer to all chapters --- .../main/java/com/seaofnodes/simple/Parser.java | 3 ++ .../simple/print/SimpleGraphAdapter.java | 45 ++++++++++++++++++++++ .../simple/print/SimpleGraphObserver.java | 35 +++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java create mode 100644 chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java (limited to 'chapter01/src/main') diff --git a/chapter01/src/main/java/com/seaofnodes/simple/Parser.java b/chapter01/src/main/java/com/seaofnodes/simple/Parser.java index 806c985d..c2ea3734 100644 --- a/chapter01/src/main/java/com/seaofnodes/simple/Parser.java +++ b/chapter01/src/main/java/com/seaofnodes/simple/Parser.java @@ -11,6 +11,9 @@ import com.seaofnodes.simple.node.*; */ public class Parser { + public int pos() { return _lexer._position; } + + /** * A Global Static, unique to each compilation. This is a public, so we * can make constants everywhere without having to thread the StartNode diff --git a/chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java new file mode 100644 index 00000000..59275832 --- /dev/null +++ b/chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java @@ -0,0 +1,45 @@ +package com.seaofnodes.simple.print; + +import com.seaofnodes.graph.GraphAdapter; +import com.seaofnodes.graph.GraphSnapshot; +import com.seaofnodes.graph.GraphSnapshot.*; +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.node.Node; +import java.util.ArrayList; + +/** Chapter 1's view of the IR; browser, transport and layout are shared. */ +public class SimpleGraphAdapter extends GraphAdapter { + @Override protected boolean dead(Node n) { return false; } + @Override protected int id(Node n) { return n._nid; } + @Override protected int nIns(Node n) { return n.nIns(); } + @Override protected Node in(Node n, int idx) { return n.in(idx); } + @Override protected int nOuts(Node n) { return n.nOuts(); } + @Override protected Node out(Node n, int idx) { return n._outputs.get(idx); } + + private ArrayList edges(Node n) { + var edges = new ArrayList(); + for( int i = 0; i < n.nIns(); i++ ) { + String name = null; + edges.add(new Edge(i, ref(n.in(i)), role(n, i), name)); + } + return edges; + } + + @Override protected GraphSnapshot.Node desc(Node n) { + Projection proj = null; + String label = n instanceof ConstantNode c ? Long.toString(c._value) : n.getClass().getSimpleName(); + return new GraphSnapshot.Node(n._nid, label == null ? n.getClass().getSimpleName() : label, + null, kind(n), edges(n), proj); + } + + private Kind kind(Node n) { + if( n instanceof StartNode ) return Kind.CTRL; + return n.isCFG() ? Kind.CTRL : Kind.DATA; + } + + private Role role(Node n, int i) { + if( n instanceof ConstantNode ) return Role.ASSOC; + if( i == 0 ) return Role.CTRL; + return Role.DATA; + } +} diff --git a/chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java b/chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java new file mode 100644 index 00000000..1c78e4dd --- /dev/null +++ b/chapter01/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java @@ -0,0 +1,35 @@ +package com.seaofnodes.simple.print; + +import com.seaofnodes.graph.GraphCapture; +import com.seaofnodes.graph.GraphViewer; +import com.seaofnodes.simple.Parser; +import com.seaofnodes.simple.node.Node; +import java.util.ArrayList; +import java.io.IOException; + +/** Chapter 1's compilation context; capture and event bookkeeping are shared. */ +public class SimpleGraphObserver extends GraphCapture { + private Parser _parser; + private Node _ret; + private String _phase; + public SimpleGraphObserver() { super(new SimpleGraphAdapter()); } + public static void main(String[] args) throws IOException { + GraphViewer.run(new SimpleGraphObserver()); + } + @Override protected void compile(String src) { + _parser = new Parser(src); + _phase = "Parse"; + _ret = _parser.parse(); + phase(_phase); + } + @Override protected String phase() { return _phase; } + @Override protected int pos() { return "Parse".equals(_phase) ? _parser.pos() : -1; } + @Override protected void detach() { + _parser = null; + _ret = null; + } + @Override protected void roots(ArrayList roots) { + roots.add(Parser.START); + roots.add(_ret); + } +} -- cgit v1.2.3