1
0

Fix tests

This commit is contained in:
Jeff 2024-12-25 10:04:35 -05:00
parent 71a68c54e4
commit dc3cc13b12
7 changed files with 120 additions and 376 deletions

View File

@ -82,6 +82,14 @@ impl ConcreteValue {
String(concatenated) String(concatenated)
} }
(Character(left), String(right)) => {
let mut concatenated = DustString::new();
concatenated.push(*left);
concatenated.push_str(right);
String(concatenated)
}
(Float(left), Float(right)) => { (Float(left), Float(right)) => {
let sum = left + right; let sum = left + right;

View File

@ -46,8 +46,8 @@ impl Value {
Value::Concrete(ConcreteValue::Integer(integer)) Value::Concrete(ConcreteValue::Integer(integer))
} }
pub fn string(string: DustString) -> Self { pub fn string(string: impl Into<DustString>) -> Self {
Value::Concrete(ConcreteValue::String(string)) Value::Concrete(ConcreteValue::String(string.into()))
} }
pub fn as_boolean(&self) -> Option<&bool> { pub fn as_boolean(&self) -> Option<&bool> {

View File

@ -232,15 +232,7 @@ pub fn add(instruction_data: InstructionData, record: &mut Record) -> ThreadSign
} = instruction_data; } = instruction_data;
let left = record.get_argument(b, b_is_constant); let left = record.get_argument(b, b_is_constant);
let right = record.get_argument(c, c_is_constant); let right = record.get_argument(c, c_is_constant);
let sum = match (left, right) { let sum = left.add(right);
(Value::Concrete(left), Value::Concrete(right)) => match (left, right) {
(ConcreteValue::Integer(left), ConcreteValue::Integer(right)) => {
ConcreteValue::Integer(left + right).to_value()
}
_ => panic!("Value Error: Cannot add values"),
},
_ => panic!("Value Error: Cannot add values {left} and {right}"),
};
let register = Register::Value(sum); let register = Register::Value(sum);
record.set_register(a, register); record.set_register(a, register);

View File

@ -1,4 +1,5 @@
use dust_lang::*; use dust_lang::*;
use smallvec::smallvec;
#[test] #[test]
fn true_and_true_and_true() { fn true_and_true_and_true() {
@ -13,18 +14,29 @@ fn true_and_true_and_true() {
value_parameters: None, value_parameters: None,
return_type: Type::Boolean, return_type: Type::Boolean,
}, },
vec![ smallvec![
(Instruction::load_boolean(0, true, false), Span(0, 4)), Instruction::load_boolean(0, true, false),
(Instruction::test(Argument::Register(0), true), Span(5, 7)), Instruction::test(0, true),
(Instruction::jump(1, true), Span(5, 7)), Instruction::jump(1, true),
(Instruction::load_boolean(1, true, false), Span(8, 12)), Instruction::load_boolean(1, true, false),
(Instruction::test(Argument::Register(1), true), Span(13, 15)), Instruction::test(1, true),
(Instruction::jump(1, true), Span(13, 15)), Instruction::jump(1, true),
(Instruction::load_boolean(2, true, false), Span(16, 20)), Instruction::load_boolean(2, true, false),
(Instruction::r#return(true), Span(20, 20)), Instruction::r#return(true),
], ],
smallvec![
Span(0, 4),
Span(5, 7),
Span(5, 7),
Span(8, 12),
Span(13, 15),
Span(13, 15),
Span(16, 20),
Span(20, 20)
],
smallvec![],
smallvec![],
vec![], vec![],
vec![]
)) ))
); );
} }

View File

