From 5bbda1a24e7f8174ecf5be824e682c4f4e15f025 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 12 Oct 2024 08:16:06 -0400 Subject: [PATCH] Disallow comparison chaining --- dust-lang/src/chunk.rs | 10 +++------- dust-lang/src/parser.rs | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/dust-lang/src/chunk.rs b/dust-lang/src/chunk.rs index 09d4158..3f5f8f7 100644 --- a/dust-lang/src/chunk.rs +++ b/dust-lang/src/chunk.rs @@ -215,13 +215,7 @@ impl Chunk { register_index: u8, position: Span, ) -> Result { - log::debug!( - "Declaring local: {:?} {:?} {:?} {:?}", - identifier, - r#type, - is_mutable, - register_index - ); + log::debug!("Declare local {identifier}"); let starting_length = self.locals.len(); @@ -253,6 +247,8 @@ impl Chunk { } })?; + log::debug!("Define local {}", local.identifier); + local.register_index = Some(register_index); Ok(()) diff --git a/dust-lang/src/parser.rs b/dust-lang/src/parser.rs index a9a3b59..de3fb7e 100644 --- a/dust-lang/src/parser.rs +++ b/dust-lang/src/parser.rs @@ -553,6 +553,14 @@ impl<'src> Parser<'src> { } fn parse_comparison_binary(&mut self) -> Result<(), ParseError> { + if let [Some(Operation::Jump), Some(Operation::Equal) | Some(Operation::Less) | Some(Operation::LessEqual)] = + self.chunk.get_last_n_operations() + { + return Err(ParseError::CannotChainComparison { + position: self.current_position, + }); + } + let (left_instruction, left_position) = self.chunk.pop_instruction(self.current_position)?; let (push_back_left, left_is_constant, _, left) = @@ -1499,6 +1507,9 @@ impl From<&Token<'_>> for ParseRule<'_> { #[derive(Debug, PartialEq)] pub enum ParseError { + CannotChainComparison { + position: Span, + }, CannotMutateImmutableVariable { identifier: Identifier, position: Span, @@ -1565,6 +1576,7 @@ impl AnnotatedError for ParseError { fn description(&self) -> &'static str { match self { + Self::CannotChainComparison { .. } => "Cannot chain comparison", Self::CannotMutateImmutableVariable { .. } => "Cannot mutate immutable variable", Self::ExpectedExpression { .. } => "Expected an expression", Self::ExpectedToken { .. } => "Expected a specific token", @@ -1584,6 +1596,9 @@ impl AnnotatedError for ParseError { fn details(&self) -> Option { match self { + Self::CannotChainComparison { .. } => { + Some("Cannot chain comparison operations".to_string()) + } Self::CannotMutateImmutableVariable { identifier, .. } => { Some(format!("Cannot mutate immutable variable {identifier}")) } @@ -1633,6 +1648,7 @@ impl AnnotatedError for ParseError { fn position(&self) -> Span { match self { + Self::CannotChainComparison { position } => *position, Self::CannotMutateImmutableVariable { position, .. } => *position, Self::ExpectedExpression { position, .. } => *position, Self::ExpectedToken { position, .. } => *position,