mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-09-11 03:45:29 +03:00
fix: decoupled bend lib and cli
This commit is contained in:
parent
8a394fc4e9
commit
c1859879a5
@ -1,12 +1,16 @@
|
||||
use super::tree_children;
|
||||
use crate::{diagnostics::Diagnostics, fun::Name};
|
||||
use crate::{diagnostics::Diagnostics, fun::Name, CompileOpts};
|
||||
use hvm::ast::{Book, Net, Tree};
|
||||
|
||||
pub const MAX_NET_SIZE_C: usize = 4095;
|
||||
pub const MAX_NET_SIZE_CUDA: usize = 64;
|
||||
|
||||
pub fn check_net_sizes(book: &Book, diagnostics: &mut Diagnostics, cmd: &str) -> Result<(), Diagnostics> {
|
||||
let net_size_bound = match cmd {
|
||||
pub fn check_net_sizes(
|
||||
book: &Book,
|
||||
diagnostics: &mut Diagnostics,
|
||||
opts: &CompileOpts,
|
||||
) -> Result<(), Diagnostics> {
|
||||
let net_size_bound = match opts.command.as_str() {
|
||||
"run-cu" | "gen-cu" | "gen-hvm" => MAX_NET_SIZE_CUDA,
|
||||
_ => MAX_NET_SIZE_C,
|
||||
};
|
||||
|
15
src/lib.rs
15
src/lib.rs
@ -35,7 +35,7 @@ pub fn check_book(
|
||||
compile_opts: CompileOpts,
|
||||
) -> Result<Diagnostics, Diagnostics> {
|
||||
// TODO: Do the checks without having to do full compilation
|
||||
let res = compile_book(book, compile_opts, diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(book, compile_opts, diagnostics_cfg, None)?;
|
||||
Ok(res.diagnostics)
|
||||
}
|
||||
|
||||
@ -44,7 +44,6 @@ pub fn compile_book(
|
||||
opts: CompileOpts,
|
||||
diagnostics_cfg: DiagnosticsConfig,
|
||||
args: Option<Vec<Term>>,
|
||||
cmd: Option<&str>,
|
||||
) -> Result<CompileResult, Diagnostics> {
|
||||
let mut diagnostics = desugar_book(book, opts.clone(), diagnostics_cfg, args)?;
|
||||
|
||||
@ -73,8 +72,8 @@ pub fn compile_book(
|
||||
prune_hvm_book(&mut hvm_book, &prune_entrypoints);
|
||||
}
|
||||
|
||||
if opts.check_net_size && cmd.is_some() {
|
||||
check_net_sizes(&hvm_book, &mut diagnostics, cmd.unwrap())?;
|
||||
if opts.check_net_size {
|
||||
check_net_sizes(&hvm_book, &mut diagnostics, &opts)?;
|
||||
}
|
||||
|
||||
add_recursive_priority(&mut hvm_book);
|
||||
@ -175,7 +174,7 @@ pub fn run_book(
|
||||
cmd: &str,
|
||||
) -> Result<Option<(Term, String, Diagnostics)>, Diagnostics> {
|
||||
let CompileResult { hvm_book: core_book, labels, diagnostics } =
|
||||
compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args, Some(cmd))?;
|
||||
compile_book(&mut book, compile_opts.clone(), diagnostics_cfg, args)?;
|
||||
|
||||
// TODO: Printing should be taken care by the cli module, but we'd
|
||||
// like to print any warnings before running so that the user can
|
||||
@ -332,6 +331,9 @@ impl OptLevel {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CompileOpts {
|
||||
/// The Bend command
|
||||
pub command: String,
|
||||
|
||||
/// Enables [hvm::eta_reduce].
|
||||
pub eta: bool,
|
||||
|
||||
@ -362,6 +364,7 @@ impl CompileOpts {
|
||||
#[must_use]
|
||||
pub fn set_all(self) -> Self {
|
||||
Self {
|
||||
command: self.command,
|
||||
eta: true,
|
||||
prune: true,
|
||||
float_combinators: true,
|
||||
@ -377,6 +380,7 @@ impl CompileOpts {
|
||||
#[must_use]
|
||||
pub fn set_no_all(self) -> Self {
|
||||
Self {
|
||||
command: self.command,
|
||||
eta: false,
|
||||
prune: false,
|
||||
linearize_matches: OptLevel::Disabled,
|
||||
@ -407,6 +411,7 @@ impl Default for CompileOpts {
|
||||
/// Uses num-scott ADT encoding.
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
command: String::from("run"),
|
||||
eta: true,
|
||||
prune: false,
|
||||
linearize_matches: OptLevel::Enabled,
|
||||
|
20
src/main.rs
20
src/main.rs
@ -190,10 +190,14 @@ pub enum OptArgs {
|
||||
AdtNumScott,
|
||||
}
|
||||
|
||||
fn compile_opts_from_cli(args: &Vec<OptArgs>) -> CompileOpts {
|
||||
fn compile_opts_from_cli(args: &Vec<OptArgs>, cmd: Option<&str>) -> CompileOpts {
|
||||
use OptArgs::*;
|
||||
let mut opts = CompileOpts::default();
|
||||
|
||||
if let Some(cmd) = cmd {
|
||||
opts.command = cmd.to_string();
|
||||
}
|
||||
|
||||
for arg in args {
|
||||
match arg {
|
||||
All => opts = opts.set_all(),
|
||||
@ -291,7 +295,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
|
||||
match cli.mode {
|
||||
Mode::Check { comp_opts, warn_opts, path } => {
|
||||
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
|
||||
let compile_opts = compile_opts_from_cli(&comp_opts);
|
||||
let compile_opts = compile_opts_from_cli(&comp_opts, None);
|
||||
|
||||
let mut book = load_book(&path, diagnostics_cfg)?;
|
||||
let diagnostics = check_book(&mut book, diagnostics_cfg, compile_opts)?;
|
||||
@ -300,10 +304,10 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
|
||||
|
||||
Mode::GenHvm(GenArgs { comp_opts, warn_opts, path, .. }) => {
|
||||
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
|
||||
let opts = compile_opts_from_cli(&comp_opts);
|
||||
let opts = compile_opts_from_cli(&comp_opts, Some("gen-hvm"));
|
||||
|
||||
let mut book = load_book(&path, diagnostics_cfg)?;
|
||||
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None, Some("gen-hvm"))?;
|
||||
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
|
||||
|
||||
eprint!("{}", compile_res.diagnostics);
|
||||
println!("{}", hvm_book_show_pretty(&compile_res.hvm_book));
|
||||
@ -317,7 +321,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
|
||||
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);
|
||||
let compile_opts = compile_opts_from_cli(&comp_opts, Some(run_cmd));
|
||||
|
||||
compile_opts.check_for_strict();
|
||||
|
||||
@ -342,10 +346,10 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
|
||||
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);
|
||||
let opts = compile_opts_from_cli(&comp_opts);
|
||||
let opts = compile_opts_from_cli(&comp_opts, Some(gen_cmd));
|
||||
|
||||
let mut book = load_book(&path, diagnostics_cfg)?;
|
||||
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None, Some(gen_cmd))?;
|
||||
let compile_res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
|
||||
|
||||
let out_path = ".out.hvm";
|
||||
std::fs::write(out_path, hvm_book_show_pretty(&compile_res.hvm_book)).map_err(|x| x.to_string())?;
|
||||
@ -373,7 +377,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Diagnostics> {
|
||||
Mode::Desugar { path, comp_opts, warn_opts, pretty } => {
|
||||
let diagnostics_cfg = set_warning_cfg_from_cli(DiagnosticsConfig::default(), warn_opts);
|
||||
|
||||
let opts = compile_opts_from_cli(&comp_opts);
|
||||
let opts = compile_opts_from_cli(&comp_opts, None);
|
||||
|
||||
let mut book = load_book(&path, diagnostics_cfg)?;
|
||||
let diagnostics = desugar_book(&mut book, opts, diagnostics_cfg, None)?;
|
||||
|
@ -99,7 +99,7 @@ fn compile_file() {
|
||||
let compile_opts = CompileOpts::default();
|
||||
let diagnostics_cfg = DiagnosticsConfig { unused_definition: Severity::Allow, ..Default::default() };
|
||||
|
||||
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None)?;
|
||||
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
|
||||
})
|
||||
}
|
||||
@ -115,7 +115,7 @@ fn compile_file_o_all() {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
|
||||
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
|
||||
})
|
||||
}
|
||||
@ -126,7 +126,7 @@ fn compile_file_o_no_all() {
|
||||
let mut book = do_parse_book_default(code, path)?;
|
||||
let compile_opts = CompileOpts::default().set_no_all();
|
||||
let diagnostics_cfg = DiagnosticsConfig::default();
|
||||
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(&mut book, compile_opts, diagnostics_cfg, None)?;
|
||||
Ok(hvm_book_show_pretty(&res.hvm_book).to_string())
|
||||
})
|
||||
}
|
||||
@ -373,7 +373,7 @@ fn compile_entrypoint() {
|
||||
let mut book = do_parse_book_default(code, path)?;
|
||||
book.entrypoint = Some(Name::new("foo"));
|
||||
let diagnostics_cfg = DiagnosticsConfig { ..DiagnosticsConfig::new(Severity::Error, true) };
|
||||
let res = compile_book(&mut book, CompileOpts::default(), diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(&mut book, CompileOpts::default(), diagnostics_cfg, None)?;
|
||||
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
|
||||
})
|
||||
}
|
||||
@ -421,7 +421,7 @@ fn mutual_recursion() {
|
||||
DiagnosticsConfig { recursion_cycle: Severity::Error, ..DiagnosticsConfig::new(Severity::Allow, true) };
|
||||
let mut book = do_parse_book_default(code, path)?;
|
||||
let opts = CompileOpts { merge: true, ..CompileOpts::default() };
|
||||
let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
|
||||
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
|
||||
})
|
||||
}
|
||||
@ -500,7 +500,7 @@ fn scott_triggers_unused() {
|
||||
let opts = CompileOpts::default();
|
||||
let diagnostics_cfg =
|
||||
DiagnosticsConfig { unused_definition: Severity::Error, ..DiagnosticsConfig::default() };
|
||||
let res = compile_book(&mut book, opts, diagnostics_cfg, None, None)?;
|
||||
let res = compile_book(&mut book, opts, diagnostics_cfg, None)?;
|
||||
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
|
||||
})
|
||||
}
|
||||
@ -517,7 +517,7 @@ fn compile_long() {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
compile_book(&mut book, opts.clone(), diagnostics_cfg, None, None)?;
|
||||
Ok("Compiled".to_string())
|
||||
let res = compile_book(&mut book, opts.clone(), diagnostics_cfg, None)?;
|
||||
Ok(format!("{}{}", res.diagnostics, hvm_book_show_pretty(&res.hvm_book)))
|
||||
})
|
||||
}
|
||||
|
@ -2,4 +2,6 @@
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/compile_long/huge_tree.bend
|
||||
---
|
||||
Compiled
|
||||
[4m[1m[31mErrors:[0m
|
||||
[1mIn definition '[4mmain[0m[1m':[0m
|
||||
Definition is too large for hvm (size=120002, max size=4095). Please break it into smaller pieces.
|
||||
|
Loading…
Reference in New Issue
Block a user