From f67bb6188802620a2d4af067974e787728b2fc5f Mon Sep 17 00:00:00 2001 From: janis Date: Fri, 10 Oct 2025 14:54:24 +0200 Subject: [PATCH] if conditions --- crates/parser/src/lib.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 087b1cf..e4be2e3 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -605,7 +605,7 @@ fn expr<'a>() -> impl Parser<'a, TokenInput<'a>, Index, ParserExtra> { let field = just(Token::Dot).ignore_then(select! {Token::Ident(ident) => ident}); - let expr = simple.pratt(( + let assignment_expr = simple.pratt(( postfix(100, subscript, |expr, index, e: &mut E| { let node = AstNode::Subscript { expr, index }; e.state().push(node) @@ -729,6 +729,27 @@ fn expr<'a>() -> impl Parser<'a, TokenInput<'a>, Index, ParserExtra> { }), )); + let else_expr = just(Token::Else).ignore_then(_expr.clone()); + + let if_expr = just(Token::If) + .ignore_then( + _expr + .clone() + .delimited_by(just(Token::OpenParens), just(Token::CloseParens)), + ) + .then(_expr.clone()) + .then(else_expr.or_not()) + .map_with(|((condition, then), r#else), e: &mut E| { + let node = AstNode::If { + condition, + then, + r#else, + }; + e.state().push(node) + }); + + let expr = choice((if_expr, assignment_expr)).labelled("expression"); + Arc::new(expr) }) }