added arguments, fixed some memory bugs

This commit is contained in:
afonya2 2025-06-03 20:17:30 +02:00
parent 9b0335e8d6
commit 7276312961
Signed by: afonya
GPG key ID: EBB9C4CAFAAFB2DC
2 changed files with 60 additions and 4 deletions

View file

@ -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;