2024-11-03 01:24:41 +00:00
|
|
|
use dust_lang::*;
|
2024-12-10 08:34:41 +00:00
|
|
|
use smallvec::smallvec;
|
2024-11-03 01:24:41 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function() {
|
|
|
|
let source = "fn(a: int, b: int) -> int { a + b }";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-28 00:43:50 +00:00
|
|
|
run(source),
|
2024-12-04 05:04:56 +00:00
|
|
|
Ok(Some(ConcreteValue::function(Chunk::with_data(
|
2024-11-11 00:28:21 +00:00
|
|
|
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::function(FunctionType {
|
2024-11-16 10:16:51 +00:00
|
|
|
type_parameters: None,
|
2024-12-10 08:34:41 +00:00
|
|
|
value_parameters: Some(smallvec![(0, Type::Integer), (1, Type::Integer)]),
|
|
|
|
return_type: Type::Integer,
|
|
|
|
})
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-28 00:43:50 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(2, Argument::Register(0), Argument::Register(1)),
|
2024-11-28 00:43:50 +00:00
|
|
|
Span(30, 31)
|
|
|
|
),
|
2024-12-03 18:35:47 +00:00
|
|
|
(Instruction::r#return(true), Span(34, 35)),
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::string("a"), ConcreteValue::string("b"),],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
Local::new(0, 0, false, Scope::default()),
|
|
|
|
Local::new(1, 1, false, Scope::default())
|
2024-11-11 00:28:21 +00:00
|
|
|
]
|
|
|
|
))))
|
2024-11-03 01:24:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_call() {
|
|
|
|
let source = "fn(a: int, b: int) -> int { a + b }(1, 2)";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
2024-11-03 01:24:41 +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::Integer
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-03 01:24:41 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(0, 35)),
|
|
|
|
(Instruction::load_constant(1, 1, false), Span(36, 37)),
|
|
|
|
(Instruction::load_constant(2, 2, false), Span(39, 40)),
|
|
|
|
(Instruction::call(3, Argument::Constant(0), 2), Span(35, 41)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(41, 41)),
|
2024-11-03 01:24:41 +00:00
|
|
|
],
|
|
|
|
vec![
|
2024-12-04 05:04:56 +00:00
|
|
|
ConcreteValue::function(Chunk::with_data(
|
2024-11-11 00:28:21 +00:00
|
|
|
None,
|
2024-11-16 10:16:51 +00:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
2024-12-10 08:34:41 +00:00
|
|
|
value_parameters: Some(smallvec![(0, Type::Integer), (1, Type::Integer)]),
|
|
|
|
return_type: Type::Integer
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-28 00:43:50 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(2, Argument::Register(0), Argument::Register(1)),
|
2024-11-28 00:43:50 +00:00
|
|
|
Span(30, 31)
|
|
|
|
),
|
2024-12-03 18:35:47 +00:00
|
|
|
(Instruction::r#return(true), Span(34, 35)),
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::string("a"), ConcreteValue::string("b"),],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
Local::new(0, 0, false, Scope::default()),
|
|
|
|
Local::new(1, 1, false, Scope::default())
|
2024-11-11 00:28:21 +00:00
|
|
|
]
|
|
|
|
)),
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2)
|
2024-11-03 01:24:41 +00:00
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(3))));
|
2024-11-03 01:24:41 +00:00
|
|
|
}
|
2024-11-04 23:07:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_declaration() {
|
|
|
|
let source = "fn add (a: int, b: int) -> int { a + b }";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
2024-11-04 23:07:58 +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::None
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-04 23:07:58 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(0, 40)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(false), Span(40, 40))
|
2024-11-04 23:07:58 +00:00
|
|
|
],
|
|
|
|
vec![
|
2024-12-04 05:04:56 +00:00
|
|
|
ConcreteValue::function(Chunk::with_data(
|
2024-12-04 11:38:24 +00:00
|
|
|
Some("add".into()),
|
2024-11-16 10:16:51 +00:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
2024-12-10 08:34:41 +00:00
|
|
|
value_parameters: Some(smallvec![(0, Type::Integer), (1, Type::Integer)]),
|
|
|
|
return_type: Type::Integer
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-28 00:43:50 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(2, Argument::Register(0), Argument::Register(1)),
|
2024-11-28 00:43:50 +00:00
|
|
|
Span(35, 36)
|
|
|
|
),
|
2024-12-03 18:35:47 +00:00
|
|
|
(Instruction::r#return(true), Span(39, 40)),
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::string("a"), ConcreteValue::string("b")],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
Local::new(0, 0, false, Scope::default()),
|
|
|
|
Local::new(1, 1, false, Scope::default())
|
2024-11-11 00:28:21 +00:00
|
|
|
]
|
2024-11-16 10:16:51 +00:00
|
|
|
)),
|
|
|
|
ConcreteValue::string("add"),
|
2024-11-04 23:07:58 +00:00
|
|
|
],
|
2024-12-10 08:34:41 +00:00
|
|
|
vec![Local::new(1, 0, false, Scope::default(),),],
|
2024-11-04 23:07:58 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-11-04 23:07:58 +00:00
|
|
|
}
|