From 982bd48d64fbd4d41f9754bc588609b5ce53bed4 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sun, 15 Mar 2026 18:38:27 +0200 Subject: [PATCH] Implement assigning to multiple variables --- examples/test.lua | 3 ++- src/compile.rs | 21 ++++++++++++++++++--- src/vm.rs | 3 ++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/test.lua b/examples/test.lua index 6fa487d..9170a8c 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -8,4 +8,5 @@ function test() return add(10)(15) end -local c = print(test()) \ No newline at end of file +local a, b, c = add(10)(15) +local pr = print(a, b, c) \ No newline at end of file diff --git a/src/compile.rs b/src/compile.rs index 6888698..7385f3a 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -112,15 +112,30 @@ impl Statement { let mut expr_regs = Vec::new(); for expr in &expr_list.0 { - let (instr, regs) = expr.kind.compile(state, scope, Some(1)); + let (instr, regs) = expr.kind.compile( + state, + scope, + if expr_list.0.len() == 1 { + Some(names.len()) + } else { + Some(1) + }, + ); instructions.extend(instr); expr_regs.extend(regs); } match access_modifier { AccessModifier::Local => { - for (name, reg) in names.iter().zip(expr_regs) { - scope.locals.insert(name.kind.clone(), reg); + for (i, name) in names.iter().enumerate() { + scope.locals.insert( + name.kind.clone(), + expr_regs + .get(i) + .cloned() + .unwrap_or(scope.register_counter.0 + i as u16), + ); } + dbg!(&scope.locals); } AccessModifier::Global => { for (name, reg) in names.iter().zip(expr_regs) { diff --git a/src/vm.rs b/src/vm.rs index 823c200..7609804 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -209,7 +209,8 @@ impl ClosureRunner { if let Some(instr) = instructions.get(self.program_counter) { match instr { Instruction::Move(a, b) => { - self.stack.insert(*a, self.stack.get(b).unwrap().clone()); + self.stack + .insert(*a, self.stack.get(b).unwrap_or(&Value::Nil).clone()); } Instruction::LoadK(reg, constant) => { self.stack.insert(