2024-11-05 21:33:56 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn define_local() {
|
|
|
|
let source = "let x = 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-10 08:34:41 +00:00
|
|
|
return_type: Type::None,
|
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(8, 10)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(false), Span(11, 11))
|
2024-11-05 21:33:56 +00:00
|
|
|
],
|
2024-11-16 06:29:21 +00:00
|
|
|
vec![ConcreteValue::Integer(42), ConcreteValue::string("x")],
|
2024-12-10 08:34:41 +00:00
|
|
|
vec![Local::new(1, 0, false, Scope::default())]
|
2024-11-05 21:33:56 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-11-05 21:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn let_statement_expects_identifier() {
|
|
|
|
let source = "let 1 = 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
2024-12-10 08:34:41 +00:00
|
|
|
Err(DustError::Compile {
|
2024-11-06 20:40:37 +00:00
|
|
|
error: CompileError::ExpectedToken {
|
2024-11-05 21:33:56 +00:00
|
|
|
expected: TokenKind::Identifier,
|
|
|
|
found: Token::Integer("1").to_owned(),
|
|
|
|
position: Span(4, 5)
|
|
|
|
},
|
|
|
|
source
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_local() {
|
|
|
|
let source = "let mut x = 41; x = 42; 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,
|
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::load_constant(0, 0, false), Span(12, 14)),
|
|
|
|
(Instruction::load_constant(1, 2, false), Span(20, 22)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::set_local(1, 0), Span(16, 17)),
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::get_local(2, 0), Span(24, 25)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(25, 25)),
|
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(41),
|
|
|
|
ConcreteValue::string("x"),
|
|
|
|
ConcreteValue::Integer(42)
|
2024-11-11 00:28:21 +00:00
|
|
|
],
|
2024-12-10 08:34:41 +00:00
|
|
|
vec![Local::new(1, 0, true, Scope::default())]
|
2024-11-05 21:33:56 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
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
|
|
|
}
|