From 71a4f863e36297cce6beeb53a8c1c557ac5569fc Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 15 Sep 2024 10:20:43 -0400 Subject: [PATCH] Add test --- dust-lang/src/instruction.rs | 9 ++++----- dust-lang/src/parser/mod.rs | 15 ++++++++------- dust-lang/src/parser/tests.rs | 19 ++++++++++++++++++- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/dust-lang/src/instruction.rs b/dust-lang/src/instruction.rs index 311441f..3420b3c 100644 --- a/dust-lang/src/instruction.rs +++ b/dust-lang/src/instruction.rs @@ -137,6 +137,7 @@ impl Instruction { } pub fn set_destination(&mut self, destination: u8) { + self.0 &= 0x00FFFFFF; self.0 |= (destination as u32) << 24; } @@ -492,19 +493,17 @@ mod tests { #[test] fn add() { - let mut instruction = Instruction::add(0, 1, 2); + let mut instruction = Instruction::add(1, 1, 0); instruction.set_operation(Operation::Add); instruction.set_first_argument_to_constant(); - instruction.set_second_argument_to_constant(); assert_eq!(instruction.operation(), Operation::Add); - assert_eq!(instruction.destination(), 0); + assert_eq!(instruction.destination(), 1); assert_eq!(instruction.first_argument(), 1); - assert_eq!(instruction.second_argument(), 2); + assert_eq!(instruction.second_argument(), 0); assert!(instruction.first_argument_is_constant()); - assert!(instruction.second_argument_is_constant()); } #[test] diff --git a/dust-lang/src/parser/mod.rs b/dust-lang/src/parser/mod.rs index cf874dc..396921a 100644 --- a/dust-lang/src/parser/mod.rs +++ b/dust-lang/src/parser/mod.rs @@ -71,8 +71,6 @@ impl<'src> Parser<'src> { } else { self.current_register += 1; - log::trace!("Incremented register to {}", self.current_register); - Ok(()) } } @@ -87,8 +85,6 @@ impl<'src> Parser<'src> { } else { self.current_register -= 1; - log::trace!("Decremented register to {}", self.current_register); - Ok(()) } } @@ -100,7 +96,7 @@ impl<'src> Parser<'src> { let (new_token, position) = self.lexer.next_token()?; - log::trace!("Advancing to token {new_token} at {position}"); + log::trace!("Parsing token \"{new_token}\" at {position}"); self.previous_token = replace(&mut self.current_token, new_token); self.previous_position = replace(&mut self.current_position, position); @@ -369,7 +365,6 @@ impl<'src> Parser<'src> { if let Some(register_index) = previous_register { previous_instruction.set_destination(register_index); - self.emit_instruction(previous_instruction, self.previous_position); self.decrement_register()?; } else { @@ -379,13 +374,19 @@ impl<'src> Parser<'src> { self.previous_position, ); } + } else { + self.emit_instruction(previous_instruction, previous_position); + self.emit_instruction( + Instruction::set_local(self.current_register - 1, local_index), + start_position, + ); } } else { + self.increment_register()?; self.emit_instruction( Instruction::get_local(self.current_register, local_index), self.previous_position, ); - self.increment_register()?; } Ok(()) diff --git a/dust-lang/src/parser/tests.rs b/dust-lang/src/parser/tests.rs index 157fea6..583a23f 100644 --- a/dust-lang/src/parser/tests.rs +++ b/dust-lang/src/parser/tests.rs @@ -2,6 +2,23 @@ use crate::Local; use super::*; +#[test] +fn set_local() { + assert_eq!( + parse("let x = 41; x = 42;"), + Ok(Chunk::with_data( + vec![ + (Instruction::load_constant(0, 0), Span(8, 10)), + (Instruction::declare_local(0, 0), Span(4, 5)), + (Instruction::load_constant(1, 1), Span(16, 18)), + (Instruction::set_local(1, 0), Span(12, 13)), + ], + vec![Value::integer(41), Value::integer(42)], + vec![Local::new(Identifier::new("x"), 0, Some(0)),] + )), + ); +} + #[test] fn parentheses_precedence() { assert_eq!( @@ -51,7 +68,7 @@ fn add_multiply_precedence() { } #[test] -fn let_statement() { +fn declare_local() { assert_eq!( parse("let x = 42;"), Ok(Chunk::with_data(