1
0

Bring back the assignment error tests

This commit is contained in:
Jeff 2025-02-11 06:44:26 -05:00
parent e1e259141c
commit 00f35bd3ae
3 changed files with 116 additions and 20 deletions

View File

@ -106,3 +106,7 @@ path = "tests/logic/and_or.rs"
[[test]] [[test]]
name = "or_and" name = "or_and"
path = "tests/logic/or_and.rs" path = "tests/logic/or_and.rs"
[[test]]
name = "assignment_errors"
path = "tests/compiler_errors/assignment_errors.rs"

View File

@ -1239,7 +1239,6 @@ impl<'src> Compiler<'src> {
let mut item_type = Type::None; let mut item_type = Type::None;
let mut start_register = None; let mut start_register = None;
let mut close_instructions = Vec::new();
while !self.allow(Token::RightBracket)? { while !self.allow(Token::RightBracket)? {
let next_boolean_register = self.next_boolean_register(); let next_boolean_register = self.next_boolean_register();
@ -1271,22 +1270,26 @@ impl<'src> Compiler<'src> {
self.next_boolean_register() - next_boolean_register; self.next_boolean_register() - next_boolean_register;
if used_boolean_registers > 1 { if used_boolean_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_boolean_register, next_boolean_register,
self.next_boolean_register() - 2, self.next_boolean_register() - 2,
TypeCode::BOOLEAN, TypeCode::BOOLEAN,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
Type::Byte => { Type::Byte => {
let used_byte_registers = self.next_byte_register() - next_byte_register; let used_byte_registers = self.next_byte_register() - next_byte_register;
if used_byte_registers > 1 { if used_byte_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_byte_register, next_byte_register,
self.next_byte_register() - 2, self.next_byte_register() - 2,
TypeCode::BYTE, TypeCode::BYTE,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
Type::Character => { Type::Character => {
@ -1294,22 +1297,26 @@ impl<'src> Compiler<'src> {
self.next_character_register() - next_character_register; self.next_character_register() - next_character_register;
if used_character_registers > 1 { if used_character_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_character_register, next_character_register,
self.next_byte_register() - 2, self.next_character_register() - 2,
TypeCode::CHARACTER, TypeCode::CHARACTER,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
Type::Float => { Type::Float => {
let used_float_registers = self.next_float_register() - next_float_register; let used_float_registers = self.next_float_register() - next_float_register;
if used_float_registers > 1 { if used_float_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_float_register, next_float_register,
self.next_float_register() - 2, self.next_float_register() - 2,
TypeCode::FLOAT, TypeCode::FLOAT,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
Type::Integer => { Type::Integer => {
@ -1317,33 +1324,39 @@ impl<'src> Compiler<'src> {
self.next_integer_register() - next_integer_register; self.next_integer_register() - next_integer_register;
if used_integer_registers > 1 { if used_integer_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_integer_register, next_integer_register,
self.next_integer_register() - 2, self.next_integer_register() - 2,
TypeCode::INTEGER, TypeCode::INTEGER,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
Type::String => { Type::String => {
let used_string_registers = self.next_string_register() - next_string_register; let used_string_registers = self.next_string_register() - next_string_register;
if used_string_registers > 1 { if used_string_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_string_register, next_string_register,
self.next_string_register() - 2, self.next_string_register() - 2,
TypeCode::STRING, TypeCode::STRING,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
Type::List { .. } => { Type::List { .. } => {
let used_list_registers = self.next_list_register() - next_list_register; let used_list_registers = self.next_list_register() - next_list_register;
if used_list_registers > 1 { if used_list_registers > 1 {
close_instructions.push(Instruction::close( let close = Instruction::close(
next_list_register, next_list_register,
self.next_list_register() - 2, self.next_list_register() - 2,
TypeCode::LIST, TypeCode::LIST,
)); );
self.emit_instruction(close, Type::None, self.current_position);
} }
} }
_ => unimplemented!(), _ => unimplemented!(),
@ -1371,10 +1384,8 @@ impl<'src> Compiler<'src> {
); );
let list_length = end_register - start_register.unwrap_or(0) + 1; let list_length = end_register - start_register.unwrap_or(0) + 1;
if list_length > 1 { if list_length == 1 && self.get_last_operation() == Some(Operation::CLOSE) {
for close_instruction in close_instructions { self.instructions.pop();
self.emit_instruction(close_instruction, item_type.clone(), Span(start, end));
}
} }
self.emit_instruction( self.emit_instruction(

View File

@ -0,0 +1,81 @@
use dust_lang::*;
#[test]
fn add_assign_expects_mutable_variable() {
let source = "1 += 2";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::ExpectedMutableVariable {
found: Token::Integer("1").to_owned(),
position: Span(0, 1)
},
source
})
);
}
#[test]
fn divide_assign_expects_mutable_variable() {
let source = "1 -= 2";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::ExpectedMutableVariable {
found: Token::Integer("1").to_owned(),
position: Span(0, 1)
},
source
})
);
}
#[test]
fn multiply_assign_expects_mutable_variable() {
let source = "1 *= 2";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::ExpectedMutableVariable {
found: Token::Integer("1").to_owned(),
position: Span(0, 1)
},
source
})
);
}
#[test]
fn subtract_assign_expects_mutable_variable() {
let source = "1 -= 2";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::ExpectedMutableVariable {
found: Token::Integer("1").to_owned(),
position: Span(0, 1)
},
source
})
);
}
#[test]
fn modulo_assign_expects_mutable_variable() {
let source = "1 %= 2";
assert_eq!(
compile(source),
Err(DustError::Compile {
error: CompileError::ExpectedMutableVariable {
found: Token::Integer("1").to_owned(),
position: Span(0, 1)
},
source
})
);
}