From ec0dd6fa680129f851e425c5f4c9afd459815523 Mon Sep 17 00:00:00 2001 From: Renato Alencar Date: Fri, 17 May 2024 22:31:27 -0300 Subject: [PATCH 1/2] Add --hvm option in order to make it configurable --- src/lib.rs | 19 +++++++++++++++---- src/main.rs | 7 +++++-- tests/golden_tests.rs | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4a5b91a0..2411999a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,7 +171,7 @@ pub fn run_book( // cancel the run if a problem is detected. eprint!("{diagnostics}"); - let out = run_hvm(&core_book, cmd)?; + let out = run_hvm(&core_book, cmd, &run_opts)?; let (net, stats) = parse_hvm_output(&out)?; let (term, diags) = readback_hvm_net(&net, &book, &labels, run_opts.linear_readback, compile_opts.adt_encoding); @@ -196,7 +196,7 @@ pub fn readback_hvm_net( } /// Runs an HVM book by invoking HVM as a subprocess. -fn run_hvm(book: &::hvm::ast::Book, cmd: &str) -> Result { +fn run_hvm(book: &::hvm::ast::Book, cmd: &str, run_opts: &RunOpts) -> Result { fn filter_hvm_output( mut stream: impl std::io::Read + Send, mut output: impl std::io::Write + Send, @@ -245,7 +245,7 @@ fn run_hvm(book: &::hvm::ast::Book, cmd: &str) -> Result { let out_path = ".out.hvm"; std::fs::write(out_path, display_hvm_book(book).to_string()).map_err(|x| x.to_string())?; - let mut process = std::process::Command::new("hvm") + let mut process = std::process::Command::new(run_opts.hvm_path.clone()) .arg(cmd) .arg(out_path) .stdout(std::process::Stdio::piped()) @@ -279,10 +279,21 @@ fn parse_hvm_output(out: &str) -> Result<(::hvm::ast::Net, String), String> { Ok((net, stats.to_string())) } -#[derive(Clone, Copy, Debug, Default)] +#[derive(Clone, Debug)] pub struct RunOpts { pub linear_readback: bool, pub pretty: bool, + pub hvm_path: String, +} + +impl Default for RunOpts { + fn default() -> Self { + RunOpts { + linear_readback: false, + pretty: false, + hvm_path: "hvm".to_string() + } + } } #[derive(Clone, Copy, Debug, Default)] diff --git a/src/main.rs b/src/main.rs index 4f27c9f0..2a596193 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,9 @@ struct Cli { #[arg(short, long, global = true)] pub verbose: bool, + #[arg(long, global = true, default_value = "hvm", help = "Path to hvm binary")] + pub hvm_path: String, + #[arg(short = 'e', long, global = true, help = "Use other entrypoint rather than main or Main")] pub entrypoint: Option, } @@ -303,7 +306,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> { .map_err(|x| x.to_string())?; let gen_fn = |out_path: &str| { - let mut process = std::process::Command::new("hvm"); + let mut process = std::process::Command::new(cli.hvm_path); process.arg(gen_cmd).arg(out_path); process.output().map_err(|e| format!("While running hvm: {e}")) }; @@ -350,7 +353,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> { compile_opts.check_for_strict(); - let run_opts = RunOpts { linear_readback: linear, pretty }; + let run_opts = RunOpts { linear_readback: linear, pretty, hvm_path: cli.hvm_path }; let book = load_book(&path)?; if let Some((term, stats, diags)) = diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index a5394502..18a8b61b 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -175,7 +175,7 @@ fn run_file() { for adt_encoding in [AdtEncoding::NumScott, AdtEncoding::Scott] { let compile_opts = CompileOpts { adt_encoding, ..CompileOpts::default() }; - let (term, _, diags) = run_book_simple(book.clone(), run_opts, compile_opts, diagnostics_cfg, None)?; + let (term, _, diags) = run_book_simple(book.clone(), run_opts.clone(), compile_opts, diagnostics_cfg, None)?; res.push_str(&format!("{adt_encoding}:\n{diags}{term}\n\n")); } Ok(res) From 8e652500d8b5cbe6ca0337788d8ae1a5d8497649 Mon Sep 17 00:00:00 2001 From: Nicolas Abril Date: Fri, 31 May 2024 21:04:50 +0200 Subject: [PATCH 2/2] Run cargo fmt --- src/lib.rs | 6 +----- tests/golden_tests.rs | 3 ++- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2411999a..a9fd54d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -288,11 +288,7 @@ pub struct RunOpts { impl Default for RunOpts { fn default() -> Self { - RunOpts { - linear_readback: false, - pretty: false, - hvm_path: "hvm".to_string() - } + RunOpts { linear_readback: false, pretty: false, hvm_path: "hvm".to_string() } } } diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index 18a8b61b..d214277d 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -175,7 +175,8 @@ fn run_file() { for adt_encoding in [AdtEncoding::NumScott, AdtEncoding::Scott] { let compile_opts = CompileOpts { adt_encoding, ..CompileOpts::default() }; - let (term, _, diags) = run_book_simple(book.clone(), run_opts.clone(), compile_opts, diagnostics_cfg, None)?; + let (term, _, diags) = + run_book_simple(book.clone(), run_opts.clone(), compile_opts, diagnostics_cfg, None)?; res.push_str(&format!("{adt_encoding}:\n{diags}{term}\n\n")); } Ok(res)