1
0
This commit is contained in:
Jeff 2024-09-15 06:33:56 -04:00
parent b66710e3eb
commit 9cb6873618
2 changed files with 26 additions and 11 deletions

View File

@ -227,16 +227,7 @@ impl<'src> Parser<'src> {
fn parse_grouped(&mut self, _allow_assignment: bool) -> Result<(), ParseError> { fn parse_grouped(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
self.parse_expression()?; self.parse_expression()?;
self.expect(TokenKind::RightParenthesis)
if self.previous_token == Token::RightParenthesis {
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::RightParenthesis,
found: self.current_token.to_owned(),
position: self.current_position,
})
}
} }
fn parse_unary(&mut self, _allow_assignment: bool) -> Result<(), ParseError> { fn parse_unary(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {

View File

@ -2,6 +2,30 @@ use crate::Local;
use super::*; use super::*;
#[test]
fn parentheses_precedence() {
assert_eq!(
parse("(1 + 2) * 3"),
Ok(Chunk::with_data(
vec![
(
*Instruction::add(0, 0, 1)
.set_first_argument_to_constant()
.set_second_argument_to_constant(),
Span(3, 4)
),
(
*Instruction::multiply(1, 0, 2).set_second_argument_to_constant(),
Span(8, 9)
),
(Instruction::r#return(), Span(0, 11)),
],
vec![Value::integer(1), Value::integer(2), Value::integer(3)],
vec![]
))
);
}
#[test] #[test]
fn add_multiply_precedence() { fn add_multiply_precedence() {
assert_eq!( assert_eq!(
@ -42,7 +66,7 @@ fn let_statement() {
} }
#[test] #[test]
fn integer() { fn constant() {
assert_eq!( assert_eq!(
parse("42"), parse("42"),
Ok(Chunk::with_data( Ok(Chunk::with_data(