Refactor cli arguments

This commit is contained in:
imaqtkatt 2024-03-01 09:25:56 -03:00
parent e7edca1bc2
commit 5cf5d24086
5 changed files with 28 additions and 27 deletions

View File

@ -95,12 +95,16 @@ pub fn create_host(book: Arc<Book>, labels: Arc<Labels>, compile_opts: CompileOp
pub fn check_book(book: &mut Book) -> Result<(), Info> {
// TODO: Do the checks without having to do full compilation
// TODO: Shouldn't the check mode show warnings?
compile_book(book, CompileOpts::light())?;
compile_book(book, CompileOpts::light(), None)?;
Ok(())
}
pub fn compile_book(book: &mut Book, opts: CompileOpts) -> Result<CompileResult, Info> {
let warns = desugar_book(book, opts)?;
pub fn compile_book(
book: &mut Book,
opts: CompileOpts,
args: Option<Vec<Term>>,
) -> Result<CompileResult, Info> {
let warns = desugar_book(book, opts, args)?;
let (nets, labels) = book_to_nets(book);
let mut core_book = nets_to_hvmc(nets)?;
@ -113,11 +117,16 @@ pub fn compile_book(book: &mut Book, opts: CompileOpts) -> Result<CompileResult,
Ok(CompileResult { core_book, labels, warns })
}
pub fn desugar_book(book: &mut Book, opts: CompileOpts) -> Result<Vec<Warning>, Info> {
pub fn desugar_book(
book: &mut Book,
opts: CompileOpts,
args: Option<Vec<Term>>,
) -> Result<Vec<Warning>, Info> {
let mut ctx = Ctx::new(book);
ctx.check_shared_names();
ctx.set_entrypoint();
ctx.apply_args(args)?;
ctx.book.encode_adts(opts.adt_encoding);
ctx.book.encode_builtins();
@ -187,10 +196,7 @@ pub fn run_book(
compile_opts: CompileOpts,
args: Option<Vec<Term>>,
) -> Result<(Term, RunInfo), Info> {
let mut ctx = Ctx::new(&mut book);
ctx.set_entrypoint();
ctx.apply_args(args)?;
let CompileResult { core_book, labels, warns } = compile_book(&mut book, compile_opts)?;
let CompileResult { core_book, labels, warns } = compile_book(&mut book, compile_opts, args)?;
// Turn the book into an Arc so that we can use it for logging, debugging, etc.
// from anywhere else in the program

View File

@ -199,7 +199,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Info> {
}
let mut book = load_book(&path)?;
let compiled = compile_book(&mut book, opts)?;
let compiled = compile_book(&mut book, opts, None)?;
println!("{}", compiled.display_with_warns(warning_opts)?);
}
Mode::Desugar { path, comp_opts, lazy_mode } => {
@ -209,7 +209,7 @@ fn execute_cli_mode(mut cli: Cli) -> Result<(), Info> {
}
let mut book = load_book(&path)?;
// TODO: Shouldn't the desugar have `warn_opts` too? maybe WarningOpts::allow_all() by default
let _warns = desugar_book(&mut book, opts)?;
let _warns = desugar_book(&mut book, opts, None)?;
println!("{}", book);
}
Mode::Run {

View File

@ -28,11 +28,6 @@ impl Display for EntryErr {
impl Ctx<'_> {
pub fn set_entrypoint(&mut self) {
// already set
if self.book.entrypoint.is_some() {
return;
}
let mut entrypoint = None;
let (custom, main, hvm1_main) = self.book.get_possible_entry_points();

View File

@ -24,25 +24,25 @@ impl Ctx<'_> {
pub fn apply_args(&mut self, args: Option<Vec<Term>>) -> Result<(), Info> {
self.info.start_pass();
if let Some(entrypoint) = &self.book.entrypoint
&& let Some(args) = args
{
if let Some(entrypoint) = &self.book.entrypoint {
let main_def = &mut self.book.defs[entrypoint];
let expected = main_def.rules[0].pats.len();
let got = args.len();
if !main_def.rules[0].pats.iter().all(|pat| matches!(pat, Pattern::Var(Some(..)))) {
self.info.def_error(entrypoint.clone(), ArgError::PatternArgError);
}
let expected = main_def.rules[0].pats.len();
let got = if let Some(args) = &args { args.len() } else { 0 };
if expected != got {
self.info.error(ArgError::ArityArgError { expected, got });
}
main_def.convert_match_def_to_term();
let main_body = &mut self.book.defs[entrypoint].rule_mut().body;
if let Some(args) = args {
main_def.convert_match_def_to_term();
let main_body = &mut self.book.defs[entrypoint].rule_mut().body;
*main_body = Term::call(main_body.clone(), args);
*main_body = Term::call(main_body.clone(), args);
}
}
self.info.fatal(())

View File

@ -109,7 +109,7 @@ fn compile_term() {
fn compile_file_o_all() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
let compiled = compile_book(&mut book, CompileOpts::heavy())?;
let compiled = compile_book(&mut book, CompileOpts::heavy(), None)?;
Ok(format!("{:?}", compiled))
})
}
@ -117,7 +117,7 @@ fn compile_file_o_all() {
fn compile_file() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
let compiled = compile_book(&mut book, CompileOpts::light())?;
let compiled = compile_book(&mut book, CompileOpts::light(), None)?;
Ok(format!("{:?}", compiled))
})
}
@ -258,7 +258,7 @@ fn encode_pattern_match() {
fn desugar_file() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
desugar_book(&mut book, CompileOpts::light())?;
desugar_book(&mut book, CompileOpts::light(), None)?;
Ok(book.to_string())
})
}
@ -289,7 +289,7 @@ fn compile_entrypoint() {
run_golden_test_dir(function_name!(), &|code, path| {
let mut book = do_parse_book(code, path)?;
book.entrypoint = Some(Name::from("foo"));
let compiled = compile_book(&mut book, CompileOpts::light())?;
let compiled = compile_book(&mut book, CompileOpts::light(), None)?;
Ok(format!("{:?}", compiled))
})
}