feat: add lambda

This commit is contained in:
Kasey White 2022-05-06 00:51:00 -04:00
parent b3318e5f24
commit 6da0c829df
2 changed files with 41 additions and 7 deletions

View File

@ -1,3 +1,3 @@
(program 1.0.0
(con bool False)
[(lam x (con integer 4)) (con bool False)]
)

View File

@ -52,11 +52,19 @@ where
Input: Stream<Token = char>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
{
between(
choice((
attempt(between(token('['), token(']'), apply())),
attempt(between(
token('('),
token(')'),
choice((attempt(constant()), attempt(delay()), attempt(force()))),
)
choice((
attempt(delay()),
attempt(lambda()),
attempt(constant()),
attempt(force()),
)),
)),
))
.skip(spaces())
}
@ -90,6 +98,32 @@ where
.map(|term| Term::Force(Box::new(term)))
}
pub fn lambda<Input>() -> impl Parser<Input, Output = Term>
where
Input: Stream<Token = char>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
{
string("lam")
.with(skip_many1(space()))
.with((many1(alpha_num()), skip_many1(space()), term_()))
.map(|(parameter_name, _, term)| Term::Lambda {
parameter_name,
body: Box::new(term),
})
}
pub fn apply<Input>() -> impl Parser<Input, Output = Term>
where
Input: Stream<Token = char>,
Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
{
println!("daid");
(term_(), skip_many1(space()), term_()).map(|(function, _, argument)| Term::Apply {
function: Box::new(function),
argument: Box::new(argument),
})
}
pub fn constant<Input>() -> impl Parser<Input, Output = Term>
where
Input: Stream<Token = char>,