Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions simpcli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn usage(process_name: &str) {
eprintln!("Usage:");
eprintln!(" {} assemble <filename>", process_name);
eprintln!(" {} disassemble <base64>", process_name);
eprintln!(" {} graph <base64>", process_name);
eprintln!(" {} relabel <base64>", process_name);
eprintln!();
eprintln!("For commands which take an optional expression, the default value is \"main\".");
Expand All @@ -43,6 +44,7 @@ fn invalid_usage(process_name: &str) -> Result<(), String> {
enum Command {
Assemble,
Disassemble,
Graph,
Relabel,
Help,
}
Expand All @@ -53,6 +55,7 @@ impl FromStr for Command {
match s {
"assemble" => Ok(Command::Assemble),
"disassemble" => Ok(Command::Disassemble),
"graphviz" | "dot" | "graph" => Ok(Command::Graph),
"relabel" => Ok(Command::Relabel),
"help" => Ok(Command::Help),
x => Err(format!("unknown command {}", x)),
Expand All @@ -65,6 +68,7 @@ impl Command {
match *self {
Command::Assemble => false,
Command::Disassemble => false,
Command::Graph => false,
Command::Relabel => false,
Command::Help => false,
}
Expand Down Expand Up @@ -155,6 +159,14 @@ fn main() -> Result<(), String> {
let prog = Forest::<DefaultJet>::from_program(commit);
println!("{}", prog.string_serialize());
}
Command::Graph => {
let v = simplicity::base64::Engine::decode(&STANDARD, first_arg.as_bytes())
.map_err(|e| format!("failed to parse base64: {}", e))?;
let iter = BitIter::from(v.into_iter());
let commit = CommitNode::<DefaultJet>::decode(iter)
.map_err(|e| format!("failed to decode program: {}", e))?;
println!("{}", commit.display_as_dot());
}
Command::Relabel => {
let prog = parse_file(&first_arg)?;
println!("{}", prog.string_serialize());
Expand Down
83 changes: 83 additions & 0 deletions src/node/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,39 @@ where
}
}

pub struct DisplayAsDot<'a, M: Marker>(&'a Node<M>);

impl<'a, M: Marker> From<&'a Node<M>> for DisplayAsDot<'a, M> {
fn from(node: &'a Node<M>) -> Self {
Self(node)
}
}

impl<'a, M: Marker> fmt::Display for DisplayAsDot<'a, M>
where
&'a Node<M>: DagLike,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "digraph G {{")?;
writeln!(f, "ordering=\"out\";")?;

for data in self.0.post_order_iter::<NoSharing>() {
let node_name = data.node.inner().to_string();
writeln!(f, " node{}[label=\"{}\"];", data.index, node_name)?;
if let Some(left) = data.left_index {
writeln!(f, " node{}->node{};", data.index, left)?;
}
if let Some(right) = data.right_index {
writeln!(f, " node{}->node{};", data.index, right)?;
}
}

writeln!(f, "}}")?;

Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::human_encoding::Forest;
Expand Down Expand Up @@ -241,4 +274,54 @@ mod tests {
program.display_expr().to_string()
)
}

#[test]
fn display_as_dot() {
let s = "
oih := take drop iden
input := pair (pair unit unit) unit
output := unit
main := comp input (comp (pair oih (take unit)) output)";
let program = parse_program(s);
let str = program
.display_as_dot()
.to_string()
.replace(" ", "")
.replace("\n", "");
let expected = "
digraph G {
ordering=\"out\";
node0[label=\"unit\"];
node1[label=\"unit\"];
node2[label=\"pair\"];
node2->node0;
node2->node1;
node3[label=\"unit\"];
node4[label=\"pair\"];
node4->node2;
node4->node3;
node5[label=\"iden\"];
node6[label=\"drop\"];
node6->node5;
node7[label=\"take\"];
node7->node6;
node8[label=\"unit\"];
node9[label=\"take\"];
node9->node8;
node10[label=\"pair\"];
node10->node7;
node10->node9;
node11[label=\"unit\"];
node12[label=\"comp\"];
node12->node10;
node12->node11;
node13[label=\"comp\"];
node13->node4;
node13->node12;
}"
.replace(" ", "")
.replace("\n", "");

assert_eq!(str, expected);
}
}
5 changes: 5 additions & 0 deletions src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
use crate::dag::{DagLike, MaxSharing, SharingTracker};
use crate::encode;
use crate::jet::Jet;
use crate::node::display::DisplayAsDot;
use crate::{types, BitWriter, Cmr, FailEntropy, HasCmr, Value};

use std::sync::Arc;
Expand Down Expand Up @@ -723,6 +724,10 @@ impl<N: Marker> Node<N> {
DisplayExpr::from(self)
}

pub fn display_as_dot(&self) -> DisplayAsDot<'_, N> {
DisplayAsDot::from(self)
}

/// Encode a Simplicity expression to bits without any witness data.
pub fn encode_without_witness<W: io::Write>(&self, prog: W) -> io::Result<usize> {
let mut w = BitWriter::new(prog);
Expand Down
Loading