diff --git a/src/virtualmachine.rs b/src/virtualmachine.rs index 0cd5bbb..35df2e0 100644 --- a/src/virtualmachine.rs +++ b/src/virtualmachine.rs @@ -505,7 +505,32 @@ impl Machine { panic!("Memory with variable ID {} not found", operation.arg2); } let mem = mem.unwrap(); - set_register(&mut self.registers, Register { id: operation.arg1, pointer: mem }); + if mem >= self.memory.len() || mem < 1 { + panic!("Memory location out of bounds for variable ID {}", operation.arg2); + } + let mut nmem = self.memory[mem].clone(); + match &mut nmem { + VMMemory::Number(num) => { + num.variable_id = 0; + }, + VMMemory::String(str) => { + str.variable_id = 0; + }, + VMMemory::Boolean(bool) => { + bool.variable_id = 0; + }, + VMMemory::Null(null) => { + null.variable_id = 0; + }, + VMMemory::Function(func) => { + func.variable_id = 0; + }, + VMMemory::Table(tbl) => { + tbl.variable_id = 0; + } + } + self.memory.push(nmem); + set_register(&mut self.registers, Register { id: operation.arg1, pointer: self.memory.len() - 1 }); }, 3 => { //LDI @@ -708,9 +733,38 @@ impl Machine { if mem.is_none() { panic!("Memory location not found for register"); } - let mem = mem.unwrap(); + let mem = mem.unwrap().clone(); match mem { VMMemory::Function(func) => { + let arg = 0; + for i in &self.stack { + let mut new_mem = i.clone(); + if self.functions[func.id].variables.len() <= arg || self.functions[func.id].variables[arg].start != 0 { + panic!("Function {} does not have enough arguments", func.id); + } + match &mut new_mem { + VMMemory::String(str) => { + str.variable_id = self.functions[func.id].variables[arg].id; + }, + VMMemory::Number(num) => { + num.variable_id = self.functions[func.id].variables[arg].id; + }, + VMMemory::Boolean(bool) => { + bool.variable_id = self.functions[func.id].variables[arg].id; + }, + VMMemory::Null(null) => { + null.variable_id = self.functions[func.id].variables[arg].id; + }, + VMMemory::Function(func) => { + func.variable_id = self.functions[func.id].variables[arg].id; + }, + VMMemory::Table(tbl) => { + tbl.variable_id = self.functions[func.id].variables[arg].id; + }, + } + self.memory.push(new_mem); + } + self.call_stack.push(CallStack { func: func.id, return_reg: operation.arg2 as usize, pc: 0 }); executed_func = &self.functions[func.id]; let last_stack = self.call_stack.len() - 1; diff --git a/test.as b/test.as index 4754b41..ea086c8 100644 --- a/test.as +++ b/test.as @@ -1,2 +1,4 @@ -gethelj a = {1,2,3} -gethelj b = a[0] \ No newline at end of file +gethelj a = lőcsve(m) { + gethelj b = m +} +a(69) \ No newline at end of file