Skip to content

Commit

Permalink
Improve apply_args code
Browse files Browse the repository at this point in the history
  • Loading branch information
imaqtkatt committed Feb 29, 2024
1 parent d2ab89f commit 25c51b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/term/check/set_entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl Display for EntryErr {
write!(f, "File has '{}', '{}' and '{}' definitions.", fnd[0], fnd[1], fnd[2])
}
EntryErr::MultipleRules => write!(f, "Main definition can't have more than one rule."),
EntryErr::Arguments(expected, got) => write!(f, "Main definition expects {expected} arguments, got {got}."),
EntryErr::Arguments(expected, got) => {
write!(f, "Main definition expects {expected} arguments, got {got}.")
}
}
}
}
Expand Down Expand Up @@ -71,7 +73,7 @@ impl Ctx<'_> {
fn validate_entry_point(entry: &Definition, given_arguments: usize) -> Result<Name, EntryErr> {
if entry.rules.len() > 1 {
Err(EntryErr::MultipleRules)
} else if !(entry.rules[0].pats.len() == given_arguments) {
} else if entry.rules[0].pats.len() != given_arguments {
Err(EntryErr::Arguments(entry.rules[0].pats.len(), given_arguments))
} else {
Ok(entry.name.clone())
Expand Down
17 changes: 7 additions & 10 deletions src/term/transform/apply_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ impl Book {
if let Some(main) = &self.entrypoint
&& let Some(args) = args
{
let mut args = args.into_iter();
let main_rule = &mut self.defs[main].rules[0];
let main_body = &mut main_rule.body;
let main_def = &mut self.defs[main];

for pat in &main_rule.pats {
if let Pattern::Var(Some(x)) = pat {
main_body.subst(x, &args.next().unwrap());
} else {
return Err(format!("Expected a variable pattern, but found '{pat}'."));
}
if !main_def.rules[0].pats.iter().all(|pat| matches!(pat, Pattern::Var(Some(..)))) {
return Err("Main definition should contain only var patterns.".into());
}

main_rule.pats.clear();
main_def.convert_match_def_to_term();
let main_body = &mut self.defs[main].rule_mut().body;

*main_body = Term::call(main_body.clone(), args);
}
Ok(())
}
Expand Down

0 comments on commit 25c51b6

Please sign in to comment.