Skip to content

Commit

Permalink
Generate mermaid graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Nov 9, 2022
1 parent 7d50655 commit 467f143
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ impl ToInternedString for CodeBlock {
}
}

println!("\n{}\n", self.to_graph(interner).to_dot_format());
println!("\n{}\n", self.to_graph(interner).to_mermaid_format());

f
}
Expand Down
39 changes: 38 additions & 1 deletion boa_engine/src/vm/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Graph {
self.edges.push(edge);
}

pub fn to_dot_format(&self) -> String {
pub fn to_graphviz_format(&self) -> String {
let mut result = String::new();
result += "digraph {\n";
result += "\tnode [shape=record];\n";
Expand Down Expand Up @@ -131,4 +131,41 @@ impl Graph {
result += "}\n";
result
}

pub fn to_mermaid_format(&self) -> String {
let mut result = String::new();
let rankdir = match self.rank_direction {
RankDirection::TopToBottom => "TD",
RankDirection::LeftToRight => "LR",
RankDirection::RightToLeft => "RL",
};
result += &format!("graph {}\n", rankdir);

for node in &self.nodes {
let (shape_begin, shape_end) = match node.shape {
NodeShape::None | NodeShape::Record => ('[', ']'),
NodeShape::Diamond => ('{', '}'),
};
result += &format!(
" {}_i_{}{shape_begin}{}{shape_end}\n",
self.label, node.location, node.label
);
}

for edge in &self.edges {
let color = match edge.color {
NodeColor::None => "",
};
result += &format!(
" {}_i_{} -->| {}| {}_i_{}\n",
self.label,
edge.from,
edge.label.as_deref().unwrap_or(""),
self.label,
edge.to,
);
}
result += "\n";
result
}
}

0 comments on commit 467f143

Please sign in to comment.