2024-11-02 21:24:41 -04:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function() {
|
|
|
|
let source = "fn(a: int, b: int) -> int { a + b }";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-27 19:43:50 -05:00
|
|
|
run(source),
|
2024-11-16 01:29:21 -05:00
|
|
|
Ok(Some(ConcreteValue::Function(Chunk::with_data(
|
2024-11-10 19:28:21 -05:00
|
|
|
None,
|
2024-11-16 05:16:51 -05:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: None,
|
|
|
|
return_type: Box::new(Type::Function(FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: Some(vec![(0, Type::Integer), (1, Type::Integer)]),
|
|
|
|
return_type: Box::new(Type::Integer),
|
|
|
|
}))
|
|
|
|
},
|
2024-11-10 19:28:21 -05:00
|
|
|
vec![
|
2024-11-27 19:43:50 -05:00
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::add(
|
|
|
|
Destination::Register(2),
|
|
|
|
Argument::Local(0),
|
|
|
|
Argument::Local(1)
|
|
|
|
),
|
2024-11-27 19:43:50 -05:00
|
|
|
Span(30, 31)
|
|
|
|
),
|
2024-12-03 13:35:47 -05:00
|
|
|
(Instruction::r#return(true), Span(34, 35)),
|
2024-11-10 19:28:21 -05:00
|
|
|
],
|
2024-11-16 01:29:21 -05:00
|
|
|
vec![ConcreteValue::string("a"), ConcreteValue::string("b"),],
|
2024-11-10 19:28:21 -05:00
|
|
|
vec![
|
2024-11-25 20:43:18 -05:00
|
|
|
Local::new(0, Type::Integer, false, Scope::default()),
|
|
|
|
Local::new(1, Type::Integer, false, Scope::default())
|
2024-11-10 19:28:21 -05:00
|
|
|
]
|
|
|
|
))))
|
2024-11-02 21:24:41 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_call() {
|
|
|
|
let source = "fn(a: int, b: int) -> int { a + b }(1, 2)";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 15:40:37 -05:00
|
|
|
compile(source),
|
2024-11-02 21:24:41 -04:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
None,
|
2024-11-16 05:16:51 -05:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: None,
|
|
|
|
return_type: Box::new(Type::Integer)
|
|
|
|
},
|
2024-11-02 21:24:41 -04:00
|
|
|
vec![
|
2024-11-27 21:13:35 -05:00
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::load_constant(Destination::Register(0), 0, false),
|
2024-12-03 13:35:47 -05:00
|
|
|
Span(0, 35)
|
2024-11-27 21:13:35 -05:00
|
|
|
),
|
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::load_constant(Destination::Register(1), 1, false),
|
2024-11-27 21:13:35 -05:00
|
|
|
Span(36, 37)
|
|
|
|
),
|
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::load_constant(Destination::Register(2), 2, false),
|
2024-11-27 21:13:35 -05:00
|
|
|
Span(39, 40)
|
|
|
|
),
|
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::call(Destination::Register(3), Argument::Constant(0), 2),
|
2024-11-27 21:13:35 -05:00
|
|
|
Span(35, 41)
|
|
|
|
),
|
2024-12-02 05:59:01 -05:00
|
|
|
(Instruction::r#return(true), Span(41, 41)),
|
2024-11-02 21:24:41 -04:00
|
|
|
],
|
|
|
|
vec![
|
2024-11-16 01:29:21 -05:00
|
|
|
ConcreteValue::Function(Chunk::with_data(
|
2024-11-10 19:28:21 -05:00
|
|
|
None,
|
2024-11-16 05:16:51 -05:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: Some(vec![(0, Type::Integer), (1, Type::Integer)]),
|
|
|
|
return_type: Box::new(Type::Integer)
|
|
|
|
},
|
2024-11-10 19:28:21 -05:00
|
|
|
vec![
|
2024-11-27 19:43:50 -05:00
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::add(
|
|
|
|
Destination::Register(2),
|
|
|
|
Argument::Local(0),
|
|
|
|
Argument::Local(1)
|
|
|
|
),
|
2024-11-27 19:43:50 -05:00
|
|
|
Span(30, 31)
|
|
|
|
),
|
2024-12-03 13:35:47 -05:00
|
|
|
(Instruction::r#return(true), Span(34, 35)),
|
2024-11-10 19:28:21 -05:00
|
|
|
],
|
2024-11-16 01:29:21 -05:00
|
|
|
vec![ConcreteValue::string("a"), ConcreteValue::string("b"),],
|
2024-11-10 19:28:21 -05:00
|
|
|
vec![
|
2024-11-25 20:43:18 -05:00
|
|
|
Local::new(0, Type::Integer, false, Scope::default()),
|
|
|
|
Local::new(1, Type::Integer, false, Scope::default())
|
2024-11-10 19:28:21 -05:00
|
|
|
]
|
|
|
|
)),
|
2024-11-16 01:29:21 -05:00
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2)
|
2024-11-02 21:24:41 -04:00
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-27 19:43:50 -05:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(3))));
|
2024-11-02 21:24:41 -04:00
|
|
|
}
|
2024-11-04 18:07:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_declaration() {
|
|
|
|
let source = "fn add (a: int, b: int) -> int { a + b }";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 15:40:37 -05:00
|
|
|
compile(source),
|
2024-11-04 18:07:58 -05:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
None,
|
2024-11-16 05:16:51 -05:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: None,
|
|
|
|
return_type: Box::new(Type::None)
|
|
|
|
},
|
2024-11-04 18:07:58 -05:00
|
|
|
vec![
|
2024-11-27 21:13:35 -05:00
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::load_constant(Destination::Register(0), 0, false),
|
2024-11-27 21:13:35 -05:00
|
|
|
Span(0, 40)
|
|
|
|
),
|
2024-12-02 05:59:01 -05:00
|
|
|
(Instruction::define_local(0, 0, false), Span(3, 6)),
|
|
|
|
(Instruction::r#return(false), Span(40, 40))
|
2024-11-04 18:07:58 -05:00
|
|
|
],
|
|
|
|
vec![
|
2024-11-16 01:29:21 -05:00
|
|
|
ConcreteValue::Function(Chunk::with_data(
|
|
|
|
Some("add".to_string()),
|
2024-11-16 05:16:51 -05:00
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: Some(vec![(0, Type::Integer), (1, Type::Integer)]),
|
|
|
|
return_type: Box::new(Type::Integer)
|
|
|
|
},
|
2024-11-10 19:28:21 -05:00
|
|
|
vec![
|
2024-11-27 19:43:50 -05:00
|
|
|
(
|
2024-11-28 01:10:49 -05:00
|
|
|
Instruction::add(
|
|
|
|
Destination::Register(2),
|
|
|
|
Argument::Local(0),
|
|
|
|
Argument::Local(1)
|
|
|
|
),
|
2024-11-27 19:43:50 -05:00
|
|
|
Span(35, 36)
|
|
|
|
),
|
2024-12-03 13:35:47 -05:00
|
|
|
(Instruction::r#return(true), Span(39, 40)),
|
2024-11-10 19:28:21 -05:00
|
|
|
],
|
2024-11-16 01:29:21 -05:00
|
|
|
vec![ConcreteValue::string("a"), ConcreteValue::string("b")],
|
2024-11-10 19:28:21 -05:00
|
|
|
vec![
|
2024-11-25 20:43:18 -05:00
|
|
|
Local::new(0, Type::Integer, false, Scope::default()),
|
|
|
|
Local::new(1, Type::Integer, false, Scope::default())
|
2024-11-10 19:28:21 -05:00
|
|
|
]
|
2024-11-16 05:16:51 -05:00
|
|
|
)),
|
|
|
|
ConcreteValue::string("add"),
|
2024-11-04 18:07:58 -05:00
|
|
|
],
|
|
|
|
vec![Local::new(
|
2024-11-16 05:16:51 -05:00
|
|
|
1,
|
2024-11-09 21:40:33 -05:00
|
|
|
Type::Function(FunctionType {
|
2024-11-04 18:07:58 -05:00
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: Some(vec![(0, Type::Integer), (1, Type::Integer)]),
|
2024-11-09 21:40:33 -05:00
|
|
|
return_type: Box::new(Type::Integer),
|
|
|
|
}),
|
2024-11-04 18:07:58 -05:00
|
|
|
false,
|
2024-11-05 12:44:16 -05:00
|
|
|
Scope::default(),
|
2024-11-17 20:32:53 -05:00
|
|
|
),],
|
2024-11-04 18:07:58 -05:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-27 19:43:50 -05:00
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-11-04 18:07:58 -05:00
|
|
|
}
|