Introduce Closure

This commit is contained in:
Richard Feldman 2019-05-22 21:54:55 -04:00
parent 91aac9a86e
commit 3b34c82b42
2 changed files with 13 additions and 0 deletions

View file

@ -15,6 +15,7 @@ pub enum Expr {
Func(String, Box<Expr>),
Apply(Box<Expr>, Box<Expr>),
Operator(Box<Expr>, Operator, Box<Expr>),
Closure(Vec<Pattern>, Box<Expr>),
If(Box<Expr>, Box<Expr>, Box<Expr>),
}

View file

@ -263,6 +263,18 @@ where I: Stream<Item = char, Position = IndentablePosition>,
)
}
pub fn closure<I>(min_indent: i32) -> impl Parser<Input = I, Output = Expr>
where I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>
{
// TODO patterns must be separated by commas!
between(char('|'), char('|'), many1::<Vec<_>, _>(pattern()))
.and(expr_body(min_indent))
.map(|(patterns, closure_body)| {
Expr::Closure(patterns, Box::new(closure_body))
})
}
pub fn pattern<I>() -> impl Parser<Input = I, Output = Pattern>
where I: Stream<Item = char, Position = IndentablePosition>,
I::Error: ParseError<I::Item, I::Range, I::Position>