1
0
Fork 0

Parse multiple BinaryOps and fix associated tests

This commit is contained in:
Vili Sinervä 2025-01-28 17:04:19 +02:00
parent b10f5f39fd
commit 3c45dcbb4c
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996

View file

@ -92,10 +92,16 @@ fn parse_term<'source>(pos: &mut usize, tokens: &[Token<'source>]) -> Expression
} }
fn parse_expression<'source>(pos: &mut usize, tokens: &[Token<'source>]) -> Expression<'source> { fn parse_expression<'source>(pos: &mut usize, tokens: &[Token<'source>]) -> Expression<'source> {
let left = parse_term(pos, tokens); let mut left = parse_term(pos, tokens);
let operator_token = next_expect_strings(pos, tokens, &vec!["+", "-"]);
let right = parse_term(pos, tokens); while vec!["+", "-"].contains(&peek(pos, tokens).text) {
Expression::BinaryOp(Box::new(left), operator_token.text, Box::new(right)) let operator_token = next_expect_strings(pos, tokens, &vec!["+", "-"]);
let right = parse_term(pos, tokens);
left = BinaryOp(Box::new(left), operator_token.text, Box::new(right));
}
left
} }
#[cfg(test)] #[cfg(test)]
@ -161,13 +167,13 @@ mod tests {
assert_eq!( assert_eq!(
result, result,
BinaryOp( BinaryOp(
Box::new(IntLiteral(1)),
"+",
Box::new(BinaryOp( Box::new(BinaryOp(
Box::new(IntLiteral(2)), Box::new(IntLiteral(1)),
"-", "+",
Box::new(IntLiteral(3)) Box::new(IntLiteral(2))
)) )),
"-",
Box::new(IntLiteral(3))
) )
); );
} }