2024-11-05 21:07:51 +00:00
|
|
|
use dust_lang::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn r#while() {
|
|
|
|
let source = "let mut x = 0; while x < 5 { x = x + 1 } x";
|
|
|
|
|
|
|
|
assert_eq!(
|
2024-11-06 20:40:37 +00:00
|
|
|
compile(source),
|
2024-11-05 21:07:51 +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:07:51 +00:00
|
|
|
vec![
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
2024-11-28 02:13:35 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::less(0, true, Argument::Register(0), Argument::Constant(2)),
|
2024-11-05 21:07:51 +00:00
|
|
|
Span(23, 24)
|
|
|
|
),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::jump(2, true), Span(41, 42)),
|
2024-11-28 00:43:50 +00:00
|
|
|
(
|
2024-12-10 08:34:41 +00:00
|
|
|
Instruction::add(0, Argument::Register(0), Argument::Constant(3)),
|
2024-11-28 00:43:50 +00:00
|
|
|
Span(35, 36)
|
|
|
|
),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::jump(3, false), Span(41, 42)),
|
2024-12-10 08:34:41 +00:00
|
|
|
(Instruction::get_local(1, 0), Span(41, 42)),
|
2024-12-02 10:59:01 +00:00
|
|
|
(Instruction::r#return(true), Span(42, 42)),
|
2024-11-05 21:07:51 +00:00
|
|
|
],
|
|
|
|
vec![
|
2024-11-16 06:29:21 +00:00
|
|
|
ConcreteValue::Integer(0),
|
|
|
|
ConcreteValue::string("x"),
|
|
|
|
ConcreteValue::Integer(5),
|
|
|
|
ConcreteValue::Integer(1),
|
2024-11-05 21:07:51 +00:00
|
|
|
],
|
2024-12-10 08:34:41 +00:00
|
|
|
vec![Local::new(1, 0, true, Scope::default())]
|
2024-11-05 21:07:51 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-11-28 00:43:50 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(ConcreteValue::Integer(5))));
|
2024-11-05 21:07:51 +00:00
|
|
|
}
|