Skip to content

Commit

Permalink
Refactor cli arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
imaqtkatt committed Mar 1, 2024
1 parent e7edca1 commit 5cf5d24
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
22 changes: 14 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand All @@ -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 {
Expand Down
5 changes: 0 additions & 5 deletions src/term/check/set_entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions src/term/transform/apply_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
8 changes: 4 additions & 4 deletions tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ 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))
})
}
#[test]
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))
})
}
Expand Down Expand Up @@ -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())
})
}
Expand Down Expand Up @@ -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))
})
}
Expand Down

0 comments on commit 5cf5d24

Please sign in to comment.