2024-11-05 21:33:56 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_list() {
|
|
|
|
let source = "[]";
|
|
|
|
|
|
|
|
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,
|
2024-12-10 08:34:41 +00:00
|
|
|
return_type: Type::List(Box::new(Type::Any)),
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_list(0, 0), Span(0, 2)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(2, 2)),
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
|
|
|
vec![],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::list([]))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list() {
|
|
|
|
let source = "[1, 2, 3]";
|
|
|
|
|
|
|
|
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,
|
2024-12-10 08:34:41 +00:00
|
|
|
return_type: Type::List(Box::new(Type::Integer)),
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(1, 2)),
|
|
|
|
(Instruction::load_constant(1, 1, false), Span(4, 5)),
|
|
|
|
(Instruction::load_constant(2, 2, false), Span(7, 8)),
|
|
|
|
(Instruction::load_list(3, 0), Span(0, 9)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(9, 9)),
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2),
|
|
|
|
ConcreteValue::Integer(3)
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-06 08:36:46 +00:00
|
|
|
assert_eq!(
|
2024-11-28 00:43:50 +00:00
|
|
|
run(source),
|
2024-11-16 06:29:21 +00:00
|
|
|
Ok(Some(ConcreteValue::list([
|
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2),
|
|
|
|
ConcreteValue::Integer(3)
|
2024-11-11 00:28:21 +00:00
|
|
|
])))
|
2024-11-06 08:36:46 +00:00
|
|
|
);
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_with_complex_expression() {
|
|
|
|
let source = "[1, 2 + 3 - 4 * 5]";
|
|
|
|
|
|
|
|
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,
|
2024-12-10 08:34:41 +00:00
|
|
|
return_type: Type::List(Box::new(Type::Integer)),
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(1, 2)),
|
2024-11-28 02:13:35 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(1, Argument::Constant(1), Argument::Constant(2)),
|
2024-11-05 21:33:56 +00:00
|
|
|
Span(6, 7)
|
|
|
|
),
|
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::multiply(2, Argument::Constant(3), Argument::Constant(4)),
|
2024-11-05 21:33:56 +00:00
|
|
|
Span(14, 15)
|
|
|
|
),
|
2024-11-28 00:43:50 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::subtract(3, Argument::Register(1), Argument::Register(2)),
|
2024-11-28 00:43:50 +00:00
|
|
|
Span(10, 11)
|
|
|
|
),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::close(1, 3), Span(17, 18)),
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_list(4, 0), Span(0, 18)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(18, 18)),
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2),
|
|
|
|
ConcreteValue::Integer(3),
|
|
|
|
ConcreteValue::Integer(4),
|
|
|
|
ConcreteValue::Integer(5)
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-06 08:36:46 +00:00
|
|
|
assert_eq!(
|
2024-11-28 00:43:50 +00:00
|
|
|
run(source),
|
2024-11-16 06:29:21 +00:00
|
|
|
Ok(Some(ConcreteValue::list([
|
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(-15)
|
2024-11-11 00:28:21 +00:00
|
|
|
])))
|
2024-11-06 08:36:46 +00:00
|
|
|
);
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_with_simple_expression() {
|
|
|
|
let source = "[1, 2 + 3, 4]";
|
|
|
|
|
|
|
|
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,
|
2024-12-10 08:34:41 +00:00
|
|
|
return_type: Type::List(Box::new(Type::Integer)),
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(1, 2)),
|
2024-11-28 02:13:35 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(1, Argument::Constant(1), Argument::Constant(2)),
|
2024-11-05 21:33:56 +00:00
|
|
|
Span(6, 7)
|
|
|
|
),
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(2, 3, false), Span(11, 12)),
|
|
|
|
(Instruction::load_list(3, 0), Span(0, 13)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(13, 13)),
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2),
|
|
|
|
ConcreteValue::Integer(3),
|
|
|
|
ConcreteValue::Integer(4),
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-06 08:36:46 +00:00
|
|
|
assert_eq!(
|
2024-11-28 00:43:50 +00:00
|
|
|
run(source),
|
2024-11-16 06:29:21 +00:00
|
|
|
Ok(Some(ConcreteValue::list([
|
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(5),
|
|
|
|
ConcreteValue::Integer(4),
|
2024-11-11 00:28:21 +00:00
|
|
|
])))
|
2024-11-06 08:36:46 +00:00
|
|
|
);
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|