diff --git a/src/errors.rs b/src/errors.rs index f40c8be..6bcbd07 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -35,7 +35,6 @@ pub enum ErrorSubType { //Math errors DivisionByZero, //Type errors - InvalidType, WrongType, } @@ -87,7 +86,6 @@ fn convert_subtypes_to_string(stype: &ErrorSubType) -> String { ErrorSubType::UnknownMemoryLocation => String::from("Unknown memory location"), ErrorSubType::NonFunctionCall => String::from("Non-function call"), ErrorSubType::DivisionByZero => String::from("Division by zero"), - ErrorSubType::InvalidType => String::from("Invalid type"), ErrorSubType::WrongType => String::from("Wrong type"), ErrorSubType::TooManyArguments => String::from("Too many arguments"), } @@ -114,7 +112,6 @@ fn convert_subtypes_to_short(stype: &ErrorSubType) -> String { ErrorSubType::UnknownMemoryLocation => String::from("UM:"), ErrorSubType::NonFunctionCall => String::from("NF:"), ErrorSubType::DivisionByZero => String::from("DZ:"), - ErrorSubType::InvalidType => String::from("IT:"), ErrorSubType::WrongType => String::from("WT:"), ErrorSubType::TooManyArguments => String::from("TA:"), } diff --git a/src/virtualmachine.rs b/src/virtualmachine.rs index 0647b02..6de7e10 100644 --- a/src/virtualmachine.rs +++ b/src/virtualmachine.rs @@ -267,6 +267,30 @@ fn operation_to_name(opcode: u8) -> String { } } +fn get_var_id(var: &VMMemory) -> u32 { + match var { + VMMemory::String(str) => str.variable_id, + VMMemory::Number(num) => num.variable_id, + VMMemory::Boolean(bool) => bool.variable_id, + VMMemory::Null(null) => null.variable_id, + VMMemory::Function(func) => func.variable_id, + VMMemory::Table(tbl) => tbl.variable_id, + VMMemory::NativeFunction(func) => func.variable_id, + } +} + +fn set_var_id(var: &mut VMMemory, target: u32) { + match var { + VMMemory::String(str) => str.variable_id = target, + VMMemory::Number(num) => num.variable_id = target, + VMMemory::Boolean(bool) => bool.variable_id = target, + VMMemory::Null(null) => null.variable_id = target, + VMMemory::Function(func) => func.variable_id = target, + VMMemory::Table(tbl) => tbl.variable_id = target, + VMMemory::NativeFunction(func) => func.variable_id = target, + } +} + fn do_operation_operation(registers: &mut Vec, memory: &mut Vec, operation: &DecompiledOperation, ctx: &Context) { let reg_clone = registers.clone(); let reg1 = get_register_by_id(®_clone, operation.arg1); @@ -391,7 +415,7 @@ fn do_operation_operation(registers: &mut Vec, memory: &mut Vec { - let err = create_error(&format!("Wrong memory types for operation: `{}`, position: `{}` and `{}`", operation_to_name(operation.opcode), mem1, mem2), operation.pos, ErrorType::TypeError, ErrorSubType::InvalidType); + let err = create_error(&format!("Wrong memory types for operation: `{}`, position: `{}` and `{}`", operation_to_name(operation.opcode), mem1, mem2), operation.pos, ErrorType::TypeError, ErrorSubType::WrongType); print_error(&err, &ctx); process::exit(1); } @@ -402,38 +426,8 @@ fn do_operation_operation(registers: &mut Vec, memory: &mut Vec str.variable_id, - VMMemory::Number(num) => num.variable_id, - VMMemory::Boolean(bool) => bool.variable_id, - VMMemory::Null(null) => null.variable_id, - VMMemory::Function(func) => func.variable_id, - VMMemory::Table(tbl) => tbl.variable_id, - VMMemory::NativeFunction(func) => func.variable_id, - }; - match &mut result { - VMMemory::String(str) => { - str.variable_id = var_id; - }, - VMMemory::Number(num) => { - num.variable_id = var_id; - }, - VMMemory::Boolean(bool) => { - bool.variable_id = var_id; - }, - VMMemory::Null(null) => { - null.variable_id = var_id; - }, - VMMemory::Function(func) => { - func.variable_id = var_id; - }, - VMMemory::Table(tbl) => { - tbl.variable_id = var_id; - }, - VMMemory::NativeFunction(func) => { - func.variable_id = var_id; - }, - } + let var_id = get_var_id(&memory[reg3_pointer]); + set_var_id(&mut result, var_id); memory[reg3_pointer] = result; } @@ -679,53 +673,9 @@ impl Machine { } let used_var = get_mem_pos_by_var_id(&self.memory, operation.arg2 as u32); if let Some(pos) = used_var { - match &mut self.memory[pos] { - 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; - }, - VMMemory::NativeFunction(func) => { - func.variable_id = 0; - }, - } - } - match &mut self.memory[reg.pointer] { - VMMemory::Number(num) => { - num.variable_id = operation.arg2 as u32; - }, - VMMemory::String(str) => { - str.variable_id = operation.arg2 as u32; - }, - VMMemory::Boolean(bool) => { - bool.variable_id = operation.arg2 as u32; - }, - VMMemory::Null(null) => { - null.variable_id = operation.arg2 as u32; - }, - VMMemory::Function(func) => { - func.variable_id = operation.arg2 as u32; - }, - VMMemory::Table(tbl) => { - tbl.variable_id = operation.arg2 as u32; - }, - VMMemory::NativeFunction(func) => { - func.variable_id = operation.arg2 as u32; - }, + set_var_id(&mut self.memory[pos], 0); } + set_var_id(&mut self.memory[reg.pointer], operation.arg2 as u32); }, 8 => { //UNB @@ -779,39 +729,9 @@ impl Machine { self.memory.push(VMMemory::Null(VMMemoryNull { variable_id: 0 })); set_register(&mut self.registers, Register { id: operation.arg2 as u8, pointer: reg2_pointer }); } - let var_id = match &self.memory[reg2.unwrap().pointer] { - VMMemory::String(str) => str.variable_id, - VMMemory::Number(num) => num.variable_id, - VMMemory::Boolean(bool) => bool.variable_id, - VMMemory::Null(null) => null.variable_id, - VMMemory::Function(func) => func.variable_id, - VMMemory::Table(tbl) => tbl.variable_id, - VMMemory::NativeFunction(func) => func.variable_id, - }; - match &mut result { - VMMemory::String(str) => { - str.variable_id = var_id; - }, - VMMemory::Number(num) => { - num.variable_id = var_id; - }, - VMMemory::Boolean(bool) => { - bool.variable_id = var_id; - }, - VMMemory::Null(null) => { - null.variable_id = var_id; - }, - VMMemory::Function(func) => { - func.variable_id = var_id; - }, - VMMemory::Table(tbl) => { - tbl.variable_id = var_id; - }, - VMMemory::NativeFunction(func) => { - func.variable_id = var_id; - }, - } - self.memory[reg2.unwrap().pointer] = result; + let var_id = get_var_id(&self.memory[reg2_pointer]); + set_var_id(&mut result, var_id); + self.memory[reg2_pointer] = result; }, 25 => { //JMP @@ -872,29 +792,7 @@ impl Machine { print_error(&err, &self.ctx[self.call_stack[executed_stack].func].clone()); process::exit(1); } - 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(vfunc) => { - vfunc.variable_id = self.functions[func.id].variables[arg].id; - }, - VMMemory::Table(tbl) => { - tbl.variable_id = self.functions[func.id].variables[arg].id; - }, - VMMemory::NativeFunction(nfunc) => { - nfunc.variable_id = self.functions[func.id].variables[arg].id; - }, - } + set_var_id(&mut new_mem, self.functions[func.id].variables[arg].id); self.memory.push(new_mem); } self.stack.clear(); @@ -1000,38 +898,8 @@ impl Machine { self.memory.push(VMMemory::Null(VMMemoryNull { variable_id: 0 })); set_register(&mut self.registers, Register { id: operation.arg3, pointer: reg3_pointer }); } - let var_id = match &self.memory[reg3.unwrap().pointer] { - VMMemory::String(str) => str.variable_id, - VMMemory::Number(num) => num.variable_id, - VMMemory::Boolean(bool) => bool.variable_id, - VMMemory::Null(null) => null.variable_id, - VMMemory::Function(func) => func.variable_id, - VMMemory::Table(tbl) => tbl.variable_id, - VMMemory::NativeFunction(func) => func.variable_id, - }; - match &mut result { - VMMemory::String(str) => { - str.variable_id = var_id; - }, - VMMemory::Number(num) => { - num.variable_id = var_id; - }, - VMMemory::Boolean(bool) => { - bool.variable_id = var_id; - }, - VMMemory::Null(null) => { - null.variable_id = var_id; - }, - VMMemory::Function(func) => { - func.variable_id = var_id; - }, - VMMemory::Table(tbl) => { - tbl.variable_id = var_id; - }, - VMMemory::NativeFunction(func) => { - func.variable_id = var_id; - }, - } + let var_id = get_var_id(&self.memory[reg3_pointer]); + set_var_id(&mut result, var_id); self.memory[reg3_pointer] = result; continue; } @@ -1074,39 +942,9 @@ impl Machine { self.memory.push(VMMemory::Null(VMMemoryNull { variable_id: 0 })); set_register(&mut self.registers, Register { id: operation.arg3 as u8, pointer: reg3_pointer }); } - let var_id = match &self.memory[reg3.unwrap().pointer] { - VMMemory::String(str) => str.variable_id, - VMMemory::Number(num) => num.variable_id, - VMMemory::Boolean(bool) => bool.variable_id, - VMMemory::Null(null) => null.variable_id, - VMMemory::Function(func) => func.variable_id, - VMMemory::Table(tbl) => tbl.variable_id, - VMMemory::NativeFunction(func) => func.variable_id, - }; - match &mut value { - VMMemory::String(str) => { - str.variable_id = var_id; - }, - VMMemory::Number(num) => { - num.variable_id = var_id; - }, - VMMemory::Boolean(bool) => { - bool.variable_id = var_id; - }, - VMMemory::Null(null) => { - null.variable_id = var_id; - }, - VMMemory::Function(func) => { - func.variable_id = var_id; - }, - VMMemory::Table(tbl) => { - tbl.variable_id = var_id; - }, - VMMemory::NativeFunction(func) => { - func.variable_id = var_id; - }, - } - self.memory[reg3.unwrap().pointer] = value; + let var_id = get_var_id(&self.memory[reg3_pointer]); + set_var_id(&mut value, var_id); + self.memory[reg3_pointer] = value; }, 31 => { //SET diff --git a/test.as b/test.as index 44bb932..714029a 100644 --- a/test.as +++ b/test.as @@ -1 +1,2 @@ -ugass(szaft"test"szaft, ugass) \ No newline at end of file +ugass(szaft"test"szaft, ugass) +gethelj a = 1 + piszv \ No newline at end of file