2024-09-12 04:39:31 +00:00
|
|
|
use dust_lang::*;
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-09-25 13:55:10 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn add() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "1 + 2";
|
|
|
|
|
2024-09-25 13:55:10 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::add(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5))
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
))
|
2024-09-25 13:55:10 +00:00
|
|
|
);
|
2024-10-05 02:21:17 +00:00
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(3))));
|
2024-09-25 13:55:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 08:33:38 +00:00
|
|
|
#[test]
|
|
|
|
fn add_assign() {
|
|
|
|
let source = "let mut a = 1; a += 2; a";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(*Instruction::add(0, 0, 1).set_c_is_constant(), Span(17, 19)),
|
|
|
|
(Instruction::get_local(1, 0), Span(23, 24)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(24, 24))
|
2024-10-05 08:33:38 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("a"), None, true, 0, Some(0))]
|
2024-10-05 08:33:38 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(3))));
|
|
|
|
}
|
|
|
|
|
2024-09-24 04:24:09 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn and() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "true && false";
|
|
|
|
|
2024-09-24 20:49:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_boolean(0, true, false), Span(0, 4)),
|
|
|
|
(Instruction::test(0, false), Span(5, 7)),
|
|
|
|
(Instruction::jump(1, true), Span(5, 7)),
|
|
|
|
(Instruction::load_boolean(1, false, false), Span(8, 13)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(13, 13)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![],
|
|
|
|
vec![]
|
|
|
|
))
|
2024-09-24 20:49:17 +00:00
|
|
|
);
|
2024-10-05 02:21:17 +00:00
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
2024-09-24 20:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn block_scope() {
|
|
|
|
let source = "
|
|
|
|
let a = 0;
|
|
|
|
{
|
|
|
|
let b = 42;
|
|
|
|
{
|
|
|
|
let c = 1;
|
|
|
|
}
|
|
|
|
let d = 2;
|
|
|
|
}
|
|
|
|
let e = 1;
|
|
|
|
";
|
|
|
|
|
2024-09-24 05:30:21 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:21:17 +00:00
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(17, 18)),
|
|
|
|
(Instruction::define_local(0, 0, false), Span(13, 14)),
|
|
|
|
(Instruction::load_constant(1, 1, false), Span(50, 52)),
|
|
|
|
(Instruction::define_local(1, 1, false), Span(46, 47)),
|
|
|
|
(Instruction::load_constant(2, 2, false), Span(92, 93)),
|
|
|
|
(Instruction::define_local(2, 2, false), Span(88, 89)),
|
|
|
|
(Instruction::load_constant(3, 3, false), Span(129, 130)),
|
|
|
|
(Instruction::define_local(3, 3, false), Span(125, 126)),
|
|
|
|
(Instruction::load_constant(4, 4, false), Span(158, 159)),
|
|
|
|
(Instruction::define_local(4, 4, false), Span(154, 155)),
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
Value::integer(0),
|
|
|
|
Value::integer(42),
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
|
|
|
Value::integer(1)
|
|
|
|
],
|
|
|
|
vec![
|
2024-10-09 10:15:06 +00:00
|
|
|
Local::new(Identifier::new("a"), None, false, 0, Some(0)),
|
|
|
|
Local::new(Identifier::new("b"), None, false, 1, Some(1)),
|
|
|
|
Local::new(Identifier::new("c"), None, false, 2, Some(2)),
|
|
|
|
Local::new(Identifier::new("d"), None, false, 1, Some(3)),
|
|
|
|
Local::new(Identifier::new("e"), None, false, 0, Some(4)),
|
2024-10-05 02:21:17 +00:00
|
|
|
]
|
|
|
|
)),
|
2024-09-24 05:30:21 +00:00
|
|
|
);
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-09-24 04:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 23:16:15 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn constant() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "42";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
2024-10-05 02:33:47 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(0, 2)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(2, 2))
|
2024-10-05 02:33:47 +00:00
|
|
|
],
|
2024-10-05 02:21:17 +00:00
|
|
|
vec![Value::integer(42)],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(42))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn define_local() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "let x = 42;";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(8, 10)),
|
|
|
|
(Instruction::define_local(0, 0, false), Span(4, 5)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("x"), None, false, 0, Some(0))]
|
2024-10-05 02:21:17 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn divide() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "2 / 2";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
2024-10-05 02:33:47 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::divide(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5))
|
2024-10-05 02:33:47 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(2), Value::integer(2)],
|
2024-10-05 02:21:17 +00:00
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(1))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 08:33:38 +00:00
|
|
|
#[test]
|
|
|
|
fn divide_assign() {
|
|
|
|
let source = "let mut a = 2; a /= 2; a";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(
|
|
|
|
*Instruction::divide(0, 0, 1).set_c_is_constant(),
|
|
|
|
Span(17, 19)
|
|
|
|
),
|
|
|
|
(Instruction::get_local(1, 0), Span(23, 24)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(24, 24))
|
2024-10-05 08:33:38 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(2), Value::integer(2)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("a"), None, true, 0, Some(0))]
|
2024-10-05 08:33:38 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(1))));
|
|
|
|
}
|
|
|
|
|
2024-09-23 23:16:15 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn empty() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(parse(source), Ok(Chunk::with_data(vec![], vec![], vec![])),);
|
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn equal() {
|
2024-10-05 02:21:17 +00:00
|
|
|
let source = "1 == 2";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 4)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(2, 4)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(6, 6)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn equality_assignment_long() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "let a = if 4 == 4 { true } else { false }; a";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(13, 15)
|
|
|
|
),
|
2024-10-05 04:11:03 +00:00
|
|
|
(Instruction::jump(1, true), Span(13, 15)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(20, 24)),
|
2024-10-05 02:21:17 +00:00
|
|
|
(Instruction::load_boolean(0, false, false), Span(34, 39)),
|
|
|
|
(Instruction::define_local(0, 0, false), Span(4, 5)),
|
2024-10-05 04:11:03 +00:00
|
|
|
(Instruction::get_local(1, 0), Span(43, 44)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(44, 44)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(4), Value::integer(4)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("a"), None, false, 0, Some(0))]
|
2024-10-05 02:21:17 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn equality_assignment_short() {
|
2024-10-09 16:16:46 +00:00
|
|
|
let source = "let a = 4 == 4 a";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(10, 12)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(10, 12)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(10, 12)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(10, 12)),
|
|
|
|
(Instruction::define_local(0, 0, false), Span(4, 5)),
|
2024-10-09 16:16:46 +00:00
|
|
|
(Instruction::get_local(1, 0), Span(15, 16)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(16, 16)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(4), Value::integer(4)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("a"), None, false, 0, Some(0))]
|
2024-10-05 02:21:17 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-10-05 04:11:03 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn greater() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "1 > 2";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::less_equal(false, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(2, 3)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(2, 3)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(2, 3)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn greater_than_or_equal() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "1 >= 2";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::less(false, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 4)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(2, 4)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(6, 6)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_else_expression() {
|
|
|
|
let source = "if 1 == 1 { 2 } else { 3 }";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(5, 7)
|
|
|
|
),
|
2024-10-05 04:11:03 +00:00
|
|
|
(Instruction::jump(1, true), Span(5, 7)),
|
2024-10-05 02:21:17 +00:00
|
|
|
(Instruction::load_constant(0, 2, true), Span(12, 13)),
|
|
|
|
(Instruction::load_constant(0, 3, false), Span(23, 24)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(26, 26)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
|
|
|
Value::integer(3)
|
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(2))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 06:07:26 +00:00
|
|
|
fn if_expression_false() {
|
|
|
|
let source = "if 1 == 2 { 2 }";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(5, 7)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(5, 7)),
|
|
|
|
(Instruction::load_constant(0, 2, false), Span(12, 13)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(None));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_expression_true() {
|
2024-10-05 02:21:17 +00:00
|
|
|
let source = "if 1 == 1 { 2 }";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(5, 7)
|
|
|
|
),
|
2024-10-05 04:11:03 +00:00
|
|
|
(Instruction::jump(1, true), Span(5, 7)),
|
2024-10-05 02:21:17 +00:00
|
|
|
(Instruction::load_constant(0, 2, false), Span(12, 13)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
assert_eq!(run(source), Ok(None));
|
2024-10-05 02:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn less_than() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "1 < 2";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::less(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(2, 3)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(2, 3)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(2, 3)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn less_than_or_equal() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "1 <= 2";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::less_equal(true, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 4)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(2, 4)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(6, 6)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list() {
|
|
|
|
let source = "[1, 2, 3]";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(1, 2)),
|
|
|
|
(Instruction::load_constant(1, 1, false), Span(4, 5)),
|
|
|
|
(Instruction::load_constant(2, 2, false), Span(7, 8)),
|
|
|
|
(Instruction::load_list(3, 0, 2), Span(0, 9)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(9, 9)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2), Value::integer(3),],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
run(source),
|
|
|
|
Ok(Some(Value::list(vec![
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
|
|
|
Value::integer(3)
|
|
|
|
])))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_with_complex_expression() {
|
|
|
|
let source = "[1, 2 + 3 - 4 * 5]";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(1, 2)),
|
|
|
|
(
|
|
|
|
*Instruction::add(1, 1, 2)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(6, 7)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
*Instruction::multiply(2, 3, 4)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(14, 15)
|
|
|
|
),
|
|
|
|
(Instruction::subtract(3, 1, 2), Span(10, 11)),
|
|
|
|
(Instruction::close(1, 3), Span(17, 18)),
|
|
|
|
(Instruction::load_list(4, 0, 3), Span(0, 18)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(18, 18)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
|
|
|
Value::integer(3),
|
|
|
|
Value::integer(4),
|
|
|
|
Value::integer(5)
|
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
run(source),
|
|
|
|
Ok(Some(Value::list(vec![
|
|
|
|
Value::integer(1),
|
2024-10-05 04:11:03 +00:00
|
|
|
Value::integer(-15)
|
2024-10-05 02:21:17 +00:00
|
|
|
])))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn list_with_simple_expression() {
|
|
|
|
let source = "[1, 2 + 3, 4]";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(1, 2)),
|
|
|
|
(
|
|
|
|
*Instruction::add(1, 1, 2)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(6, 7)
|
|
|
|
),
|
|
|
|
(Instruction::load_constant(2, 3, false), Span(11, 12)),
|
|
|
|
(Instruction::load_list(3, 0, 2), Span(0, 13)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(13, 13)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
|
|
|
Value::integer(3),
|
|
|
|
Value::integer(4),
|
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
run(source),
|
|
|
|
Ok(Some(Value::list(vec![
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(5),
|
|
|
|
Value::integer(4)
|
|
|
|
])))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn math_operator_precedence() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "1 + 2 - 3 * 4 / 5";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
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)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(17, 17)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![
|
|
|
|
Value::integer(1),
|
|
|
|
Value::integer(2),
|
|
|
|
Value::integer(3),
|
|
|
|
Value::integer(4),
|
|
|
|
Value::integer(5),
|
|
|
|
],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(1))));
|
2024-10-05 02:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiply() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "1 * 2";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
2024-10-05 04:11:03 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::multiply(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
2024-10-05 04:11:03 +00:00
|
|
|
],
|
2024-10-05 02:21:17 +00:00
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(2))));
|
2024-10-05 02:21:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 08:33:38 +00:00
|
|
|
#[test]
|
|
|
|
fn multiply_assign() {
|
|
|
|
let source = "let mut a = 2; a *= 3 a";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(
|
|
|
|
*Instruction::multiply(0, 0, 1).set_c_is_constant(),
|
|
|
|
Span(17, 19)
|
|
|
|
),
|
|
|
|
(Instruction::get_local(1, 0), Span(22, 23)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(23, 23))
|
2024-10-05 08:33:38 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(2), Value::integer(3)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("a"), None, true, 0, Some(0)),]
|
2024-10-05 08:33:38 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(6))));
|
|
|
|
}
|
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
#[test]
|
|
|
|
fn negate() {
|
|
|
|
let source = "-(42)";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
2024-10-05 04:11:03 +00:00
|
|
|
vec![
|
|
|
|
(*Instruction::negate(0, 0).set_b_is_constant(), Span(0, 1)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
2024-10-05 04:11:03 +00:00
|
|
|
],
|
2024-10-05 02:21:17 +00:00
|
|
|
vec![Value::integer(42)],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(-42))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn not() {
|
2024-10-05 02:21:17 +00:00
|
|
|
let source = "!true";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_boolean(0, true, false), Span(1, 5)),
|
|
|
|
(Instruction::not(1, 0), Span(0, 1)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![],
|
|
|
|
vec![]
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
2024-09-23 23:16:15 +00:00
|
|
|
}
|
|
|
|
|
2024-09-17 21:51:39 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn not_equal() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "1 != 2";
|
2024-10-05 02:21:17 +00:00
|
|
|
|
2024-09-19 22:07:11 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:21:17 +00:00
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::equal(false, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 4)
|
|
|
|
),
|
|
|
|
(Instruction::jump(1, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, true, true), Span(2, 4)),
|
|
|
|
(Instruction::load_boolean(0, false, false), Span(2, 4)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(6, 6)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
)),
|
2024-09-19 22:07:11 +00:00
|
|
|
);
|
2024-10-05 02:21:17 +00:00
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
2024-09-17 21:51:39 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn or() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "true || false";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_boolean(0, true, false), Span(0, 4)),
|
|
|
|
(Instruction::test(0, true), Span(5, 7)),
|
|
|
|
(Instruction::jump(1, true), Span(5, 7)),
|
|
|
|
(Instruction::load_boolean(1, false, false), Span(8, 13)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(13, 13)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(true))));
|
2024-10-05 02:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parentheses_precedence() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "(1 + 2) * 3";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::add(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(3, 4)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
*Instruction::multiply(1, 0, 2).set_c_is_constant(),
|
|
|
|
Span(8, 9)
|
|
|
|
),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(11, 11)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(1), Value::integer(2), Value::integer(3)],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(9))));
|
2024-10-05 02:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_local() {
|
2024-10-05 04:11:03 +00:00
|
|
|
let source = "let mut x = 41; x = 42; x";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 14)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(Instruction::load_constant(1, 1, false), Span(20, 22)),
|
|
|
|
(Instruction::set_local(1, 0), Span(16, 17)),
|
2024-10-05 04:11:03 +00:00
|
|
|
(Instruction::get_local(1, 0), Span(24, 25)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(25, 25)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(41), Value::integer(42)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("x"), None, true, 0, Some(0)),]
|
2024-10-05 02:21:17 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(42))));
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subtract() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "1 - 2";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
2024-10-05 06:07:26 +00:00
|
|
|
vec![
|
|
|
|
(
|
|
|
|
*Instruction::subtract(0, 0, 1)
|
|
|
|
.set_b_is_constant()
|
|
|
|
.set_c_is_constant(),
|
|
|
|
Span(2, 3)
|
|
|
|
),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(5, 5)),
|
2024-10-05 06:07:26 +00:00
|
|
|
],
|
2024-10-05 02:21:17 +00:00
|
|
|
vec![Value::integer(1), Value::integer(2)],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(-1))));
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 08:33:38 +00:00
|
|
|
#[test]
|
|
|
|
fn subtract_assign() {
|
|
|
|
let source = "let mut x = 42; x -= 2; x";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 14)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
|
|
|
(
|
|
|
|
*Instruction::subtract(0, 0, 1).set_c_is_constant(),
|
|
|
|
Span(18, 20)
|
|
|
|
),
|
|
|
|
(Instruction::get_local(1, 0), Span(24, 25)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(25, 25)),
|
2024-10-05 08:33:38 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(2)],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("x"), None, true, 0, Some(0)),]
|
2024-10-05 08:33:38 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(40))));
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:39:31 +00:00
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn variable_and() {
|
2024-10-05 02:36:33 +00:00
|
|
|
let source = "let a = true; let b = false; a && b";
|
2024-10-05 08:33:38 +00:00
|
|
|
|
2024-10-05 02:21:17 +00:00
|
|
|
assert_eq!(
|
2024-10-05 02:36:33 +00:00
|
|
|
parse(source),
|
2024-10-05 02:21:17 +00:00
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::load_boolean(0, true, false), Span(8, 12)),
|
|
|
|
(Instruction::define_local(0, 0, false), Span(4, 5)),
|
|
|
|
(Instruction::load_boolean(1, false, false), Span(22, 27)),
|
|
|
|
(Instruction::define_local(1, 1, false), Span(18, 19)),
|
|
|
|
(Instruction::get_local(2, 0), Span(29, 30)),
|
|
|
|
(Instruction::test(2, false), Span(31, 33)),
|
|
|
|
(Instruction::jump(1, true), Span(31, 33)),
|
|
|
|
(Instruction::get_local(3, 1), Span(34, 35)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(35, 35)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
|
|
|
vec![],
|
|
|
|
vec![
|
2024-10-09 10:15:06 +00:00
|
|
|
Local::new(Identifier::new("a"), None, false, 0, Some(0)),
|
|
|
|
Local::new(Identifier::new("b"), None, false, 0, Some(1)),
|
2024-10-05 02:21:17 +00:00
|
|
|
]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
2024-10-05 02:36:33 +00:00
|
|
|
assert_eq!(run(source), Ok(Some(Value::boolean(false))));
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-10-05 02:21:17 +00:00
|
|
|
fn r#while() {
|
|
|
|
let source = "let mut x = 0; while x < 5 { x = x + 1 } x";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse(source),
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
2024-10-05 06:07:26 +00:00
|
|
|
(Instruction::load_constant(0, 0, false), Span(12, 13)),
|
|
|
|
(Instruction::define_local(0, 0, true), Span(8, 9)),
|
2024-10-05 02:21:17 +00:00
|
|
|
(
|
2024-10-05 06:07:26 +00:00
|
|
|
*Instruction::less(true, 0, 1).set_c_is_constant(),
|
|
|
|
Span(23, 24)
|
2024-10-05 02:21:17 +00:00
|
|
|
),
|
2024-10-05 06:07:26 +00:00
|
|
|
(Instruction::jump(2, true), Span(23, 24)),
|
|
|
|
(*Instruction::add(0, 0, 2).set_c_is_constant(), Span(39, 40)),
|
|
|
|
(Instruction::jump(3, false), Span(41, 42)),
|
|
|
|
(Instruction::get_local(1, 0), Span(41, 42)),
|
2024-10-12 07:06:44 +00:00
|
|
|
(Instruction::r#return(true), Span(42, 42)),
|
2024-10-05 02:21:17 +00:00
|
|
|
],
|
2024-10-05 06:07:26 +00:00
|
|
|
vec![Value::integer(0), Value::integer(5), Value::integer(1),],
|
2024-10-09 10:15:06 +00:00
|
|
|
vec![Local::new(Identifier::new("x"), None, true, 0, Some(0)),]
|
2024-10-05 02:21:17 +00:00
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(run(source), Ok(Some(Value::integer(5))));
|
2024-09-12 04:39:31 +00:00
|
|
|
}
|