Add tests for subtract instruction
This commit is contained in:
parent
dcea611a0b
commit
1bb87de70f
@ -328,7 +328,7 @@ impl Instruction {
|
|||||||
let register_type = match left.as_type() {
|
let register_type = match left.as_type() {
|
||||||
TypeCode::BOOLEAN => TypeCode::BOOLEAN,
|
TypeCode::BOOLEAN => TypeCode::BOOLEAN,
|
||||||
TypeCode::BYTE => TypeCode::BYTE,
|
TypeCode::BYTE => TypeCode::BYTE,
|
||||||
TypeCode::CHARACTER => TypeCode::STRING,
|
TypeCode::CHARACTER => TypeCode::STRING, // Adding characters concatenates them
|
||||||
TypeCode::INTEGER => TypeCode::INTEGER,
|
TypeCode::INTEGER => TypeCode::INTEGER,
|
||||||
TypeCode::FLOAT => TypeCode::FLOAT,
|
TypeCode::FLOAT => TypeCode::FLOAT,
|
||||||
TypeCode::STRING => TypeCode::STRING,
|
TypeCode::STRING => TypeCode::STRING,
|
||||||
@ -339,6 +339,13 @@ impl Instruction {
|
|||||||
|
|
||||||
Operand::Register(destination, register_type)
|
Operand::Register(destination, register_type)
|
||||||
}
|
}
|
||||||
|
Operation::SUBTRACT => {
|
||||||
|
let Subtract {
|
||||||
|
destination, left, ..
|
||||||
|
} = Subtract::from(*self);
|
||||||
|
|
||||||
|
Operand::Register(destination, left.as_type())
|
||||||
|
}
|
||||||
unsupported => todo!("Support {unsupported}"),
|
unsupported => todo!("Support {unsupported}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -593,7 +593,91 @@ pub fn add(instruction: InstructionFields, thread: &mut Thread) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subtract(instruction: InstructionFields, thread: &mut Thread) {}
|
pub fn subtract(instruction: InstructionFields, thread: &mut Thread) {
|
||||||
|
let destination = instruction.a_field as usize;
|
||||||
|
let left = instruction.b_field as usize;
|
||||||
|
let left_type = instruction.b_type;
|
||||||
|
let left_is_constant = instruction.b_is_constant;
|
||||||
|
let right = instruction.c_field as usize;
|
||||||
|
let right_type = instruction.c_type;
|
||||||
|
let right_is_constant = instruction.c_is_constant;
|
||||||
|
|
||||||
|
match (left_type, right_type) {
|
||||||
|
(TypeCode::INTEGER, TypeCode::INTEGER) => {
|
||||||
|
let left_value = if left_is_constant {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
thread.get_constant(left).as_integer().unwrap()
|
||||||
|
} else {
|
||||||
|
unsafe { thread.get_constant(left).as_integer().unwrap_unchecked() }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thread.get_integer_register(left)
|
||||||
|
};
|
||||||
|
let right_value = if right_is_constant {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
thread.get_constant(right).as_integer().unwrap()
|
||||||
|
} else {
|
||||||
|
unsafe { thread.get_constant(right).as_integer().unwrap_unchecked() }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thread.get_integer_register(right)
|
||||||
|
};
|
||||||
|
let result = left_value.saturating_sub(*right_value);
|
||||||
|
let register = Register::Value(result);
|
||||||
|
|
||||||
|
thread.set_integer_register(destination, register);
|
||||||
|
}
|
||||||
|
(TypeCode::BYTE, TypeCode::BYTE) => {
|
||||||
|
let left_value = if left_is_constant {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
thread.get_constant(left).as_byte().unwrap()
|
||||||
|
} else {
|
||||||
|
unsafe { thread.get_constant(left).as_byte().unwrap_unchecked() }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thread.get_byte_register(left)
|
||||||
|
};
|
||||||
|
let right_value = if right_is_constant {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
thread.get_constant(right).as_byte().unwrap()
|
||||||
|
} else {
|
||||||
|
unsafe { thread.get_constant(right).as_byte().unwrap_unchecked() }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thread.get_byte_register(right)
|
||||||
|
};
|
||||||
|
let result = left_value.saturating_sub(*right_value);
|
||||||
|
let register = Register::Value(result);
|
||||||
|
|
||||||
|
thread.set_byte_register(destination, register);
|
||||||
|
}
|
||||||
|
(TypeCode::FLOAT, TypeCode::FLOAT) => {
|
||||||
|
let left_value = if left_is_constant {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
thread.get_constant(left).as_float().unwrap()
|
||||||
|
} else {
|
||||||
|
unsafe { thread.get_constant(left).as_float().unwrap_unchecked() }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thread.get_float_register(left)
|
||||||
|
};
|
||||||
|
let right_value = if right_is_constant {
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
|
thread.get_constant(right).as_float().unwrap()
|
||||||
|
} else {
|
||||||
|
unsafe { thread.get_constant(right).as_float().unwrap_unchecked() }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
thread.get_float_register(right)
|
||||||
|
};
|
||||||
|
let result = left_value - right_value;
|
||||||
|
let register = Register::Value(result);
|
||||||
|
|
||||||
|
thread.set_float_register(destination, register);
|
||||||
|
}
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn multiply(instruction: InstructionFields, thread: &mut Thread) {
|
pub fn multiply(instruction: InstructionFields, thread: &mut Thread) {
|
||||||
let destination = instruction.a_field as usize;
|
let destination = instruction.a_field as usize;
|
||||||
|
@ -0,0 +1,167 @@
|
|||||||
|
use dust_lang::{
|
||||||
|
Chunk, ConcreteValue, FunctionType, Instruction, Operand, Span, Type, Value, compile,
|
||||||
|
instruction::TypeCode, run,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subtract_bytes() {
|
||||||
|
let source = "0x28 - 0x02";
|
||||||
|
let chunk = Chunk {
|
||||||
|
r#type: FunctionType::new([], [], Type::Byte),
|
||||||
|
instructions: vec![
|
||||||
|
Instruction::load_encoded(0, 40, TypeCode::BYTE, false),
|
||||||
|
Instruction::load_encoded(1, 2, TypeCode::BYTE, false),
|
||||||
|
Instruction::subtract(
|
||||||
|
2,
|
||||||
|
Operand::Register(0, TypeCode::BYTE),
|
||||||
|
Operand::Register(1, TypeCode::BYTE),
|
||||||
|
),
|
||||||
|
Instruction::r#return(true, 2, TypeCode::BYTE),
|
||||||
|
],
|
||||||
|
positions: vec![Span(0, 4), Span(7, 11), Span(0, 11), Span(11, 11)],
|
||||||
|
..Chunk::default()
|
||||||
|
};
|
||||||
|
let return_value = Some(Value::byte(0x26));
|
||||||
|
|
||||||
|
assert_eq!(chunk, compile(source).unwrap());
|
||||||
|
assert_eq!(return_value, run(source).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subtract_many_bytes() {
|
||||||
|
let source = "0x28 - 0x02 - 0x02";
|
||||||
|
let chunk = Chunk {
|
||||||
|
r#type: FunctionType::new([], [], Type::Byte),
|
||||||
|
instructions: vec![
|
||||||
|
Instruction::load_encoded(0, 40, TypeCode::BYTE, false),
|
||||||
|
Instruction::load_encoded(1, 2, TypeCode::BYTE, false),
|
||||||
|
Instruction::subtract(
|
||||||
|
2,
|
||||||
|
Operand::Register(0, TypeCode::BYTE),
|
||||||
|
Operand::Register(1, TypeCode::BYTE),
|
||||||
|
),
|
||||||
|
Instruction::load_encoded(3, 2, TypeCode::BYTE, false),
|
||||||
|
Instruction::subtract(
|
||||||
|
4,
|
||||||
|
Operand::Register(2, TypeCode::BYTE),
|
||||||
|
Operand::Register(3, TypeCode::BYTE),
|
||||||
|
),
|
||||||
|
Instruction::r#return(true, 4, TypeCode::BYTE),
|
||||||
|
],
|
||||||
|
positions: vec![
|
||||||
|
Span(0, 4),
|
||||||
|
Span(7, 11),
|
||||||
|
Span(0, 11),
|
||||||
|
Span(14, 18),
|
||||||
|
Span(0, 18),
|
||||||
|
Span(18, 18),
|
||||||
|
],
|
||||||
|
..Chunk::default()
|
||||||
|
};
|
||||||
|
let return_value = Some(Value::byte(0x24));
|
||||||
|
|
||||||
|
assert_eq!(chunk, compile(source).unwrap());
|
||||||
|
assert_eq!(return_value, run(source).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_floats() {
|
||||||
|
let source = "0.5 - 0.25";
|
||||||
|
let chunk = Chunk {
|
||||||
|
r#type: FunctionType::new([], [], Type::Float),
|
||||||
|
instructions: vec![
|
||||||
|
Instruction::subtract(
|
||||||
|
0,
|
||||||
|
Operand::Constant(0, TypeCode::FLOAT),
|
||||||
|
Operand::Constant(1, TypeCode::FLOAT),
|
||||||
|
),
|
||||||
|
Instruction::r#return(true, 0, TypeCode::FLOAT),
|
||||||
|
],
|
||||||
|
positions: vec![Span(0, 10), Span(10, 10)],
|
||||||
|
constants: vec![ConcreteValue::Float(0.5), ConcreteValue::Float(0.25)],
|
||||||
|
..Chunk::default()
|
||||||
|
};
|
||||||
|
let return_value = Some(Value::float(0.25));
|
||||||
|
|
||||||
|
assert_eq!(chunk, compile(source).unwrap());
|
||||||
|
assert_eq!(return_value, run(source).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_many_floats() {
|
||||||
|
let source = "0.5 - 0.25 - 0.25";
|
||||||
|
let chunk = Chunk {
|
||||||
|
r#type: FunctionType::new([], [], Type::Float),
|
||||||
|
instructions: vec![
|
||||||
|
Instruction::subtract(
|
||||||
|
0,
|
||||||
|
Operand::Constant(0, TypeCode::FLOAT),
|
||||||
|
Operand::Constant(1, TypeCode::FLOAT),
|
||||||
|
),
|
||||||
|
Instruction::subtract(
|
||||||
|
1,
|
||||||
|
Operand::Register(0, TypeCode::FLOAT),
|
||||||
|
Operand::Constant(1, TypeCode::FLOAT),
|
||||||
|
),
|
||||||
|
Instruction::r#return(true, 1, TypeCode::FLOAT),
|
||||||
|
],
|
||||||
|
positions: vec![Span(0, 10), Span(0, 17), Span(17, 17)],
|
||||||
|
constants: vec![ConcreteValue::Float(0.5), ConcreteValue::Float(0.25)],
|
||||||
|
..Chunk::default()
|
||||||
|
};
|
||||||
|
let return_value = Some(Value::float(0.0));
|
||||||
|
|
||||||
|
assert_eq!(chunk, compile(source).unwrap());
|
||||||
|
assert_eq!(return_value, run(source).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subtract_integers() {
|
||||||
|
let source = "10 - 5";
|
||||||
|
let chunk = Chunk {
|
||||||
|
r#type: FunctionType::new([], [], Type::Integer),
|
||||||
|
instructions: vec![
|
||||||
|
Instruction::subtract(
|
||||||
|
0,
|
||||||
|
Operand::Constant(0, TypeCode::INTEGER),
|
||||||
|
Operand::Constant(1, TypeCode::INTEGER),
|
||||||
|
),
|
||||||
|
Instruction::r#return(true, 0, TypeCode::INTEGER),
|
||||||
|
],
|
||||||
|
positions: vec![Span(0, 6), Span(6, 6)],
|
||||||
|
constants: vec![ConcreteValue::Integer(10), ConcreteValue::Integer(5)],
|
||||||
|
..Chunk::default()
|
||||||
|
};
|
||||||
|
let return_value = Some(Value::integer(5));
|
||||||
|
|
||||||
|
assert_eq!(chunk, compile(source).unwrap());
|
||||||
|
assert_eq!(return_value, run(source).unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn subtract_many_integers() {
|
||||||
|
let source = "10 - 5 - 5";
|
||||||
|
let chunk = Chunk {
|
||||||
|
r#type: FunctionType::new([], [], Type::Integer),
|
||||||
|
instructions: vec![
|
||||||
|
Instruction::subtract(
|
||||||
|
0,
|
||||||
|
Operand::Constant(0, TypeCode::INTEGER),
|
||||||
|
Operand::Constant(1, TypeCode::INTEGER),
|
||||||
|
),
|
||||||
|
Instruction::subtract(
|
||||||
|
1,
|
||||||
|
Operand::Register(0, TypeCode::INTEGER),
|
||||||
|
Operand::Constant(1, TypeCode::INTEGER),
|
||||||
|
),
|
||||||
|
Instruction::r#return(true, 1, TypeCode::INTEGER),
|
||||||
|
],
|
||||||
|
positions: vec![Span(0, 6), Span(0, 10), Span(10, 10)],
|
||||||
|
constants: vec![ConcreteValue::Integer(10), ConcreteValue::Integer(5)],
|
||||||
|
..Chunk::default()
|
||||||
|
};
|
||||||
|
let return_value = Some(Value::integer(0));
|
||||||
|
|
||||||
|
assert_eq!(chunk, compile(source).unwrap());
|
||||||
|
assert_eq!(return_value, run(source).unwrap());
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user