From 77cb82b9fe19726d7e4a26feed142b106865dfb2 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 29 Nov 2024 10:44:35 -0500 Subject: [PATCH] Fix bugs with new instruction features --- dust-lang/src/compiler.rs | 8 ++++++-- dust-lang/src/value/concrete_value.rs | 2 +- dust-lang/src/vm.rs | 21 +++++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/dust-lang/src/compiler.rs b/dust-lang/src/compiler.rs index f28abda..81863d0 100644 --- a/dust-lang/src/compiler.rs +++ b/dust-lang/src/compiler.rs @@ -1491,14 +1491,17 @@ impl<'src> Compiler<'src> { self.advance()?; + let mut argument_count = 0; + while !self.allow(Token::RightParenthesis)? { let expected_register = self.next_register(); self.parse_expression()?; let actual_register = self.next_register() - 1; + let registers_to_close = actual_register - expected_register; - if expected_register < actual_register { + if registers_to_close > 0 { let close = Instruction::from(Close { from: expected_register, to: actual_register, @@ -1507,12 +1510,13 @@ impl<'src> Compiler<'src> { self.emit_instruction(close, Type::None, self.current_position); } + argument_count += registers_to_close + 1; + self.allow(Token::Comma)?; } let end = self.current_position.1; let register = self.next_register(); - let argument_count = self.next_register() - function.index() - 1; let call = Instruction::from(Call { destination: Destination::Register(register), function, diff --git a/dust-lang/src/value/concrete_value.rs b/dust-lang/src/value/concrete_value.rs index fae26c0..994de90 100644 --- a/dust-lang/src/value/concrete_value.rs +++ b/dust-lang/src/value/concrete_value.rs @@ -286,7 +286,7 @@ impl Display for ConcreteValue { Ok(()) } - ConcreteValue::Function(function) => write!(f, "{function}"), + ConcreteValue::Function(chunk) => write!(f, "{}", chunk.r#type()), ConcreteValue::Integer(integer) => write!(f, "{integer}"), ConcreteValue::List(list) => { write!(f, "[")?; diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index 0d69e42..1a11d2b 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -285,6 +285,14 @@ impl<'a> Vm<'a> { if boolean == test_value { self.jump(1, true); + } else { + let jump = self.read()?; + let Jump { + offset, + is_positive, + } = Jump::from(&jump); + + self.jump(offset as usize, is_positive); } } Operation::TestSet => { @@ -451,11 +459,10 @@ impl<'a> Vm<'a> { self.set_register(register_index, register)?; } Operation::Jump => { - let jump = self.read()?; let Jump { offset, is_positive, - } = Jump::from(&jump); + } = Jump::from(&instruction); self.jump(offset as usize, is_positive); } @@ -479,7 +486,7 @@ impl<'a> Vm<'a> { }); }; let mut function_vm = Vm::new(chunk, Some(self)); - let first_argument_index = register_index - argument_count - 1; + let first_argument_index = register_index - argument_count; for (argument_index, argument_register_index) in (first_argument_index..register_index).enumerate() @@ -489,9 +496,7 @@ impl<'a> Vm<'a> { Register::Pointer(Pointer::ParentStack(argument_register_index)), )?; - function_vm - .local_definitions - .push(Some(argument_index as u16)); + function_vm.local_definitions[argument_index] = Some(argument_index as u16); } let return_value = function_vm.run()?; @@ -627,7 +632,7 @@ impl<'a> Vm<'a> { if is_positive { format!("+{}", offset) } else { - format!("-{}", offset + 1) + format!("-{}", offset) } ); @@ -749,7 +754,7 @@ impl<'a> Vm<'a> { position: self.current_position, })?; - self.jump(1, true); + self.ip += 1; self.current_position = *position; Ok(*instruction)