if conditions

This commit is contained in:
janis 2025-10-10 14:54:24 +02:00
parent 77bd4f3f16
commit f67bb61888
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -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)
})
}