fix "if" parsing

This commit is contained in:
minish 2025-07-20 00:06:32 -04:00
parent 1510dee5ae
commit 56a07748cd
Signed by: min
SSH Key Fingerprint: SHA256:h4k7JNrfe1dzv1WE3oGVeAY9DPSZXIu3/j89+6DtHWE
1 changed files with 39 additions and 32 deletions

View File

@ -137,26 +137,23 @@ where
// unary ops!! (prefix) // unary ops!! (prefix)
t if t.prefix_precedence().is_some() => { t if t.prefix_precedence().is_some() => {
let prec = t.prefix_precedence().unwrap(); let prec = t.prefix_precedence().unwrap();
match t {
// parse function // parse function
if matches!(t, Token::Func) { Token::Func => {
// parse args // parse args
self.expect_next(TokenKind::ParenOpen)?; self.expect_next(TokenKind::ParenOpen)?;
let args = let args = self
self.parse_delimited_until(TokenKind::Comma, Some(TokenKind::ParenClose))?; .parse_delimited_until(TokenKind::Comma, Some(TokenKind::ParenClose))?;
self.eat(); self.eat();
// parse body // parse body
let body = self.parse_expr(prec, in_group)?; let body = self.parse_expr(prec, in_group)?;
// pack // pack
Box::new(Expr::Func(args, body)) Box::new(Expr::Func(args, body))
} else { }
let rhs = self.parse_expr(prec, in_group)?; // parse if
Box::new(match t {
Token::Minus => Expr::Negate(rhs),
Token::Not => Expr::Not(rhs),
Token::Return => Expr::Return(rhs),
Token::If => { Token::If => {
// parse the condition
let cond = self.parse_expr(Precedence::Min, false)?;
// parse the true case // parse the true case
let true_case = self.parse_expr(prec, in_group)?; let true_case = self.parse_expr(prec, in_group)?;
// and maybe a false case // and maybe a false case
@ -167,12 +164,22 @@ where
}) })
.transpose()?; .transpose()?;
// pack // pack
Expr::If(rhs, true_case, false_case) Box::new(Expr::If(cond, true_case, false_case))
} }
// another op
_ => {
let rhs = self.parse_expr(prec, in_group)?;
Box::new(match t {
Token::Minus => Expr::Negate(rhs),
Token::Not => Expr::Not(rhs),
Token::Return => Expr::Return(rhs),
_ => unreachable!(), _ => unreachable!(),
}) })
} }
} }
}
// unexpected token // unexpected token
t => return Err(ParseError::UnexpectedToken(t)), t => return Err(ParseError::UnexpectedToken(t)),