@ -1,4 +1,5 @@
use dust_lang::*; use dust_lang::*;
use smallvec::smallvec;
#[test] #[test]
fn true_or_false() { fn true_or_false() {
@ -13,17 +14,25 @@ fn true_or_false() {
value_parameters: None, value_parameters: None,
return_type: Type::Boolean, return_type: Type::Boolean,
}, },
vec![ smallvec![
(Instruction::load_boolean(0, true, false), Span(0, 4)), Instruction::load_boolean(0, true, false),
(Instruction::test(Argument::Register(0), false), Span(5, 7)), Instruction::test(0, false),
(Instruction::jump(1, true), Span(5, 7)), Instruction::jump(1, true),
(Instruction::load_boolean(1, false, false), Span(8, 13)), Instruction::load_boolean(1, false, false),
(Instruction::r#return(true), Span(13, 13)), Instruction::r#return(true),
], ],
vec![], smallvec![
Span(0, 4),
Span(5, 7),
Span(5, 7),
Span(8, 13),
Span(13, 13),
],
smallvec![],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true)))); assert_eq!(run(source), Ok(Some(Value::boolean(true))));
} }

View File

@ -1,262 +0,0 @@
use dust_lang::*;
#[test]
fn true_and_true() {
let source = "let a = true; let b = true; a && b";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, true, false), Span(8, 12)),
(Instruction::load_boolean(1, true, false), Span(22, 26)),
(Instruction::get_local(2, 0), Span(28, 29)),
(Instruction::test(Argument::Register(0), true), Span(30, 32)),
(Instruction::jump(1, true), Span(30, 32)),
(Instruction::get_local(3, 1), Span(33, 34)),
(Instruction::r#return(true), Span(34, 34)),
],
vec![ConcreteValue::string("a"), ConcreteValue::string("b")],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 1, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true))));
}
#[test]
fn false_and_false() {
let source = "let a = false; let b = false; a && b";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, false, false), Span(8, 13)),
(Instruction::load_boolean(1, false, false), Span(23, 28)),
(Instruction::get_local(2, 0), Span(30, 31)),
(Instruction::test(Argument::Register(0), true), Span(32, 34)),
(Instruction::jump(1, true), Span(32, 34)),
(Instruction::get_local(3, 1), Span(35, 36)),
(Instruction::r#return(true), Span(36, 36)),
],
vec![ConcreteValue::string("a"), ConcreteValue::string("b")],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 0, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false))));
}
#[test]
fn true_and_false() {
let source = "let a = true; let b = false; a && b";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, true, false), Span(8, 12)),
(Instruction::load_boolean(1, false, false), Span(22, 27)),
(Instruction::get_local(2, 0), Span(29, 30)),
(Instruction::test(Argument::Register(0), true), Span(31, 33)),
(Instruction::jump(1, true), Span(31, 33)),
(Instruction::get_local(3, 1), Span(34, 35)),
(Instruction::r#return(true), Span(35, 35)),
],
vec![ConcreteValue::string("a"), ConcreteValue::string("b")],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 1, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false))));
}
#[test]
fn false_and_true() {
let source = "let a = false; let b = true; a && b";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, false, false), Span(8, 13)),
(Instruction::load_boolean(1, true, false), Span(23, 27)),
(Instruction::get_local(2, 0), Span(29, 30)),
(Instruction::test(Argument::Register(0), true), Span(31, 33)),
(Instruction::jump(1, true), Span(31, 33)),
(Instruction::get_local(3, 1), Span(34, 35)),
(Instruction::r#return(true), Span(35, 35)),
],
vec![ConcreteValue::string("a"), ConcreteValue::string("b")],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 1, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false))));
}
#[test]
fn true_and_true_and_true() {
let source = "let a = true; let b = true; let c = true; a && b && c";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, true, false), Span(8, 12)),
(Instruction::load_boolean(1, true, false), Span(22, 26)),
(Instruction::load_boolean(2, true, false), Span(36, 40)),
(Instruction::get_local(3, 0), Span(42, 43)),
(Instruction::test(Argument::Register(0), true), Span(44, 46)),
(Instruction::jump(1, true), Span(44, 46)),
(Instruction::get_local(4, 1), Span(47, 48)),
(Instruction::test(Argument::Register(1), true), Span(49, 51)),
(Instruction::jump(1, true), Span(49, 51)),
(Instruction::get_local(5, 2), Span(52, 53)),
(Instruction::r#return(true), Span(53, 53)),
],
vec![
ConcreteValue::string("a"),
ConcreteValue::string("b"),
ConcreteValue::string("c")
],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 1, false, Scope::default()),
Local::new(2, 2, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true))));
}
#[test]
fn false_and_false_and_false() {
let source = "let a = false; let b = false; let c = false; a && b && c";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, false, false), Span(8, 13)),
(Instruction::load_boolean(1, false, false), Span(23, 28)),
(Instruction::load_boolean(2, false, false), Span(38, 43)),
(Instruction::get_local(3, 0), Span(45, 46)),
(Instruction::test(Argument::Register(0), true), Span(47, 49)),
(Instruction::jump(1, true), Span(47, 49)),
(Instruction::get_local(4, 1), Span(50, 51)),
(Instruction::test(Argument::Register(1), true), Span(52, 54)),
(Instruction::jump(1, true), Span(52, 54)),
(Instruction::get_local(5, 2), Span(55, 56)),
(Instruction::r#return(true), Span(56, 56)),
],
vec![
ConcreteValue::string("a"),
ConcreteValue::string("b"),
ConcreteValue::string("c")
],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 1, false, Scope::default()),
Local::new(2, 2, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false))));
}
#[test]
fn true_and_true_or_false() {
let source = "let a = true; let b = true; let c = false; a && b || c";
assert_eq!(
compile(source),
Ok(Chunk::with_data(
None,
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Type::Boolean,
},
vec![
(Instruction::load_boolean(0, true, false), Span(8, 12)),
(Instruction::load_boolean(1, true, false), Span(22, 26)),
(Instruction::load_boolean(2, false, false), Span(36, 41)),
(Instruction::get_local(3, 0), Span(43, 44)),
(Instruction::test(Argument::Register(0), true), Span(45, 47)),
(Instruction::jump(1, true), Span(45, 47)),
(Instruction::get_local(4, 1), Span(48, 49)),
(
Instruction::test(Argument::Register(1), false),
Span(50, 52)
),
(Instruction::jump(1, true), Span(50, 52)),
(Instruction::get_local(5, 2), Span(53, 54)),
(Instruction::r#return(true), Span(54, 54)),
],
vec![
ConcreteValue::string("a"),
ConcreteValue::string("b"),
ConcreteValue::string("c")
],
vec![
Local::new(0, 0, false, Scope::default()),
Local::new(1, 1, false, Scope::default()),
Local::new(2, 2, false, Scope::default())
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true))));
}

