Implement assigning to multiple variables

This commit is contained in:
Sofia 2026-03-15 18:38:27 +02:00
parent 1673ae964a
commit 982bd48d64
3 changed files with 22 additions and 5 deletions

View File

@ -8,4 +8,5 @@ function test()
return add(10)(15) return add(10)(15)
end end
local c = print(test()) local a, b, c = add(10)(15)
local pr = print(a, b, c)

View File

@ -112,15 +112,30 @@ impl Statement {
let mut expr_regs = Vec::new(); let mut expr_regs = Vec::new();
for expr in &expr_list.0 { 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); instructions.extend(instr);
expr_regs.extend(regs); expr_regs.extend(regs);
} }
match access_modifier { match access_modifier {
AccessModifier::Local => { AccessModifier::Local => {
for (name, reg) in names.iter().zip(expr_regs) { for (i, name) in names.iter().enumerate() {
scope.locals.insert(name.kind.clone(), reg); 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 => { AccessModifier::Global => {
for (name, reg) in names.iter().zip(expr_regs) { for (name, reg) in names.iter().zip(expr_regs) {

View File

@ -209,7 +209,8 @@ impl ClosureRunner {
if let Some(instr) = instructions.get(self.program_counter) { if let Some(instr) = instructions.get(self.program_counter) {
match instr { match instr {
Instruction::Move(a, b) => { 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) => { Instruction::LoadK(reg, constant) => {
self.stack.insert( self.stack.insert(