2024-11-05 21:33:56 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn constant() {
|
|
|
|
let source = "42";
|
|
|
|
|
|
|
|
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-09 12:01:07 +00:00
|
|
|
return_type: Type::Integer
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
2024-12-10 06:34:53 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), 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
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(42)],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(42))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty() {
|
|
|
|
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::None
|
2024-11-16 10:16:51 +00:00
|
|
|
},
|
2024-12-02 10:59:01 +00:00
|
|
|
vec![(Instruction::r#return(false), Span(0, 0))],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parentheses_precedence() {
|
|
|
|
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::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::add(0, Argument::Constant(0), Argument::Constant(1)),
|
2024-11-05 21:33:56 +00:00
|
|
|
Span(3, 4)
|
|
|
|
),
|
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::multiply(1, Argument::Register(0), Argument::Constant(2)),
|
2024-11-05 21:33:56 +00:00
|
|
|
Span(8, 9)
|
|
|
|
),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(11, 11)),
|
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-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(9))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
2024-12-03 19:00:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn math_operator_precedence() {
|
|
|
|
let source = "1 + 2 - 3 * 4 / 5";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
compile(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
None,
|
|
|
|
FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters: None,
|
2024-12-10 08:34:41 +00:00
|
|
|
return_type: Type::Integer,
|
2024-12-03 19:00:27 +00:00
|
|
|
},
|
|
|
|
vec![
|
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(0, Argument::Constant(0), Argument::Constant(1)),
|
2024-12-03 19:00:27 +00:00
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::multiply(1, Argument::Constant(2), Argument::Constant(3)),
|
2024-12-03 19:00:27 +00:00
|
|
|
Span(10, 11)
|
|
|
|
),
|
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::divide(2, Argument::Register(1), Argument::Constant(4)),
|
2024-12-03 19:00:27 +00:00
|
|
|
Span(14, 15)
|
|
|
|
),
|
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::subtract(3, Argument::Register(0), Argument::Register(2)),
|
2024-12-03 19:00:27 +00:00
|
|
|
Span(6, 7)
|
|
|
|
),
|
|
|
|
(Instruction::r#return(true), Span(17, 17)),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::Integer(2),
|
|
|
|
ConcreteValue::Integer(3),
|
|
|
|
ConcreteValue::Integer(4),
|
|
|
|
ConcreteValue::Integer(5),
|
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(1))));
|
|
|
|
}
|