diff --git a/src/lib.rs b/src/lib.rs index e2721bee7..b2912035f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,12 +95,16 @@ pub fn create_host(book: Arc, labels: Arc, 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 { - let warns = desugar_book(book, opts)?; +pub fn compile_book( + book: &mut Book, + opts: CompileOpts, + args: Option>, +) -> Result { + 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 Result, Info> { +pub fn desugar_book( + book: &mut Book, + opts: CompileOpts, + args: Option>, +) -> Result, 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>, ) -> 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 diff --git a/src/main.rs b/src/main.rs index dfab69a56..23413bde6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 { diff --git a/src/term/check/set_entrypoint.rs b/src/term/check/set_entrypoint.rs index e9d7aff7c..1f0832a97 100644 --- a/src/term/check/set_entrypoint.rs +++ b/src/term/check/set_entrypoint.rs @@ -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(); diff --git a/src/term/transform/apply_args.rs b/src/term/transform/apply_args.rs index 8f3c9d184..5fe26cf39 100644 --- a/src/term/transform/apply_args.rs +++ b/src/term/transform/apply_args.rs @@ -24,25 +24,25 @@ impl Ctx<'_> { pub fn apply_args(&mut self, args: Option>) -> 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(()) diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index 2df2be992..0550b4ac1 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -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)) }) }