feat: implement #3 challenge for ch6

i kinda cheated, i was checking the answers and then stumbled upon the
grammar. Sorry :(
This commit is contained in:
Seymur Bagirov 2025-01-05 06:26:31 +04:00
parent 977fd9a8a8
commit 9f903556f9

View File

@ -90,6 +90,11 @@ impl Parser<'_> {
self.primary() self.primary()
} }
/* error boundaries:
("!=" | "==") equality
| (">" | ">=" | "<" | "<=") comparison
| ("+") term
| ("/" | "*") factor ; */
fn primary(&mut self) -> Result<Expr, ParseError> { fn primary(&mut self) -> Result<Expr, ParseError> {
use LiteralType::*; use LiteralType::*;
use TokenType::*; use TokenType::*;
@ -123,6 +128,38 @@ impl Parser<'_> {
}); });
} }
if self.match_token(&[Equal, BangEqual]) {
let _ = self.equality();
return Err(ParseError {
token: self.previous().clone(),
msg: "Missing left-hand operand.".to_string(),
});
}
if self.match_token(&[Greater, GreaterEqual, Less, LessEqual]) {
let _ = self.comparison();
return Err(ParseError {
token: self.previous().clone(),
msg: "Missing left-hand operand.".to_string(),
});
}
if self.match_token(&[Plus]) {
let _ = self.term();
return Err(ParseError {
token: self.previous().clone(),
msg: "Missing left-hand operand.".to_string(),
});
}
if self.match_token(&[Star, Slash]) {
let _ = self.factor();
return Err(ParseError {
token: self.previous().clone(),
msg: "Missing left-hand operand.".to_string(),
});
}
Err(ParseError { Err(ParseError {
token: self.peek().clone(), token: self.peek().clone(),
msg: "Expect expression.".to_string(), msg: "Expect expression.".to_string(),