View File

@ -1,4 +1,5 @@
use dust_lang::*; use dust_lang::*;
use smallvec::smallvec;
#[test] #[test]
fn add_bytes() { fn add_bytes() {
@ -13,19 +14,18 @@ fn add_bytes() {
value_parameters: None, value_parameters: None,
return_type: Type::Byte, return_type: Type::Byte,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(5, 6)
),
(Instruction::r#return(true), Span(11, 11))
], ],
vec![ConcreteValue::Byte(0xfe), ConcreteValue::Byte(0x01)], smallvec![Span(5, 6), Span(11, 11),],
smallvec![Value::byte(0xfe), Value::byte(0x01)],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Byte(0xff)))); assert_eq!(run(source), Ok(Some(Value::byte(0xff))));
} }
#[test] #[test]
@ -41,19 +41,18 @@ fn add_bytes_saturate() {
value_parameters: None, value_parameters: None,
return_type: Type::Byte, return_type: Type::Byte,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(5, 6)
),
(Instruction::r#return(true), Span(11, 11))
], ],
vec![ConcreteValue::Byte(0xff), ConcreteValue::Byte(0x01)], smallvec![Span(5, 6), Span(11, 11)],
smallvec![Value::byte(0xff), Value::byte(0x01)],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Byte(0xff)))); assert_eq!(run(source), Ok(Some(Value::byte(0xff))));
} }
#[test] #[test]
@ -69,19 +68,18 @@ fn add_characters() {
value_parameters: None, value_parameters: None,
return_type: Type::String, return_type: Type::String,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true)
Span(4, 5)
),
(Instruction::r#return(true), Span(9, 9))
], ],
vec![ConcreteValue::Character('a'), ConcreteValue::Character('b')], smallvec![Span(4, 5), Span(11, 11)],
smallvec![Value::character('a'), Value::character('b')],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::string("ab")))); assert_eq!(run(source), Ok(Some(Value::string("ab"))));
} }
#[test] #[test]
@ -97,19 +95,18 @@ fn add_character_and_string() {
value_parameters: None, value_parameters: None,
return_type: Type::String, return_type: Type::String,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(4, 5)
),
(Instruction::r#return(true), Span(9, 9))
], ],
vec![ConcreteValue::Character('a'), ConcreteValue::string("b")], smallvec![Span(4, 5), Span(9, 9),],
smallvec![Value::character('a'), Value::string("b")],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::string("ab")))); assert_eq!(run(source), Ok(Some(Value::string("ab"))));
} }
#[test] #[test]
@ -125,19 +122,18 @@ fn add_floats() {
value_parameters: None, value_parameters: None,
return_type: Type::Float, return_type: Type::Float,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(4, 5)
),
(Instruction::r#return(true), Span(9, 9))
], ],
vec![ConcreteValue::Float(1.0), ConcreteValue::Float(2.0)], smallvec![Span(4, 5), Span(9, 9),],
smallvec![Value::float(1.0), Value::float(2.0)],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Float(3.0)))); assert_eq!(run(source), Ok(Some(Value::float(3.0))));
} }
#[test] #[test]
@ -153,22 +149,18 @@ fn add_floats_saturatate() {
value_parameters: None, value_parameters: None,
return_type: Type::Float, return_type: Type::Float,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(24, 25)
),
(Instruction::r#return(true), Span(36, 36))
],
vec![
ConcreteValue::Float(f64::MAX),
ConcreteValue::Float(0.00000001)
], ],
smallvec![Span(24, 25), Span(36, 36),],
smallvec![Value::float(f64::MAX), Value::float(0.00000001)],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Float(f64::MAX)))); assert_eq!(run(source), Ok(Some(Value::float(f64::MAX))));
} }
#[test] #[test]
@ -184,19 +176,18 @@ fn add_integers() {
value_parameters: None, value_parameters: None,
return_type: Type::Integer, return_type: Type::Integer,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true)
Span(2, 3)
),
(Instruction::r#return(true), Span(5, 5))
], ],
vec![ConcreteValue::Integer(1), ConcreteValue::Integer(2)], smallvec![Span(2, 3), Span(5, 5),],
smallvec![Value::integer(1), Value::integer(2)],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(3)))); assert_eq!(run(source), Ok(Some(Value::integer(3))));
} }
#[test] #[test]
@ -212,19 +203,18 @@ fn add_integers_saturate() {
value_parameters: None, value_parameters: None,
return_type: Type::Integer, return_type: Type::Integer,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true)
Span(20, 21)
),
(Instruction::r#return(true), Span(23, 23))
], ],
vec![ConcreteValue::Integer(i64::MAX), ConcreteValue::Integer(1)], smallvec![Span(20, 21), Span(23, 23),],
smallvec![Value::integer(i64::MAX), Value::integer(1)],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(i64::MAX)))); assert_eq!(run(source), Ok(Some(Value::integer(i64::MAX))));
} }
#[test] #[test]
@ -240,17 +230,13 @@ fn add_strings() {
value_parameters: None, value_parameters: None,
return_type: Type::String, return_type: Type::String,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(10, 11)
),
(Instruction::r#return(true), Span(20, 20))
],
vec![
ConcreteValue::string("Hello, "),
ConcreteValue::string("world!")
], ],
smallvec![Span(10, 11), Span(20, 20)],
smallvec![Value::string("Hello, "), Value::string("world!")],
smallvec![],
vec![] vec![]
)) ))
); );
@ -269,17 +255,16 @@ fn add_string_and_character() {
value_parameters: None, value_parameters: None,
return_type: Type::String, return_type: Type::String,
}, },
vec![ smallvec![
( Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)), Instruction::r#return(true),
Span(4, 5)
),
(Instruction::r#return(true), Span(9, 9))
], ],
vec![ConcreteValue::string("a"), ConcreteValue::Character('b')], smallvec![Span(4, 5), Span(9, 9),],
smallvec![Value::string("a"), Value::character('b')],
smallvec![],
vec![] vec![]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::string("ab")))); assert_eq!(run(source), Ok(Some(Value::string("ab"))));
} }