1
0

Fix add tests

This commit is contained in:
Jeff 2025-02-17 00:02:33 -05:00
parent 7f939b693e
commit 7153cc16f2
2 changed files with 66 additions and 29 deletions

View File

@ -632,11 +632,22 @@ impl<'src> Compiler<'src> {
self.advance()?;
let destination = self.next_character_register();
let constant_index = self.character_constants.len() as u16;
let constant_index = if let Some(index) = self
.character_constants
.iter()
.position(|constant| constant == &character)
{
index as u16
} else {
let index = self.character_constants.len() as u16;
self.character_constants.push(character);
index
};
let load_constant =
Instruction::load_constant(destination, constant_index, TypeCode::CHARACTER, false);
self.character_constants.push(character);
self.emit_instruction(load_constant, Type::Character, position);
Ok(())
@ -662,11 +673,22 @@ impl<'src> Compiler<'src> {
position: self.previous_position,
})?;
let destination = self.next_float_register();
let constant_index = self.float_constants.len() as u16;
let constant_index = if let Some(index) = self
.float_constants
.iter()
.position(|constant| constant == &float)
{
index as u16
} else {
let index = self.float_constants.len() as u16;
self.float_constants.push(float);
index
};
let load_constant =
Instruction::load_constant(destination, constant_index, TypeCode::FLOAT, false);
self.float_constants.push(float);
self.emit_instruction(load_constant, Type::Float, position);
Ok(())
@ -697,12 +719,23 @@ impl<'src> Compiler<'src> {
integer = integer * 10 + digit;
}
let constant_index = self.integer_constants.len() as u16;
let constant_index = if let Some(index) = self
.integer_constants
.iter()
.position(|constant| constant == &integer)
{
index as u16
} else {
let index = self.integer_constants.len() as u16;
self.integer_constants.push(integer);
index
};
let destination = self.next_integer_register();
let load_constant =
Instruction::load_constant(destination, constant_index, TypeCode::INTEGER, false);
self.integer_constants.push(integer);
self.emit_instruction(load_constant, Type::Integer, position);
Ok(())
@ -722,7 +755,19 @@ impl<'src> Compiler<'src> {
self.advance()?;
let string = DustString::from(text);
let constant_index = self.push_or_get_constant_string(string);
let constant_index = if let Some(index) = self
.string_constants
.iter()
.position(|constant| constant == &string)
{
index as u16
} else {
let index = self.string_constants.len() as u16;
self.string_constants.push(string);
index
};
let destination = self.next_string_register();
let load_constant =
Instruction::load_constant(destination, constant_index, TypeCode::STRING, false);

View File

@ -1,6 +1,6 @@
use dust_lang::{
Chunk, ConcreteValue, DustString, FunctionType, Instruction, Operand, Span, Type, Value,
compile, instruction::TypeCode, run,
compile, instruction::TypeCode, run, Chunk, DustString, FunctionType, Instruction, Operand,
Span, Type, Value,
};
#[test]
@ -86,7 +86,7 @@ fn add_characters() {
Instruction::r#return(true, 0, TypeCode::STRING),
],
positions: vec![Span(0, 9), Span(9, 9)],
constants: vec![ConcreteValue::Character('a'), ConcreteValue::Character('b')],
character_constants: vec!['a', 'b'],
..Chunk::default()
};
let return_value = Some(Value::string("ab"));
@ -119,12 +119,7 @@ fn add_many_characters() {
Instruction::r#return(true, 2, TypeCode::STRING),
],
positions: vec![Span(0, 9), Span(0, 15), Span(0, 21), Span(21, 21)],
constants: vec![
ConcreteValue::Character('a'),
ConcreteValue::Character('b'),
ConcreteValue::Character('c'),
ConcreteValue::Character('d'),
],
character_constants: vec!['a', 'b', 'c', 'd'],
..Chunk::default()
};
let return_value = Some(Value::string("abcd"));
@ -147,7 +142,7 @@ fn add_floats() {
Instruction::r#return(true, 0, TypeCode::FLOAT),
],
positions: vec![Span(0, 12), Span(12, 12)],
constants: vec![ConcreteValue::Float(2.40), ConcreteValue::Float(40.02)],
float_constants: vec![2.40, 40.02],
..Chunk::default()
};
let return_value = Some(Value::float(42.42));
@ -180,7 +175,7 @@ fn add_many_floats() {
Instruction::r#return(true, 2, TypeCode::FLOAT),
],
positions: vec![Span(0, 12), Span(0, 19), Span(0, 27), Span(27, 27)],
constants: vec![ConcreteValue::Float(2.40), ConcreteValue::Float(40.02)],
float_constants: vec![2.40, 40.02],
..Chunk::default()
};
let return_value = Some(Value::float(84.84));
@ -203,7 +198,7 @@ fn add_integers() {
Instruction::r#return(true, 0, TypeCode::INTEGER),
],
positions: vec![Span(0, 6), Span(6, 6)],
constants: vec![ConcreteValue::Integer(40), ConcreteValue::Integer(2)],
integer_constants: vec![40, 2],
..Chunk::default()
};
let return_value = Some(Value::integer(42));
@ -236,7 +231,7 @@ fn add_many_integers() {
Instruction::r#return(true, 2, TypeCode::INTEGER),
],
positions: vec![Span(0, 6), Span(0, 11), Span(0, 15), Span(15, 15)],
constants: vec![ConcreteValue::Integer(40), ConcreteValue::Integer(2)],
integer_constants: vec![40, 2],
..Chunk::default()
};
let return_value = Some(Value::integer(84));
@ -259,10 +254,7 @@ fn add_strings() {
Instruction::r#return(true, 0, TypeCode::STRING),
],
positions: vec![Span(0, 20), Span(20, 20)],
constants: vec![
ConcreteValue::String(DustString::from("Hello, ")),
ConcreteValue::String(DustString::from("World!")),
],
string_constants: vec![DustString::from("Hello, "), DustString::from("World!")],
..Chunk::default()
};
let return_value = Some(Value::string("Hello, World!"));
@ -295,11 +287,11 @@ fn add_many_strings() {
Instruction::r#return(true, 2, TypeCode::STRING),
],
positions: vec![Span(0, 13), Span(0, 21), Span(0, 30), Span(30, 30)],
constants: vec![
ConcreteValue::String(DustString::from("foo")),
ConcreteValue::String(DustString::from("bar")),
ConcreteValue::String(DustString::from("baz")),
ConcreteValue::String(DustString::from("buzz")),
string_constants: vec![
DustString::from("foo"),
DustString::from("bar"),
DustString::from("baz"),
DustString::from("buzz"),
],
..Chunk::default()
};