Updated closures

This commit is contained in:
2026-06-05 12:14:19 +02:00
parent 8df4351563
commit 0955bca5bc
4 changed files with 46 additions and 32 deletions
+5 -1
View File
@@ -7,6 +7,11 @@ pub enum Expression {
Literal(Literal),
Path(ExprPath),
Statement(Box<Statement>),
Closure {
return_type: Option<TypeExpr>,
params: Vec<VarDecl>,
body: Box<Expression>,
},
Fix {
initial: Box<Expression>,
prefixes: Vec<Prefix>,
@@ -72,7 +77,6 @@ pub enum Prefix {
Deref,
Not,
Neg,
Closure(Option<TypeExpr>, Vec<VarDecl>),
}
#[derive(Debug, Clone, Serialize)]
+4 -3
View File
@@ -30,6 +30,7 @@ keyword = {
| "false"
| "dyn"
| "loop"
| "fn"
}
identifier = @{
@@ -448,7 +449,7 @@ match_stmt = {
// CLOSURES
// ------------------------------------------------------
closure = { type_expr? ~ "|" ~ param_list ~ "|" }
closure = { type_expr? ~ "fn" ~ "(" ~ param_list ~ ")" ~ expr }
// ------------------------------------------------------
// TUPLES & ARRAYS
@@ -490,6 +491,7 @@ expr_path = {
primary = {
control_flow
| closure
| basic_stmt
| literal
| expr_path
@@ -571,8 +573,7 @@ not_px = { "!" }
neg_px = { "-" }
prefix = {
closure
| deref_px
deref_px
| mut_ref_px
| ref_px
| not_px
+10 -10
View File
@@ -75,6 +75,16 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Expression {
))
}
Rule::closure => {
ast_expr!(Self::Closure {
return_type: consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose(),
params: collect_recovered(inner.next().unwrap().into_inner()),
body: Expression::try_from(inner.next().unwrap()).map(Box::new),
})
}
Rule::primary => pair.into_inner().next().unwrap().try_into(),
Rule::static_path => ast_expr!(Expression::Path(pair.try_into())),
Rule::literal => ast_expr!(Expression::Literal(pair.try_into())),
@@ -108,16 +118,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Prefix {
Rule::ref_px => Self::Ref,
Rule::not_px => Self::Not,
Rule::neg_px => Self::Neg,
Rule::closure => {
let mut inner = pair.into_inner();
return ast_expr!(Self::Closure(
consume_rule(&mut inner, Rule::type_expr)
.map(TypeExpr::try_from)
.transpose(),
collect_recovered(inner.next().unwrap().into_inner())
));
}
_ => return AstError::bug_unimplemented(pair),
})