2024-11-05 21:33:56 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add() {
|
|
|
|
let source = "1 + 2";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::add(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(Instruction::r#return(true), Span(5, 5))
|
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(1), ConcreteValue::Integer(2)],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(3))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_assign() {
|
|
|
|
let source = "let mut a = 1; a += 2; a";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(*Instruction::add(0, 0, 2).set_c_is_constant(), Span(17, 19)),
|
|
|
|
(Instruction::get_local(1, 0), Span(23, 24)),
|
|
|
|
(Instruction::r#return(true), Span(24, 24))
|
|
|
|
],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(1),
|
|
|
|
ConcreteValue::string("a"),
|
|
|
|
ConcreteValue::Integer(2)
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-10 02:40:33 +00:00
|
|
|
vec![Local::new(1, Type::Integer, true, Scope::default())]
|
2024-11-05 21:33:56 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(3))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_assign_expects_mutable_variable() {
|
|
|
|
let source = "1 += 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
|
|
|
Err(DustError::Compile {
|
|
|
|
error: CompileError::ExpectedMutableVariable {
|
2024-11-05 21:33:56 +00:00
|
|
|
found: Token::Integer("1").to_owned(),
|
|
|
|
position: Span(0, 1)
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-06 05:57:54 +00:00
|
|
|
// #[test]
|
|
|
|
// fn add_expects_integer_float_or_string() {
|
|
|
|
// let source = "true + false";
|
2024-11-06 04:20:58 +00:00
|
|
|
|
2024-11-06 05:57:54 +00:00
|
|
|
// assert_eq!(
|
|
|
|
// parse(source),
|
|
|
|
// Err(DustError::Parse {
|
|
|
|
// error: ParseError::ExpectedIntegerFloatOrString {
|
|
|
|
// found: Token::True,
|
|
|
|
// position: Span(0, 3)
|
|
|
|
// },
|
|
|
|
// source
|
|
|
|
// })
|
|
|
|
// );
|
|
|
|
// }
|
2024-11-06 04:20:58 +00:00
|
|
|
|
2024-11-05 21:33:56 +00:00
|
|
|
#[test]
|
|
|
|
fn divide() {
|
|
|
|
let source = "2 / 2";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::divide(0, 0, 0)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(Instruction::r#return(true), Span(5, 5))
|
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(2)],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(1))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn divide_assign() {
|
|
|
|
let source = "let mut a = 2; a /= 2; a";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(
|
|
|
|
*Instruction::divide(0, 0, 0).set_c_is_constant(),
|
|
|
|
Span(17, 19)
|
|
|
|
),
|
|
|
|
(Instruction::get_local(1, 0), Span(23, 24)),
|
|
|
|
(Instruction::r#return(true), Span(24, 24))
|
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(2), ConcreteValue::string("a")],
|
2024-11-10 02:40:33 +00:00
|
|
|
vec![Local::new(1, Type::Integer, true, Scope::default())]
|
2024-11-05 21:33:56 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(1))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn divide_assign_expects_mutable_variable() {
|
|
|
|
let source = "1 -= 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
|
|
|
Err(DustError::Compile {
|
|
|
|
error: CompileError::ExpectedMutableVariable {
|
2024-11-05 21:33:56 +00:00
|
|
|
found: Token::Integer("1").to_owned(),
|
|
|
|
position: Span(0, 1)
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn math_operator_precedence() {
|
|
|
|
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,
|
|
|
|
return_type: Box::new(Type::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::add(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
*Instruction::multiply(1, 2, 3)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(10, 11)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
*Instruction::divide(2, 1, 4).set_c_is_constant(),
|
|
|
|
Span(14, 15)
|
|
|
|
),
|
|
|
|
(Instruction::subtract(3, 0, 2), Span(6, 7)),
|
|
|
|
(Instruction::r#return(true), Span(17, 17)),
|
|
|
|
],
|
|
|
|
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-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(1))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiply() {
|
|
|
|
let source = "1 * 2";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::multiply(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(1), ConcreteValue::Integer(2)],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(2))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiply_assign() {
|
|
|
|
let source = "let mut a = 2; a *= 3 a";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(
|
|
|
|
*Instruction::multiply(0, 0, 2).set_c_is_constant(),
|
|
|
|
Span(17, 19)
|
|
|
|
),
|
|
|
|
(Instruction::get_local(1, 0), Span(22, 23)),
|
|
|
|
(Instruction::r#return(true), Span(23, 23))
|
|
|
|
],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(2),
|
|
|
|
ConcreteValue::string("a"),
|
|
|
|
ConcreteValue::Integer(3)
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-10 02:40:33 +00:00
|
|
|
vec![Local::new(1, Type::Integer, true, Scope::default())]
|
2024-11-05 21:33:56 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(6))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiply_assign_expects_mutable_variable() {
|
|
|
|
let source = "1 *= 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
|
|
|
Err(DustError::Compile {
|
|
|
|
error: CompileError::ExpectedMutableVariable {
|
2024-11-05 21:33:56 +00:00
|
|
|
found: Token::Integer("1").to_owned(),
|
|
|
|
position: Span(0, 1)
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subtract() {
|
|
|
|
let source = "1 - 2";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::subtract(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(1), ConcreteValue::Integer(2)],
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(-1))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subtract_assign() {
|
|
|
|
let source = "let mut x = 42; x -= 2; x";
|
|
|
|
|
|
|
|
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::Integer),
|
|
|
|
},
|
2024-11-05 21:33:56 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 14)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(
|
|
|
|
*Instruction::subtract(0, 0, 2).set_c_is_constant(),
|
|
|
|
Span(18, 20)
|
|
|
|
),
|
|
|
|
(Instruction::get_local(1, 0), Span(24, 25)),
|
|
|
|
(Instruction::r#return(true), Span(25, 25)),
|
|
|
|
],
|
2024-11-11 00:28:21 +00:00
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(42),
|
|
|
|
ConcreteValue::string("x"),
|
|
|
|
ConcreteValue::Integer(2)
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-11-10 02:40:33 +00:00
|
|
|
vec![Local::new(1, Type::Integer, true, Scope::default())]
|
2024-11-05 21:33:56 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-16 06:29:21 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(40))));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subtract_assign_expects_mutable_variable() {
|
|
|
|
let source = "1 -= 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
|
|
|
Err(DustError::Compile {
|
|
|
|
error: CompileError::ExpectedMutableVariable {
|
2024-11-05 21:33:56 +00:00
|
|
|
found: Token::Integer("1").to_owned(),
|
|
|
|
position: Span(0, 1)
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|