From 25c51b697984659574b29bd7b6834d4022a351c6 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Thu, 29 Feb 2024 13:12:17 -0300 Subject: [PATCH] Improve apply_args code --- src/term/check/set_entrypoint.rs | 6 ++++-- src/term/transform/apply_args.rs | 17 +++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/term/check/set_entrypoint.rs b/src/term/check/set_entrypoint.rs index f6f7dde95..d2eb61812 100644 --- a/src/term/check/set_entrypoint.rs +++ b/src/term/check/set_entrypoint.rs @@ -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}.") + } } } } @@ -71,7 +73,7 @@ impl Ctx<'_> { fn validate_entry_point(entry: &Definition, given_arguments: usize) -> Result { 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()) diff --git a/src/term/transform/apply_args.rs b/src/term/transform/apply_args.rs index 7ea9c8d15..6f64d21f8 100644 --- a/src/term/transform/apply_args.rs +++ b/src/term/transform/apply_args.rs @@ -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(()) }