Bring back the assignment error tests
This commit is contained in:
parent
e1e259141c
commit
00f35bd3ae
@ -106,3 +106,7 @@ path = "tests/logic/and_or.rs"
|
||||
[[test]]
|
||||
name = "or_and"
|
||||
path = "tests/logic/or_and.rs"
|
||||
|
||||
[[test]]
|
||||
name = "assignment_errors"
|
||||
path = "tests/compiler_errors/assignment_errors.rs"
|
||||
|
@ -1239,7 +1239,6 @@ impl<'src> Compiler<'src> {
|
||||
|
||||
let mut item_type = Type::None;
|
||||
let mut start_register = None;
|
||||
let mut close_instructions = Vec::new();
|
||||
|
||||
while !self.allow(Token::RightBracket)? {
|
||||
let next_boolean_register = self.next_boolean_register();
|
||||
@ -1271,22 +1270,26 @@ impl<'src> Compiler<'src> {
|
||||
self.next_boolean_register() - next_boolean_register;
|
||||
|
||||
if used_boolean_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_boolean_register,
|
||||
self.next_boolean_register() - 2,
|
||||
TypeCode::BOOLEAN,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
Type::Byte => {
|
||||
let used_byte_registers = self.next_byte_register() - next_byte_register;
|
||||
|
||||
if used_byte_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_byte_register,
|
||||
self.next_byte_register() - 2,
|
||||
TypeCode::BYTE,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
Type::Character => {
|
||||
@ -1294,22 +1297,26 @@ impl<'src> Compiler<'src> {
|
||||
self.next_character_register() - next_character_register;
|
||||
|
||||
if used_character_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_character_register,
|
||||
self.next_byte_register() - 2,
|
||||
self.next_character_register() - 2,
|
||||
TypeCode::CHARACTER,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
Type::Float => {
|
||||
let used_float_registers = self.next_float_register() - next_float_register;
|
||||
|
||||
if used_float_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_float_register,
|
||||
self.next_float_register() - 2,
|
||||
TypeCode::FLOAT,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
Type::Integer => {
|
||||
@ -1317,33 +1324,39 @@ impl<'src> Compiler<'src> {
|
||||
self.next_integer_register() - next_integer_register;
|
||||
|
||||
if used_integer_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_integer_register,
|
||||
self.next_integer_register() - 2,
|
||||
TypeCode::INTEGER,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
Type::String => {
|
||||
let used_string_registers = self.next_string_register() - next_string_register;
|
||||
|
||||
if used_string_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_string_register,
|
||||
self.next_string_register() - 2,
|
||||
TypeCode::STRING,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
Type::List { .. } => {
|
||||
let used_list_registers = self.next_list_register() - next_list_register;
|
||||
|
||||
if used_list_registers > 1 {
|
||||
close_instructions.push(Instruction::close(
|
||||
let close = Instruction::close(
|
||||
next_list_register,
|
||||
self.next_list_register() - 2,
|
||||
TypeCode::LIST,
|
||||
));
|
||||
);
|
||||
|
||||
self.emit_instruction(close, Type::None, self.current_position);
|
||||
}
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
@ -1371,10 +1384,8 @@ impl<'src> Compiler<'src> {
|
||||
);
|
||||
let list_length = end_register - start_register.unwrap_or(0) + 1;
|
||||
|
||||
if list_length > 1 {
|
||||
for close_instruction in close_instructions {
|
||||
self.emit_instruction(close_instruction, item_type.clone(), Span(start, end));
|
||||
}
|
||||
if list_length == 1 && self.get_last_operation() == Some(Operation::CLOSE) {
|
||||
self.instructions.pop();
|
||||
}
|
||||
|
||||
self.emit_instruction(
|
||||
|
81
dust-lang/tests/compiler_errors/assignment_errors.rs
Normal file
81
dust-lang/tests/compiler_errors/assignment_errors.rs
Normal 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
|
||||
})
|
||||
);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user