correctly parse lambdas
This commit is contained in:
@@ -33,7 +33,7 @@ impl Display for ParseError {
|
|||||||
ParseError::UnmatchedArrayClose => write!(f, "there was an unmatched array closing operator `]`"),
|
ParseError::UnmatchedArrayClose => write!(f, "there was an unmatched array closing operator `]`"),
|
||||||
ParseError::TokenizeError(e) => write!(f, "Tokenizer Error: {e}"),
|
ParseError::TokenizeError(e) => write!(f, "Tokenizer Error: {e}"),
|
||||||
ParseError::ImmutableError(i) => write!(f, "attempt to redeclare {i} met with force"),
|
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))?));
|
f.body = Some(Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?));
|
||||||
assert!(f.body.is_some());
|
assert!(f.body.is_some());
|
||||||
|
|
||||||
println!("{:?} = {:?}", f.name, f);
|
|
||||||
|
|
||||||
Ok(ParseTree::FunctionDefinition(f, Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)))
|
Ok(ParseTree::FunctionDefinition(f, Box::new(ParseTree::parse(tokens, globals, &mut Cow::Borrowed(&locals))?)))
|
||||||
},
|
},
|
||||||
Op::Compose => two_arg!(Compose, tokens, globals, locals),
|
Op::Compose => two_arg!(Compose, tokens, globals, locals),
|
||||||
@@ -253,7 +251,7 @@ impl ParseTree {
|
|||||||
Op::And => two_arg!(And, tokens, globals, locals),
|
Op::And => two_arg!(And, tokens, globals, locals),
|
||||||
Op::Or => two_arg!(Or, tokens, globals, locals),
|
Op::Or => two_arg!(Or, tokens, globals, locals),
|
||||||
Op::LambdaDefine(arg_count) => {
|
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)?));
|
f.body = Some(Box::new(ParseTree::parse(tokens, globals, locals)?));
|
||||||
|
|
||||||
@@ -271,6 +269,14 @@ impl ParseTree {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_lambda<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, ParseError>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = Result<Token, TokenizeError>>,
|
||||||
|
{
|
||||||
|
let (t, args) = Self::parse_function_declaration(tokens, arg_count)?;
|
||||||
|
Ok(Function::lambda(t, args, None))
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_function<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, ParseError>
|
fn parse_function<I>(tokens: &mut Peekable<I>, arg_count: usize) -> Result<Function, ParseError>
|
||||||
where
|
where
|
||||||
I: Iterator<Item = Result<Token, TokenizeError>>,
|
I: Iterator<Item = Result<Token, TokenizeError>>,
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn uwu() {
|
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<Token> = Tokenizer::from_str(program).unwrap().collect::<Result<_, TokenizeError>>().unwrap();
|
let tokens: Vec<Token> = Tokenizer::from_str(program).unwrap().collect::<Result<_, TokenizeError>>().unwrap();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user