From 54f82c4f415be770063a3315ebbb66e5b61c46a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:20:17 +0100 Subject: change errors in vm --- src/vm.rs | 67 +++++++++++++++++++++++++-------------------------------------- 1 file changed, 26 insertions(+), 41 deletions(-) (limited to 'src/vm.rs') diff --git a/src/vm.rs b/src/vm.rs index 2318aa6..fca2757 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -16,7 +16,7 @@ macro_rules! error { return Err($thing.error($kind, None)); }; ( $thing:expr, $kind:expr, $msg:expr) => { - return Err($thing.error($kind, Some($msg))); + return Err($thing.error($kind, Some(String::from($msg)))); }; } @@ -301,7 +301,7 @@ impl VM { } (val, slot) => { self.stack.push(Value::Nil); - error!(self, ErrorKind::ValueError(op, vec![val, slot]), String::from("Cannot index type")); + error!(self, ErrorKind::IndexError(val, slot.into())); } } } @@ -313,7 +313,7 @@ impl VM { let slot = ty.fields.get(field).unwrap().0; self.push(values.borrow()[slot].clone()); } else { - error!(self, ErrorKind::ValueError(op, vec![inst])); + error!(self, ErrorKind::UnkownField(inst, field.clone())); } } @@ -324,7 +324,7 @@ impl VM { let slot = ty.fields.get(field).unwrap().0; values.borrow_mut()[slot] = value; } else { - error!(self, ErrorKind::ValueError(op, vec![inst])); + error!(self, ErrorKind::UnkownField(inst, field.clone())); } } @@ -432,10 +432,7 @@ impl VM { let inner = block.borrow(); let args = inner.args(); if args.len() != num_args { - error!(self, - ErrorKind::InvalidProgram, - format!("Invalid number of arguments, got {} expected {}.", - num_args, args.len())); + error!(self, ErrorKind::ArgumentCount(num_args, args.len())); } if self.print_blocks { @@ -452,7 +449,7 @@ impl VM { let extern_func = self.extern_functions[slot]; let res = match extern_func(&self.stack[new_base+1..], false) { Ok(value) => value, - Err(ek) => error!(self, ek, "Wrong arguments to external function".to_string()), + Err(ek) => error!(self, ek, "Failed in external function."), }; self.stack.truncate(new_base); self.push(res); @@ -575,10 +572,7 @@ impl VM { *ty = suggestion.clone(); } else { if ty != suggestion { - error!(self, - ErrorKind::TypeError(op, - vec![ty.clone(), suggestion.clone()]), - "Failed to infer type.".to_string()); + error!(self, ErrorKind::CannotInfer(ty.clone(), suggestion.clone())); } } }; @@ -596,8 +590,9 @@ impl VM { let value = Value::from(ty.fields.get(field).unwrap().1.clone()); self.push(value); } else { + let field = field.clone(); self.push(Value::Nil); - error!(self, ErrorKind::ValueError(op, vec![inst])); + error!(self, ErrorKind::UnkownField(inst, field)); } } @@ -606,12 +601,14 @@ impl VM { let field = self.string(field); if let Value::Instance(ty, _) = inst { - let ty = &ty.fields.get(field).unwrap().1; - if ty != &Type::from(&value) { - error!(self, ErrorKind::ValueError(op, vec![inst])); + let ty = &self.blobs[ty].fields.get(field).unwrap().1; + let expected = Type::from(&value); + if ty != &expected { + error!(self, ErrorKind::TypeMismatch(expected, ty.clone()), + "Field and variables type deosn't match."); } } else { - error!(self, ErrorKind::ValueError(op, vec![inst])); + error!(self, ErrorKind::UnkownField(inst, field.clone())); } } @@ -628,8 +625,8 @@ impl VM { let var = self.frame().block.borrow().upvalues[slot].2.clone(); let up = self.pop().into(); if var != up { - error!(self, ErrorKind::TypeError(op, vec![var, up]), - "Incorrect type for upvalue.".to_string()); + error!(self, ErrorKind::TypeMismatch(up, var), + "Captured varibles type doesn't match upvalue."); } } @@ -638,9 +635,8 @@ impl VM { let inner = self.frame().block.borrow(); let ret = inner.ret(); if Type::from(&a) != *ret { - error!(self, ErrorKind::TypeError(op, vec![a.into(), - ret.clone()]), - "Not matching return type.".to_string()); + error!(self, ErrorKind::TypeMismatch(a.into(), ret.clone()), + "Value match return type."); } } @@ -655,12 +651,8 @@ impl VM { (Type::Unknown, top_type) if top_type != Type::Unknown => {} (a, b) if a != &b => { - error!(self, - ErrorKind::TypeError( - op, - vec![a.clone(), b.clone()]), - format!("Tried to assign a type {:?} to type {:?}.", a, b) - ); + error!(self, ErrorKind::TypeMismatch(a.clone(), b.clone()), + "Cannot assign missmatching types."); } _ => {} } @@ -699,19 +691,13 @@ impl VM { let inner = block.borrow(); let args = inner.args(); if args.len() != num_args { - error!(self, - ErrorKind::InvalidProgram, - format!("Invalid number of arguments, got {} expected {}.", - num_args, args.len())); + error!(self, ErrorKind::ArgumentCount(num_args, args.len())); } let stack_args = &self.stack[self.stack.len() - args.len()..]; let stack_args: Vec<_> = stack_args.iter().map(|x| x.into()).collect(); if args != &stack_args { - error!(self, - ErrorKind::TypeError(op, vec![]), - format!("Expected args of type {:?} but got {:?}.", - args, stack_args)); + error!(self, ErrorKind::ArgumentType(args.clone(), stack_args)); } self.stack[new_base] = block.borrow().ret().into(); @@ -725,16 +711,15 @@ impl VM { Err(ek) => { self.stack.truncate(new_base); self.push(Value::Nil); - error!(self, ek, "Wrong arguments to external function".to_string()) + error!(self, ek, "Error from external function.") } }; self.stack.truncate(new_base); self.push(res); } _ => { - error!(self, - ErrorKind::TypeError(op, vec![Type::from(&self.stack[new_base])]), - format!("Tried to call non-function {:?}", self.stack[new_base])); + error!(self, ErrorKind::ValueError(op, vec![self.stack[new_base].clone()]), + "Tried to call non-function."); } } } -- cgit v1.2.1