Skip to content

Commit

Permalink
Add CLI option for generating flowgraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Nov 10, 2022
1 parent 2b1b457 commit 130d3eb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
42 changes: 41 additions & 1 deletion boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@
)]

use boa_ast::StatementList;
use boa_engine::Context;
use boa_engine::{
vm::{
graph::{Direction, Graph},
},
Context, JsResult,
};
use clap::{Parser, ValueEnum, ValueHint};
use colored::{Color, Colorize};
use rustyline::{config::Config, error::ReadlineError, EditMode, Editor};
Expand Down Expand Up @@ -104,6 +109,10 @@ struct Opt {
#[arg(long, short)]
trace: bool,

/// Generate instruction flowgraph. Default is Graphviz.
#[arg(long, value_name = "FORMAT", ignore_case = true, value_enum)]
flowgraph: Option<Option<FlowgraphFormat>>,

/// Use vi mode in the REPL
#[arg(long = "vi")]
vi_mode: bool,
Expand Down Expand Up @@ -136,6 +145,14 @@ enum DumpFormat {
JsonPretty,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum FlowgraphFormat {
/// Generates in graphviz format.
Graphviz,
/// Generates in mermaid format.
Mermaid,
}

/// Parses the the token stream into an AST and returns it.
///
/// Returns a error of type String with a message,
Expand Down Expand Up @@ -184,6 +201,19 @@ where
Ok(())
}

fn generate_flowgraph(context: &mut Context, src: &[u8], flowgraph: FlowgraphFormat) -> JsResult<String> {
let ast = context.parse(src)?;
let code = context.compile(&ast)?;

let mut graph = Graph::new(Direction::TopToBottom);
code.to_graph(context.interner(), graph.subgraph(String::default()));
let result = match flowgraph {
FlowgraphFormat::Graphviz => graph.to_graphviz_format(),
FlowgraphFormat::Mermaid => graph.to_mermaid_format(),
};
Ok(result)
}

pub fn main() -> Result<(), io::Error> {
let args = Opt::parse();

Expand All @@ -199,6 +229,11 @@ pub fn main() -> Result<(), io::Error> {
if let Err(e) = dump(&buffer, &args, &mut context) {
eprintln!("{e}");
}
} else if let Some(flowgraph) = args.flowgraph {
match generate_flowgraph(&mut context, &buffer, flowgraph.unwrap_or(FlowgraphFormat::Graphviz)) {
Ok(v) => println!("{}", v),
Err(v) => eprintln!("Uncaught {v}"),
}
} else {
match context.eval(&buffer) {
Ok(v) => println!("{}", v.display()),
Expand Down Expand Up @@ -245,6 +280,11 @@ pub fn main() -> Result<(), io::Error> {
if let Err(e) = dump(&line, &args, &mut context) {
eprintln!("{e}");
}
} else if let Some(flowgraph) = args.flowgraph {
match generate_flowgraph(&mut context, line.trim_end().as_bytes(), flowgraph.unwrap_or(FlowgraphFormat::Graphviz)) {
Ok(v) => println!("{}", v),
Err(v) => eprintln!("Uncaught {v}"),
}
} else {
match context.eval(line.trim_end()) {
Ok(v) => println!("{}", v.display()),
Expand Down
6 changes: 1 addition & 5 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
},
property::PropertyDescriptor,
vm::call_frame::GeneratorResumeKind,
vm::{call_frame::FinallyReturn, graph::Graph, CallFrame, Opcode},
vm::{call_frame::FinallyReturn, CallFrame, Opcode},
Context, JsResult, JsString, JsValue,
};
use boa_ast::{expression::Identifier, function::FormalParameterList};
Expand Down Expand Up @@ -465,10 +465,6 @@ impl ToInternedString for CodeBlock {
}
}

let mut graph = Graph::new(crate::vm::graph::Direction::TopToBottom);
self.to_graph(interner, graph.subgraph(String::default()));
println!("\n{}\n", graph.to_mermaid_format());

f
}
}
Expand Down
3 changes: 1 addition & 2 deletions boa_engine/src/vm/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ impl SubGraph {
};
let style = match edge.style {
EdgeStyle::Line => "-->",
EdgeStyle::Dotted => "-.->",
EdgeStyle::Dashed => "-.->",
EdgeStyle::Dotted | EdgeStyle::Dashed => "-.->",
};
result.push_str(&format!(
" {}_i_{} {style}| {}| {}_i_{}\n",
Expand Down

0 comments on commit 130d3eb

Please sign in to comment.