1
0

Disallow comparison chaining

This commit is contained in:
Jeff 2024-10-12 08:16:06 -04:00
parent f1034534ed
commit 5bbda1a24e
2 changed files with 19 additions and 7 deletions

View File

@ -215,13 +215,7 @@ impl Chunk {
register_index: u8, register_index: u8,
position: Span, position: Span,
) -> Result<u8, ChunkError> { ) -> Result<u8, ChunkError> {
log::debug!( log::debug!("Declare local {identifier}");
"Declaring local: {:?} {:?} {:?} {:?}",
identifier,
r#type,
is_mutable,
register_index
);
let starting_length = self.locals.len(); let starting_length = self.locals.len();
@ -253,6 +247,8 @@ impl Chunk {
} }
})?; })?;
log::debug!("Define local {}", local.identifier);
local.register_index = Some(register_index); local.register_index = Some(register_index);
Ok(()) Ok(())

View File

@ -553,6 +553,14 @@ impl<'src> Parser<'src> {
} }
fn parse_comparison_binary(&mut self) -> Result<(), ParseError> { 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) = let (left_instruction, left_position) =
self.chunk.pop_instruction(self.current_position)?; self.chunk.pop_instruction(self.current_position)?;
let (push_back_left, left_is_constant, _, left) = let (push_back_left, left_is_constant, _, left) =
@ -1499,6 +1507,9 @@ impl From<&Token<'_>> for ParseRule<'_> {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ParseError { pub enum ParseError {
CannotChainComparison {
position: Span,
},
CannotMutateImmutableVariable { CannotMutateImmutableVariable {
identifier: Identifier, identifier: Identifier,
position: Span, position: Span,
@ -1565,6 +1576,7 @@ impl AnnotatedError for ParseError {
fn description(&self) -> &'static str { fn description(&self) -> &'static str {
match self { match self {
Self::CannotChainComparison { .. } => "Cannot chain comparison",
Self::CannotMutateImmutableVariable { .. } => "Cannot mutate immutable variable", Self::CannotMutateImmutableVariable { .. } => "Cannot mutate immutable variable",
Self::ExpectedExpression { .. } => "Expected an expression", Self::ExpectedExpression { .. } => "Expected an expression",
Self::ExpectedToken { .. } => "Expected a specific token", Self::ExpectedToken { .. } => "Expected a specific token",
@ -1584,6 +1596,9 @@ impl AnnotatedError for ParseError {
fn details(&self) -> Option<String> { fn details(&self) -> Option<String> {
match self { match self {
Self::CannotChainComparison { .. } => {
Some("Cannot chain comparison operations".to_string())
}
Self::CannotMutateImmutableVariable { identifier, .. } => { Self::CannotMutateImmutableVariable { identifier, .. } => {
Some(format!("Cannot mutate immutable variable {identifier}")) Some(format!("Cannot mutate immutable variable {identifier}"))
} }
@ -1633,6 +1648,7 @@ impl AnnotatedError for ParseError {
fn position(&self) -> Span { fn position(&self) -> Span {
match self { match self {
Self::CannotChainComparison { position } => *position,
Self::CannotMutateImmutableVariable { position, .. } => *position, Self::CannotMutateImmutableVariable { position, .. } => *position,
Self::ExpectedExpression { position, .. } => *position, Self::ExpectedExpression { position, .. } => *position,
Self::ExpectedToken { position, .. } => *position, Self::ExpectedToken { position, .. } => *position,