1
0
This commit is contained in:
Jeff 2024-11-29 16:24:37 -05:00
parent 12ae935f50
commit 36da1d0d7c
2 changed files with 34 additions and 13 deletions

View File

@ -47,7 +47,7 @@ pub fn compile(source: &str) -> Result<Chunk, DustError> {
Ok(compiler.finish(None, None)) Ok(compiler.finish(None, None))
} }
/// Low-level tool for compiling the input a token at a time while assembling a chunk. /// Tool for compiling the input a token at a time while assembling a chunk.
/// ///
/// See the [`compile`] function an example of how to create and use a Compiler. /// See the [`compile`] function an example of how to create and use a Compiler.
#[derive(Debug)] #[derive(Debug)]

View File

@ -81,8 +81,8 @@ fn or() {
} }
#[test] #[test]
fn variable_and() { fn and_and_or() {
let source = "let a = true; let b = false; a && b"; let source = "let a = true; let b = true; let c = false; a && b || c";
assert_eq!( assert_eq!(
compile(source), compile(source),
@ -105,35 +105,56 @@ fn variable_and() {
Span(4, 5) Span(4, 5)
), ),
( (
Instruction::load_boolean(Destination::Register(1), false, false), Instruction::load_boolean(Destination::Register(1), true, false),
Type::Boolean, Type::Boolean,
Span(22, 27) Span(22, 26)
), ),
( (
Instruction::define_local(1, 1, false), Instruction::define_local(1, 1, false),
Type::None, Type::None,
Span(18, 19) Span(18, 19)
), ),
(
Instruction::load_boolean(Destination::Register(2), false, false),
Type::Boolean,
Span(36, 41)
),
(
Instruction::define_local(2, 2, false),
Type::None,
Span(32, 33)
),
( (
Instruction::test(Argument::Local(0), true), Instruction::test(Argument::Local(0), true),
Type::None, Type::None,
Span(31, 33) Span(45, 47)
), ),
(Instruction::jump(1, true), Type::None, Span(31, 33)), (Instruction::jump(1, true), Type::None, Span(45, 47)),
( (
Instruction::get_local(Destination::Register(2), 1), Instruction::test(Argument::Local(1), false),
Type::Boolean, Type::None,
Span(34, 35) Span(50, 52)
), ),
(Instruction::r#return(true), Type::None, Span(35, 35)), (Instruction::jump(1, true), Type::None, Span(50, 52)),
(
Instruction::get_local(Destination::Register(3), 2),
Type::Boolean,
Span(53, 54)
),
(Instruction::r#return(true), Type::None, Span(54, 54)),
],
vec![
ConcreteValue::string("a"),
ConcreteValue::string("b"),
ConcreteValue::string("c")
], ],
vec![ConcreteValue::string("a"), ConcreteValue::string("b"),],
vec![ vec![
Local::new(0, Type::Boolean, false, Scope::default()), Local::new(0, Type::Boolean, false, Scope::default()),
Local::new(1, Type::Boolean, false, Scope::default()), Local::new(1, Type::Boolean, false, Scope::default()),
Local::new(2, Type::Boolean, false, Scope::default())
] ]
)) ))
); );
assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(false)))); assert_eq!(run(source), Ok(Some(ConcreteValue::Boolean(true))));
} }