From d0661512c907a10899f13229caf8c5760b1b7036 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Wed, 16 Oct 2024 16:50:57 -0400 Subject: [PATCH] correctly parse lambdas --- src/parser.rs | 14 ++++++++++---- src/tokenizer.rs | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 7f37b00..cd9899d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -33,7 +33,7 @@ impl Display for ParseError { ParseError::UnmatchedArrayClose => write!(f, "there was an unmatched array closing operator `]`"), ParseError::TokenizeError(e) => write!(f, "Tokenizer Error: {e}"), ParseError::ImmutableError(i) => write!(f, "attempt to redeclare {i} met with force"), - ParseError::UnwantedToken(_t) => write!(f, "unexpected token"), + ParseError::UnwantedToken(t) => write!(f, "unexpected token {t:?}"), } } } @@ -197,8 +197,6 @@ impl ParseTree { f.body = Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)); assert!(f.body.is_some()); - println!("{:?} = {:?}", f.name, f); - Ok(ParseTree::FunctionDefinition(f, Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?))) }, Op::Compose => two_arg!(Compose, tokens, globals, locals), @@ -253,7 +251,7 @@ impl ParseTree { Op::And => two_arg!(And, tokens, globals, locals), Op::Or => two_arg!(Or, tokens, globals, locals), Op::LambdaDefine(arg_count) => { - let mut f = ParseTree::parse_function(tokens, arg_count)?; + let mut f = ParseTree::parse_lambda(tokens, arg_count)?; f.body = Some(Box::new(ParseTree::parse(tokens, globals, locals)?)); @@ -271,6 +269,14 @@ impl ParseTree { } } + fn parse_lambda(tokens: &mut Peekable, arg_count: usize) -> Result + where + I: Iterator>, + { + let (t, args) = Self::parse_function_declaration(tokens, arg_count)?; + Ok(Function::lambda(t, args, None)) + } + fn parse_function(tokens: &mut Peekable, arg_count: usize) -> Result where I: Iterator>, diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 3dbb045..e24b6bf 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -413,7 +413,7 @@ mod tests { #[test] fn uwu() { - let program = ": id ?. x Any -> Any x id 5"; + let program = ":. apply : f x f x apply ; x ** x 2 10"; let tokens: Vec = Tokenizer::from_str(program).unwrap().collect::>().unwrap();