diff --git a/src/virtualmachine.rs b/src/virtualmachine.rs index 6ecf5f0..9ccd6a5 100644 --- a/src/virtualmachine.rs +++ b/src/virtualmachine.rs @@ -550,7 +550,7 @@ impl Machine { let executed_func = func_clone.get(self.call_stack[self.call_stack.len()-1].func); let mut executed_stack = self.call_stack.len()-1; if executed_func.is_none() { - let err = create_error(&format!("Current function not found"), 0, ErrorType::MachineError, ErrorSubType::UnknownFunction); + let err = create_error(&format!("Current function not found"), 1, ErrorType::MachineError, ErrorSubType::UnknownFunction); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -567,7 +567,7 @@ impl Machine { //LDS let str = executed_func.strings.get(&(operation.arg2 as u32)); if str.is_none() { - let err = create_error(&format!("String with ID `{}` not found", operation.arg2), 0, ErrorType::MachineError, ErrorSubType::UnknownString); + let err = create_error(&format!("String with ID `{}` not found", operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownString); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -579,13 +579,13 @@ impl Machine { //LDM let mem = get_mem_pos_by_var_id(&self.memory, operation.arg2 as u32); if mem.is_none() { - let err = create_error(&format!("Memory with variable ID `{}` not found", operation.arg2), 0, ErrorType::MachineError, ErrorSubType::UnknownMemoryLocation); + let err = create_error(&format!("Memory with variable ID `{}` not found", operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownMemoryLocation); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let mem = mem.unwrap(); if mem >= self.memory.len() || mem < 1 { - let err = create_error(&format!("Memory location out of bounds for variable ID `{}`", operation.arg2), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location out of bounds for variable ID `{}`", operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -630,14 +630,14 @@ impl Machine { //LDF let func_pos = executed_func.functions.get(&(operation.arg2 as u32)); if func_pos.is_none() { - let err = create_error(&format!("Function with ID `{}` not found", operation.arg2), 0, ErrorType::MachineError, ErrorSubType::UnknownFunction); + let err = create_error(&format!("Function with ID `{}` not found", operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownFunction); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let func_pos = func_pos.unwrap(); let func = self.functions.get(*func_pos as usize); if func.is_none() { - let err = create_error(&format!("Function with position `{}` not found", func_pos), 0, ErrorType::MachineError, ErrorSubType::UnknownFunction); + let err = create_error(&format!("Function with position `{}` not found", func_pos), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownFunction); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -661,13 +661,13 @@ impl Machine { //ASS let reg = get_register_by_id(&self.registers, operation.arg1); if reg.is_none() { - let err = create_error(&format!("Register `{}` not found", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` not found", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let reg = reg.unwrap(); if reg.pointer >= self.memory.len() || reg.pointer < 1 { - let err = create_error(&format!("Register `{}` points to an invalid memory location", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Register `{}` points to an invalid memory location", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -686,7 +686,7 @@ impl Machine { let temp_regs = self.registers.clone(); let reg1 = get_register_by_id(&temp_regs, operation.arg1); if reg1.is_none() { - let err = create_error(&format!("Register `{}` not found", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` not found", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -702,13 +702,13 @@ impl Machine { let reg1 = get_register_by_id(®_clone, operation.arg1); let reg2 = get_register_by_id(®_clone, operation.arg2 as u8); if reg1.is_none() || reg2.is_none() { - let err = create_error(&format!("Register `{}` or `{}` not found", operation.arg1, operation.arg2), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` or `{}` not found", operation.arg1, operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let mem1 = reg1.unwrap().pointer; if mem1 >= self.memory.len() || mem1 < 1 { - let err = create_error(&format!("Memory location out of bounds for register `{}`", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location out of bounds for register `{}`", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -718,7 +718,7 @@ impl Machine { result = VMMemory::Boolean(VMMemoryBoolean { value: !bool.value, variable_id: 0 }); }, _ => { - let err = create_error(&format!("Wrong memory type for NOT operation, position: `{}`", mem1), 0, ErrorType::TypeError, ErrorSubType::WrongType); + let err = create_error(&format!("Wrong memory type for NOT operation, position: `{}`", mem1), operation.pos, ErrorType::TypeError, ErrorSubType::WrongType); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -741,14 +741,14 @@ impl Machine { //CJP let reg = get_register_by_id(&self.registers, operation.arg1); if reg.is_none() { - let err = create_error(&format!("Register `{}` not found", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` not found", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let reg = reg.unwrap(); let mem = self.memory.get(reg.pointer); if mem.is_none() { - let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::UnknownMemoryLocation); + let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownMemoryLocation); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -760,7 +760,7 @@ impl Machine { } } _ => { - let err = create_error(&format!("Wrong memory type for CJP operation, position: `{}`", reg.pointer), 0, ErrorType::TypeError, ErrorSubType::WrongType); + let err = create_error(&format!("Wrong memory type for CJP operation, position: `{}`", reg.pointer), operation.pos, ErrorType::TypeError, ErrorSubType::WrongType); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); }, @@ -770,14 +770,14 @@ impl Machine { //CAL let reg = get_register_by_id(&self.registers, operation.arg1); if reg.is_none() { - let err = create_error(&format!("Register `{}` not found", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` not found", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let reg = reg.unwrap(); let mem = self.memory.get(reg.pointer); if mem.is_none() { - let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::UnknownMemoryLocation); + let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownMemoryLocation); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -788,7 +788,7 @@ impl Machine { 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 { - let err = create_error(&format!("Too many arguments supplied for function: `{}`", func.id), 0, ErrorType::SemanticError, ErrorSubType::TooManyArguments); + let err = create_error(&format!("Too many arguments supplied for function: `{}`", func.id), operation.pos, ErrorType::SemanticError, ErrorSubType::TooManyArguments); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -816,14 +816,14 @@ impl Machine { //PSH let reg = get_register_by_id(&self.registers, operation.arg1); if reg.is_none() { - let err = create_error(&format!("Register `{}` not found", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` not found", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let reg = reg.unwrap(); let mem = self.memory.get(reg.pointer); if mem.is_none() { - let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -835,13 +835,13 @@ impl Machine { let target_reg = get_register_by_id(&self.registers, self.call_stack[executed_stack].return_reg as u8); let ret_reg = get_register_by_id(&self.registers, operation.arg1); if target_reg.is_none() || ret_reg.is_none() { - let err = create_error(&format!("Register `{}` or `{}` not found", self.call_stack[executed_stack].return_reg, operation.arg1), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` or `{}` not found", self.call_stack[executed_stack].return_reg, operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let ret_mem = self.memory.get(ret_reg.unwrap().pointer); if ret_mem.is_none() { - let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location not found for register `{}`", operation.arg1), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -865,13 +865,13 @@ impl Machine { let reg2 = get_register_by_id(®_clone, operation.arg2 as u8); let reg3 = get_register_by_id(®_clone, operation.arg3); if reg2.is_none() || reg3.is_none() { - let err = create_error(&format!("Register `{}` or `{}` not found", operation.arg2, operation.arg3), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` or `{}` not found", operation.arg2, operation.arg3), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let mem2 = self.memory.get(reg2.unwrap().pointer); if mem2.is_none() { - let err = create_error(&format!("Memory location out of bounds for register `{}`", operation.arg2), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location out of bounds for register `{}`", operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -887,7 +887,7 @@ impl Machine { } }, _ => { - let err = create_error(&format!("Only string keys are allowed for GET enviroment"), 0, ErrorType::TypeError, ErrorSubType::WrongType); + let err = create_error(&format!("Only string keys are allowed for GET enviroment"), operation.pos, ErrorType::TypeError, ErrorSubType::WrongType); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -907,14 +907,14 @@ impl Machine { let reg2 = get_register_by_id(®_clone, operation.arg2 as u8); let reg3 = get_register_by_id(®_clone, operation.arg3); if reg.is_none() || reg2.is_none() || reg3.is_none() { - let err = create_error(&format!("Register `{}` or `{}` or `{}` not found", operation.arg1, operation.arg2, operation.arg3), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` or `{}` or `{}` not found", operation.arg1, operation.arg2, operation.arg3), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } let mem1 = self.memory.get(reg.unwrap().pointer); let mem2 = self.memory.get(reg2.unwrap().pointer); if mem1.is_none() || mem2.is_none() { - let err = create_error(&format!("Memory location not found for register `{}` or `{}`", operation.arg1, operation.arg2), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location not found for register `{}` or `{}`", operation.arg1, operation.arg2), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -931,7 +931,7 @@ impl Machine { } }, _ => { - let err = create_error(&format!("Wrong memory type for GET operation, position: `{}`", reg.unwrap().pointer), 0, ErrorType::TypeError, ErrorSubType::WrongType); + let err = create_error(&format!("Wrong memory type for GET operation, position: `{}`", reg.unwrap().pointer), operation.pos, ErrorType::TypeError, ErrorSubType::WrongType); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -952,7 +952,7 @@ impl Machine { let reg2 = get_register_by_id(&self.registers, operation.arg2 as u8); let reg3 = get_register_by_id(&self.registers, operation.arg3); if reg.is_none() || reg2.is_none() || reg3.is_none() { - let err = create_error(&format!("Register `{}` or `{}` or `{}` not found", operation.arg1, operation.arg2, operation.arg3), 0, ErrorType::MachineError, ErrorSubType::RegisterNotFound); + let err = create_error(&format!("Register `{}` or `{}` or `{}` not found", operation.arg1, operation.arg2, operation.arg3), operation.pos, ErrorType::MachineError, ErrorSubType::RegisterNotFound); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -960,7 +960,7 @@ impl Machine { let mem2 = self.memory.get(reg2.unwrap().pointer); let mem3 = self.memory.get(reg3.unwrap().pointer); if mem1.is_none() || mem2.is_none() || mem3.is_none() { - let err = create_error(&format!("Memory location not found for register `{}` or `{}` or `{}`", operation.arg1, operation.arg2, operation.arg3), 0, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); + let err = create_error(&format!("Memory location not found for register `{}` or `{}` or `{}`", operation.arg1, operation.arg2, operation.arg3), operation.pos, ErrorType::MachineError, ErrorSubType::MemoryOutOfBounds); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } @@ -972,14 +972,14 @@ impl Machine { set_mem_tbl_val(tbl, mem2, mem3); }, _ => { - let err = create_error(&format!("Wrong memory type for SET operation, position: `{}`", reg.unwrap().pointer), 0, ErrorType::TypeError, ErrorSubType::WrongType); + let err = create_error(&format!("Wrong memory type for SET operation, position: `{}`", reg.unwrap().pointer), operation.pos, ErrorType::TypeError, ErrorSubType::WrongType); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } } }, _ => { - let err = create_error(&format!("Unknown operation code: `{}`", operation.opcode), 0, ErrorType::MachineError, ErrorSubType::UnknownOPCode); + let err = create_error(&format!("Unknown operation code: `{}`", operation.opcode), operation.pos, ErrorType::MachineError, ErrorSubType::UnknownOPCode); print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); }