bend/fun/transform/
apply_args.rs1use crate::{
2 diagnostics::Diagnostics,
3 fun::{Ctx, Pattern, Rule, Term},
4};
5
6impl Ctx<'_> {
7 pub fn apply_args(&mut self, args: Option<Vec<Term>>) -> Result<(), Diagnostics> {
18 if let Some(entrypoint) = &self.book.entrypoint {
19 let main_def = &mut self.book.defs[entrypoint];
20
21 let n_rules = main_def.rules.len();
23 if n_rules != 1 {
24 self.info.add_function_error(
25 format!("Expected the entrypoint function to have only one rule, found {n_rules}."),
26 entrypoint.clone(),
27 main_def.source.clone(),
28 );
29 }
30
31 let mut main_body = std::mem::take(&mut main_def.rules[0].body);
32
33 for pat in main_def.rules[0].pats.iter().rev() {
34 if let Pattern::Var(var) = pat {
35 main_body = Term::lam(Pattern::Var(var.clone()), main_body);
36 } else {
37 self.info.add_function_error(
38 format!("Expected the entrypoint function to only have variable patterns, found '{pat}'."),
39 entrypoint.clone(),
40 main_def.source.clone(),
41 );
42 }
43 }
44
45 if let Some(args) = args {
46 main_body = Term::call(main_body, args);
47 }
48
49 main_def.rules = vec![Rule { pats: vec![], body: main_body }];
50 }
51
52 self.info.fatal(())
53 }
54}