fix: fix comma precedence

it should be lower than assignement.
Now we can do stuff like a = 10, b = 30(before this commit we couldn't
this was a bug)
This commit is contained in:
Seymur Bagirov 2025-01-13 08:46:39 +04:00
parent 31e348643f
commit 129d03b74d

View File

@ -111,11 +111,18 @@ impl Parser<'_> {
}
fn expression(&mut self) -> Result<Expr, ParseError> {
self.assignment()
self.comma()
}
// Challenge #1. We're writing comma before equality, because it has the lowest precedence
// comma -> equality ("," equality)* ; // expression grammar
fn comma(&mut self) -> Result<Expr, ParseError> {
use TokenType::*;
self.left_association_binary(&[Comma], Self::assignment)
}
fn assignment(&mut self) -> Result<Expr, ParseError> {
let expr = self.comma()?;
let expr = self.ternary()?;
if self.match_token(&[TokenType::Equal]) {
let value = self.assignment()?;
@ -136,13 +143,6 @@ impl Parser<'_> {
Ok(expr)
}
// Challenge #1. We're writing comma before equality, because it has the lowest precedence
// comma -> equality ("," equality)* ; // expression grammar
fn comma(&mut self) -> Result<Expr, ParseError> {
use TokenType::*;
self.left_association_binary(&[Comma], Self::ternary)
}
// ternary -> equality ("?" expression : ternary)? // expression grammar
fn ternary(&mut self) -> Result<Expr, ParseError> {
use TokenType::*;