From ce4f6c7067ad529e95e21a16941ddf48a663ae91 Mon Sep 17 00:00:00 2001 From: Nicolas Abril Date: Mon, 3 Jun 2024 17:26:39 +0200 Subject: [PATCH] Rename hvm bin option to hvm-bin, add env var with same name --- src/main.rs | 76 +++++++++++-------- tests/golden_tests/cli/custom_hvm_bin.args | 3 + tests/golden_tests/cli/custom_hvm_bin.bend | 1 + tests/snapshots/cli__custom_hvm_bin.bend.snap | 5 ++ 4 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 tests/golden_tests/cli/custom_hvm_bin.args create mode 100644 tests/golden_tests/cli/custom_hvm_bin.bend create mode 100644 tests/snapshots/cli__custom_hvm_bin.bend.snap diff --git a/src/main.rs b/src/main.rs index dd341195..642ec330 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,8 +20,8 @@ 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(long, global = true, help = "HVM command or path to HVM binary")] + pub hvm_bin: Option, #[arg(short = 'e', long, global = true, help = "Use other entrypoint rather than main or Main")] pub entrypoint: Option, @@ -260,6 +260,16 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> { Ok(book) }; + // Path/command for the HVM binary + // CLI option -> Env var -> Default + let hvm_bin = if let Some(hvm_bin) = cli.hvm_bin { + hvm_bin + } else if let Ok(hvm_bin) = std::env::var("HVM_BIN") { + hvm_bin + } else { + "hvm".to_string() + }; + let gen_cmd = match &cli.mode { Mode::GenC(..) => "gen-c", Mode::GenCu(..) => "gen-cu", @@ -293,6 +303,36 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> { println!("{}", display_hvm_book(&compile_res.hvm_book)); } + Mode::Run(RunArgs { pretty, run_opts, comp_opts, warn_opts, path, arguments }) + | Mode::RunC(RunArgs { pretty, run_opts, comp_opts, warn_opts, path, arguments }) + | Mode::RunCu(RunArgs { pretty, run_opts, comp_opts, warn_opts, path, arguments }) => { + let CliRunOpts { linear, print_stats } = run_opts; + + let diagnostics_cfg = + set_warning_cfg_from_cli(DiagnosticsConfig::new(Severity::Allow, arg_verbose), warn_opts); + + let compile_opts = compile_opts_from_cli(&comp_opts); + + compile_opts.check_for_strict(); + + let run_opts = RunOpts { linear_readback: linear, pretty, hvm_path: hvm_bin }; + + let book = load_book(&path)?; + if let Some((term, stats, diags)) = + run_book(book, run_opts, compile_opts, diagnostics_cfg, arguments, run_cmd)? + { + eprint!("{diags}"); + if pretty { + println!("Result:\n{}", term.display_pretty(0)); + } else { + println!("Result: {}", term); + } + if print_stats { + println!("{stats}"); + } + } + } + Mode::GenC(GenArgs { comp_opts, warn_opts, path }) | Mode::GenCu(GenArgs { comp_opts, warn_opts, path }) => { let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts); @@ -306,7 +346,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(cli.hvm_path); + let mut process = std::process::Command::new(hvm_bin); process.arg(gen_cmd).arg(out_path); process.output().map_err(|e| format!("While running hvm: {e}")) }; @@ -340,36 +380,6 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> { println!("{book}"); } } - - Mode::Run(RunArgs { pretty, run_opts, comp_opts, warn_opts, path, arguments }) - | Mode::RunC(RunArgs { pretty, run_opts, comp_opts, warn_opts, path, arguments }) - | Mode::RunCu(RunArgs { pretty, run_opts, comp_opts, warn_opts, path, arguments }) => { - let CliRunOpts { linear, print_stats } = run_opts; - - let diagnostics_cfg = - set_warning_cfg_from_cli(DiagnosticsConfig::new(Severity::Allow, arg_verbose), warn_opts); - - let compile_opts = compile_opts_from_cli(&comp_opts); - - compile_opts.check_for_strict(); - - let run_opts = RunOpts { linear_readback: linear, pretty, hvm_path: cli.hvm_path }; - - let book = load_book(&path)?; - if let Some((term, stats, diags)) = - run_book(book, run_opts, compile_opts, diagnostics_cfg, arguments, run_cmd)? - { - eprint!("{diags}"); - if pretty { - println!("Result:\n{}", term.display_pretty(0)); - } else { - println!("Result: {}", term); - } - if print_stats { - println!("{stats}"); - } - } - } }; Ok(()) } diff --git a/tests/golden_tests/cli/custom_hvm_bin.args b/tests/golden_tests/cli/custom_hvm_bin.args new file mode 100644 index 00000000..280544cd --- /dev/null +++ b/tests/golden_tests/cli/custom_hvm_bin.args @@ -0,0 +1,3 @@ +run +tests/golden_tests/cli/custom_hvm_bin.bend +--hvm-bin=hvm diff --git a/tests/golden_tests/cli/custom_hvm_bin.bend b/tests/golden_tests/cli/custom_hvm_bin.bend new file mode 100644 index 00000000..dad22907 --- /dev/null +++ b/tests/golden_tests/cli/custom_hvm_bin.bend @@ -0,0 +1 @@ +main = @x x \ No newline at end of file diff --git a/tests/snapshots/cli__custom_hvm_bin.bend.snap b/tests/snapshots/cli__custom_hvm_bin.bend.snap new file mode 100644 index 00000000..e63a22bb --- /dev/null +++ b/tests/snapshots/cli__custom_hvm_bin.bend.snap @@ -0,0 +1,5 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/cli/custom_hvm_bin.bend +--- +Result: λa a