1
0
dust/dust-lang/tests/logic.rs

140 lines
4.2 KiB
Rust
Raw Normal View History

2024-11-01 00:33:46 +00:00
use dust_lang::*;
#[test]
fn and() {
let source = "true && false";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
2024-11-16 10:16:51 +00:00
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Box::new(Type::Boolean),
},
2024-11-01 00:33:46 +00:00
vec![
2024-11-28 02:13:35 +00:00
(
Instruction::load_boolean(Destination::Register(0), true, false),
2024-11-28 02:13:35 +00:00
Type::Boolean,
Span(0, 4)
),
(
Instruction::test(Argument::Register(0), true),
Type::None,
Span(5, 7)
),
(Instruction::jump(1, true), Type::None, Span(5, 7)),
(
Instruction::load_boolean(Destination::Register(1), false, false),
2024-11-28 02:13:35 +00:00
Type::Boolean,
Span(8, 13)
),
(Instruction::r#return(true), Type::None, Span(13, 13)),
2024-11-01 00:33:46 +00:00
],
vec![],
vec![]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false))));
2024-11-01 00:33:46 +00:00
}
#[test]
fn or() {
let source = "true || false";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-01 00:33:46 +00:00
Ok(Chunk::with_data(
None,
2024-11-16 10:16:51 +00:00
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Box::new(Type::Boolean),
},
2024-11-01 00:33:46 +00:00
vec![
2024-11-28 02:13:35 +00:00
(
Instruction::load_boolean(Destination::Register(0), true, false),
2024-11-28 02:13:35 +00:00
Type::Boolean,
Span(0, 4)
),
(
Instruction::test(Argument::Register(0), false),
Type::None,
Span(5, 7)
),
(Instruction::jump(1, true), Type::None, Span(5, 7)),
(
Instruction::load_boolean(Destination::Register(1), false, false),
2024-11-28 02:13:35 +00:00
Type::Boolean,
Span(8, 13)
),
(Instruction::r#return(true), Type::None, Span(13, 13)),
2024-11-01 00:33:46 +00:00
],
vec![],
vec![]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true))));
2024-11-01 00:33:46 +00:00
}
2024-11-05 21:33:56 +00:00
#[test]
fn variable_and() {
let source = "let a = true; let b = false; a && b";
assert_eq!(
2024-11-06 20:40:37 +00:00
compile(source),
2024-11-05 21:33:56 +00:00
Ok(Chunk::with_data(
None,
2024-11-16 10:16:51 +00:00
FunctionType {
type_parameters: None,
value_parameters: None,
return_type: Box::new(Type::Boolean),
},
2024-11-05 21:33:56 +00:00
vec![
2024-11-28 02:13:35 +00:00
(
Instruction::load_boolean(Destination::Register(0), true, false),
2024-11-28 02:13:35 +00:00
Type::Boolean,
Span(8, 12)
),
(
Instruction::define_local(0, 0, false),
Type::None,
Span(4, 5)
),
(
Instruction::load_boolean(Destination::Register(1), false, false),
2024-11-28 02:13:35 +00:00
Type::Boolean,
Span(22, 27)
),
(
Instruction::define_local(1, 1, false),
Type::None,
Span(18, 19)
),
(
Instruction::test(Argument::Local(0), true),
2024-11-28 02:13:35 +00:00
Type::None,
Span(31, 33)
),
(Instruction::jump(1, true), Type::None, Span(31, 33)),
(
2024-11-28 06:55:35 +00:00
Instruction::get_local(Destination::Register(2), 1),
Type::Boolean,
Span(34, 35)
),
2024-11-28 02:13:35 +00:00
(Instruction::r#return(true), Type::None, Span(35, 35)),
2024-11-05 21:33:56 +00:00
],
2024-11-16 06:29:21 +00:00
vec![ConcreteValue::string("a"), ConcreteValue::string("b"),],
2024-11-05 21:33:56 +00:00
vec![
2024-11-26 01:43:18 +00:00
Local::new(0, Type::Boolean, false, Scope::default()),
Local::new(1, Type::Boolean, false, Scope::default()),
2024-11-05 21:33:56 +00:00
]
))
);
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false))));
2024-11-05 21:33:56 +00:00
}