From 63568dc3ad9dc478eac800fb5a5aba3d749f880a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 21:31:59 +0100 Subject: make the assert_errs output way clearer --- src/lib.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index ee176e5..69508cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -730,9 +730,14 @@ mod tests { #[macro_export] macro_rules! assert_errs { ($result:expr, [ $( $kind:pat ),* ]) => { - eprintln!("{} => {:?}", stringify!($result), $result); - assert!(matches!( - $result.unwrap_err().as_slice(), + let errs = if let Err(errs) = $result { + errs + } else { + eprintln!(" PROGRAM SUCCEEDED, WHEN IT SHOULD FAIL!"); + unreachable!(); + }; + if !matches!( + errs.as_slice(), &[$($crate::error::Error { kind: $kind, file: _, @@ -740,7 +745,19 @@ mod tests { message: _, }, )*] - )) + ) { + eprintln!("UNEXPECTED ERRORS"); + eprint!(" GOT: ["); + for err in errs { + eprint!(" ErrorKind::{:?} ", err.kind); + } + eprint!("]\n WANT: ["); + $( + eprint!(" {} ", stringify!($kind)); + )* + eprintln!("]"); + assert!(false); + } }; } @@ -823,6 +840,11 @@ mod tests { assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\n", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); } + #[test] + fn assign_to_constant_upvalue2() { + assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\n", true, Vec::new()), [ErrorKind::InvalidProgram]); + } + macro_rules! test_multiple { ($mod:ident, $( $fn:ident : $prog:literal ),+ $( , )? ) => { mod $mod { -- cgit v1.2.1 From 326b39b027a415e767fcdb9d40585bb68544c7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 21:35:14 +0100 Subject: write testfails on stderr --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 69508cf..153b2a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -797,8 +797,8 @@ mod tests { for e in errs.iter() { println!("{}", e); } - println!(" {} - FAILED\n", stringify!($fn)); - panic!(); + eprintln!(" {} - FAILED\n", stringify!($fn)); + unreachable!(); } } }); -- cgit v1.2.1 From 946828d4cd281c073663f3c5b078c1cf5ed1ddde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 21:35:43 +0100 Subject: remove test-testcase --- src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 153b2a7..3fa4cb6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -840,11 +840,6 @@ mod tests { assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\n", true, Vec::new()), [ErrorKind::SyntaxError(_, _)]); } - #[test] - fn assign_to_constant_upvalue2() { - assert_errs!(run_string("a :: 2\nq :: fn { a = 2 }\n", true, Vec::new()), [ErrorKind::InvalidProgram]); - } - macro_rules! test_multiple { ($mod:ident, $( $fn:ident : $prog:literal ),+ $( , )? ) => { mod $mod { -- cgit v1.2.1 From 6b9b43565c66e1da12d775ffd2881a9ea461d604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 23:46:20 +0100 Subject: bring back the benching --- src/vm.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index c4d72a1..4ed19ff 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -480,8 +480,9 @@ impl VM { self.frame().block.borrow().ops[self.frame().ip]); } - // Initalizes the VM for running. Run cannot be called before this. - pub(crate) fn init(&mut self, prog: &Prog) { + /// Initalizes the VM for running. Run cannot be called before this. + /// You do not have to call this. + pub fn init(&mut self, prog: &Prog) { let block = Rc::clone(&prog.blocks[0]); self.blobs = prog.blobs.clone(); self.constants = prog.constants.clone(); -- cgit v1.2.1 From 33e87da47b0d7dedc0b88d7e732f398fc602deca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Thu, 18 Feb 2021 20:03:39 +0100 Subject: stop scrreaming on errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gustav Sörnäs --- src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 3fa4cb6..d387316 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -733,7 +733,7 @@ mod tests { let errs = if let Err(errs) = $result { errs } else { - eprintln!(" PROGRAM SUCCEEDED, WHEN IT SHOULD FAIL!"); + eprintln!(" Program succeeded when it should've failed"); unreachable!(); }; if !matches!( @@ -746,12 +746,12 @@ mod tests { }, )*] ) { - eprintln!("UNEXPECTED ERRORS"); - eprint!(" GOT: ["); + eprintln!("Unexpected errors"); + eprint!(" Got: ["); for err in errs { eprint!(" ErrorKind::{:?} ", err.kind); } - eprint!("]\n WANT: ["); + eprint!("]\n Want: ["); $( eprint!(" {} ", stringify!($kind)); )* @@ -797,7 +797,7 @@ mod tests { for e in errs.iter() { println!("{}", e); } - eprintln!(" {} - FAILED\n", stringify!($fn)); + eprintln!(" {} - failed\n", stringify!($fn)); unreachable!(); } } -- cgit v1.2.1 From 5c26782c500bb9aee86ba7b35b2f145c1dd87dc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Thu, 18 Feb 2021 20:11:24 +0100 Subject: hide vm::init --- src/vm.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index 4ed19ff..e961b73 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -480,8 +480,7 @@ impl VM { self.frame().block.borrow().ops[self.frame().ip]); } - /// Initalizes the VM for running. Run cannot be called before this. - /// You do not have to call this. + #[doc(hidden)] pub fn init(&mut self, prog: &Prog) { let block = Rc::clone(&prog.blocks[0]); self.blobs = prog.blobs.clone(); -- cgit v1.2.1 From eca549af7e048ecddd61fcedd4202b1fe8948b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 21:56:34 +0100 Subject: RuntimeTypeError -> ValueError --- src/error.rs | 7 ++++--- src/vm.rs | 16 ++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index deab89e..4e2209e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,11 +14,12 @@ pub enum ErrorKind { TypeError(Op, Vec), /// (External function, parameters) ExternTypeMismatch(String, Vec), - RuntimeTypeError(Op, Vec), + ValueError(Op, Vec), /// (Indexed value, length, index) IndexOutOfBounds(Value, usize, usize), + /// Sanity checking errors AssertFailed, InvalidProgram, Unreachable, @@ -50,11 +51,11 @@ impl fmt::Display for ErrorKind { ErrorKind::ExternTypeMismatch(name, types) => { write!(f, "{} Extern function '{}' doesn't accept argument(s) with type(s) {:?}", "Type Error".bold(), name, types) } - ErrorKind::RuntimeTypeError(op, values) => { + ErrorKind::ValueError(op, values) => { let values = values .iter() .fold(String::new(), |a, v| { format!("{}{:?}, ", a, v) }); - write!(f, "{} Cannot apply {:?} to values {}", "Runtime Type Error".bold(), op, values) + write!(f, "{} Cannot apply {:?} to values {}", "Value Error".bold(), op, values) } ErrorKind::AssertFailed => { write!(f, "{}", "Assertion failed".bold()) diff --git a/src/vm.rs b/src/vm.rs index bfb5ce4..2318aa6 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -26,7 +26,7 @@ macro_rules! one_op { let b = $fun(&a); if b.is_nil() { $self.push(b); - error!($self, ErrorKind::RuntimeTypeError($op, vec![a])); + error!($self, ErrorKind::ValueError($op, vec![a])); } $self.push(b); }; @@ -38,7 +38,7 @@ macro_rules! two_op { let c = $fun(&a, &b); if c.is_nil() { $self.push(c); - error!($self, ErrorKind::RuntimeTypeError($op, vec![a, b])); + error!($self, ErrorKind::ValueError($op, vec![a, b])); } $self.push(c); }; @@ -301,7 +301,7 @@ impl VM { } (val, slot) => { self.stack.push(Value::Nil); - error!(self, ErrorKind::RuntimeTypeError(op, vec![val, slot]), String::from("Cannot index type")); + error!(self, ErrorKind::ValueError(op, vec![val, slot]), String::from("Cannot index type")); } } } @@ -313,7 +313,7 @@ impl VM { let slot = ty.fields.get(field).unwrap().0; self.push(values.borrow()[slot].clone()); } else { - error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); + error!(self, ErrorKind::ValueError(op, vec![inst])); } } @@ -324,7 +324,7 @@ impl VM { let slot = ty.fields.get(field).unwrap().0; values.borrow_mut()[slot] = value; } else { - error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); + error!(self, ErrorKind::ValueError(op, vec![inst])); } } @@ -597,7 +597,7 @@ impl VM { self.push(value); } else { self.push(Value::Nil); - error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); + error!(self, ErrorKind::ValueError(op, vec![inst])); } } @@ -608,10 +608,10 @@ impl VM { if let Value::Instance(ty, _) = inst { let ty = &ty.fields.get(field).unwrap().1; if ty != &Type::from(&value) { - error!(self, ErrorKind::RuntimeTypeError(op, vec![Value::from(ty)])); + error!(self, ErrorKind::ValueError(op, vec![inst])); } } else { - error!(self, ErrorKind::RuntimeTypeError(op, vec![inst])); + error!(self, ErrorKind::ValueError(op, vec![inst])); } } -- cgit v1.2.1 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/error.rs | 11 +++++++++- src/vm.rs | 67 +++++++++++++++++++++++------------------------------------- 2 files changed, 36 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index 4e2209e..337acab 100644 --- a/src/error.rs +++ b/src/error.rs @@ -12,14 +12,20 @@ use crate::tokenizer::Token; #[derive(Debug, Clone)] pub enum ErrorKind { TypeError(Op, Vec), + TypeMismatch(Type, Type), + CannotInfer(Type, Type), + ArgumentType(Vec, Vec), + IndexError(Value, Type), + /// (External function, parameters) ExternTypeMismatch(String, Vec), ValueError(Op, Vec), + UnkownField(Value, String), + ArgumentCount(usize, usize), /// (Indexed value, length, index) IndexOutOfBounds(Value, usize, usize), - /// Sanity checking errors AssertFailed, InvalidProgram, Unreachable, @@ -69,6 +75,9 @@ impl fmt::Display for ErrorKind { ErrorKind::InvalidProgram => { write!(f, "{}", "[!!] Invalid program [!!]".bold()) } + _ => { + write!(f, "{}", "[!!] UNHANDLED ERROR") + } } } } 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 From 67e9dc6cdc05b340821fc8669eaaff0797356466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:24:53 +0100 Subject: fix test errors --- src/vm.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index fca2757..6e52fc3 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -811,20 +811,20 @@ impl VM { mod tests { mod typing { use crate::error::ErrorKind; - use crate::test_string; + use crate::{test_string, Op, Type}; test_string!(uncallable_type, " f := fn i: int { i() }", - [ErrorKind::TypeError(_, _)]); + [ErrorKind::ValueError(Op::Call(0), _)]); test_string!(wrong_params, " f : fn -> int = fn a: int -> int {}", - [ErrorKind::TypeError(_, _), ErrorKind::TypeError(_, _)]); + [ErrorKind::TypeMismatch(_, _), ErrorKind::TypeMismatch(Type::Void, Type::Int)]); test_string!(wrong_ret, " f : fn -> int = fn {}", - [ErrorKind::TypeError(_, _)]); + [ErrorKind::TypeMismatch(_, _)]); } } -- cgit v1.2.1 From 63a1373ac0bed85b06f4e6a425941f862f6dd178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:39:58 +0100 Subject: add error messages --- src/error.rs | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index 337acab..9d746fe 100644 --- a/src/error.rs +++ b/src/error.rs @@ -49,22 +49,40 @@ impl fmt::Display for ErrorKind { let types = types .iter() .fold(String::new(), |a, v| { format!("{}{:?}, ", a, v) }); - write!(f, "{} Cannot apply {:?} to types {}", "Type Error".bold(), op, types) + write!(f, "Cannot apply {:?} to types {}", op, types) + } + ErrorKind::TypeMismatch(a, b) => { + write!(f, "Expected '{:?}' and got '{:?}'.", a, b) + } + ErrorKind::CannotInfer(a, b) => { + write!(f, "Failed to infer type '{:?}' from '{:?}'.", a, b) + } + ErrorKind::ArgumentType(a, b) => { + let expected = a + .iter() + .fold(String::new(), |a, v| { format!("{}{:?}, ", a, v) }); + let given = b + .iter() + .fold(String::new(), |a, v| { format!("{}{:?}, ", a, v) }); + write!(f, "Argument types don't match, expected [{:?}] but got [{:?}]", + expected, given) } ErrorKind::IndexOutOfBounds(value, len, slot) => { - write!(f, "{} for {:?} - length is {} but index is {}", "Index Error".bold(), value, len, slot) + write!(f, "Failed to index for {:?} - length is {} but index is {}", + value, len, slot) } ErrorKind::ExternTypeMismatch(name, types) => { - write!(f, "{} Extern function '{}' doesn't accept argument(s) with type(s) {:?}", "Type Error".bold(), name, types) + write!(f, "Extern function '{}' doesn't accept argument(s) with type(s) {:?}", + name, types) } ErrorKind::ValueError(op, values) => { let values = values .iter() .fold(String::new(), |a, v| { format!("{}{:?}, ", a, v) }); - write!(f, "{} Cannot apply {:?} to values {}", "Value Error".bold(), op, values) + write!(f, "Cannot apply {:?} to values {}", op, values) } ErrorKind::AssertFailed => { - write!(f, "{}", "Assertion failed".bold()) + write!(f, "Assertion failed") } ErrorKind::SyntaxError(line, token) => { write!(f, "{} on line {} at token {:?}", "Syntax Error".bold(), line, token) @@ -75,8 +93,15 @@ impl fmt::Display for ErrorKind { ErrorKind::InvalidProgram => { write!(f, "{}", "[!!] Invalid program [!!]".bold()) } - _ => { - write!(f, "{}", "[!!] UNHANDLED ERROR") + ErrorKind::IndexError(value, slot) => { + write!(f, "Cannot index value '{:?}' with type '{:?}'.", value, slot) + } + ErrorKind::UnkownField(obj, field) => { + write!(f, "Cannot find field '{}' on {:?}", field, obj) + } + ErrorKind::ArgumentCount(expected, given) => { + write!(f, "Incorrect argument count, expected {} but got {}.", + expected, given) } } } -- cgit v1.2.1 From a00e2b3f034cc8739eaa983977f5b63948feec07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:40:12 +0100 Subject: change the error message a bit --- src/error.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index 9d746fe..b42db3f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -109,8 +109,9 @@ impl fmt::Display for ErrorKind { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let prompt = "*****".red(); let message = match &self.message { - Some(s) => format!("\n{} {}", ">>>".red(), s), + Some(s) => format!("\n{} {}", prompt, s), None => String::from(""), }; @@ -122,7 +123,8 @@ impl fmt::Display for Error { String::new() }; - write!(f, "\n<{}> {}:{} {}{}{}\n", "ERR".red(), self.file.display().blue(), self.line.blue(), self.kind, message, line) + write!(f, "\n {} {}:{} \n{} {}{}{}\n", "ERR".red(), + self.file.display().blue(), self.line.blue(), prompt, self.kind, message, line) } } -- cgit v1.2.1 From ab05add4c3fbe25c688e16af9d8a9849669b797e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:42:38 +0100 Subject: make op errors into TypeError --- src/vm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index 6e52fc3..8208551 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -26,7 +26,7 @@ macro_rules! one_op { let b = $fun(&a); if b.is_nil() { $self.push(b); - error!($self, ErrorKind::ValueError($op, vec![a])); + error!($self, ErrorKind::TypeError($op, vec![a.into()])); } $self.push(b); }; @@ -38,7 +38,7 @@ macro_rules! two_op { let c = $fun(&a, &b); if c.is_nil() { $self.push(c); - error!($self, ErrorKind::ValueError($op, vec![a, b])); + error!($self, ErrorKind::TypeError($op, vec![a.into(), b.into()])); } $self.push(c); }; -- cgit v1.2.1 From 79f8e81b7b4d89231fcff207ff2c13e4c24cde56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:53:51 +0100 Subject: find and fix error in typesystem --- src/vm.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index 8208551..791bebe 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -630,6 +630,16 @@ impl VM { } } + Op::AssignLocal(slot) => { + let slot = self.frame().stack_offset + slot; + let curr = Type::from(&self.stack[slot]); + let other = Type::from(self.pop()); + if curr != other { + error!(self, ErrorKind::TypeMismatch(curr, other), + "Cannot assign to different type."); + } + } + Op::Return => { let a = self.pop(); let inner = self.frame().block.borrow(); @@ -819,6 +829,9 @@ mod tests { }", [ErrorKind::ValueError(Op::Call(0), _)]); + test_string!(invalid_assign, "a := 1\na = 0.1\n", + [ErrorKind::TypeMismatch(Type::Int, Type::Float)]); + test_string!(wrong_params, " f : fn -> int = fn a: int -> int {}", [ErrorKind::TypeMismatch(_, _), ErrorKind::TypeMismatch(Type::Void, Type::Int)]); -- cgit v1.2.1 From 98001dd938b4fc176a7ce9b044415ca0b581cad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 22:54:21 +0100 Subject: fix minor typos --- src/error.rs | 6 +++--- src/vm.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index b42db3f..b897c80 100644 --- a/src/error.rs +++ b/src/error.rs @@ -64,7 +64,7 @@ impl fmt::Display for ErrorKind { let given = b .iter() .fold(String::new(), |a, v| { format!("{}{:?}, ", a, v) }); - write!(f, "Argument types don't match, expected [{:?}] but got [{:?}]", + write!(f, "Argument types do not match, expected [{:?}] but got [{:?}]", expected, given) } ErrorKind::IndexOutOfBounds(value, len, slot) => { @@ -85,10 +85,10 @@ impl fmt::Display for ErrorKind { write!(f, "Assertion failed") } ErrorKind::SyntaxError(line, token) => { - write!(f, "{} on line {} at token {:?}", "Syntax Error".bold(), line, token) + write!(f, "Syntax Error on line {} at token {:?}", line, token) } ErrorKind::Unreachable => { - write!(f, "{}", "Unreachable".bold()) + write!(f, "Reached unreachable code.") } ErrorKind::InvalidProgram => { write!(f, "{}", "[!!] Invalid program [!!]".bold()) diff --git a/src/vm.rs b/src/vm.rs index 791bebe..a5b4af6 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -432,7 +432,7 @@ impl VM { let inner = block.borrow(); let args = inner.args(); if args.len() != num_args { - error!(self, ErrorKind::ArgumentCount(num_args, args.len())); + error!(self, ErrorKind::ArgumentCount(args.len(), num_args)); } if self.print_blocks { @@ -646,7 +646,7 @@ impl VM { let ret = inner.ret(); if Type::from(&a) != *ret { error!(self, ErrorKind::TypeMismatch(a.into(), ret.clone()), - "Value match return type."); + "Value does not match return type."); } } @@ -701,7 +701,7 @@ impl VM { let inner = block.borrow(); let args = inner.args(); if args.len() != num_args { - error!(self, ErrorKind::ArgumentCount(num_args, args.len())); + error!(self, ErrorKind::ArgumentCount(args.len(), num_args)); } let stack_args = &self.stack[self.stack.len() - args.len()..]; -- cgit v1.2.1 From 0968d7a4c7a9c4daf90b18de51cac08ce199f142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Thu, 18 Feb 2021 20:05:36 +0100 Subject: fix spelling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Gustav Sörnäs --- src/vm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index a5b4af6..a1c58d9 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -605,7 +605,7 @@ impl VM { let expected = Type::from(&value); if ty != &expected { error!(self, ErrorKind::TypeMismatch(expected, ty.clone()), - "Field and variables type deosn't match."); + "Types of field and variable do not match."); } } else { error!(self, ErrorKind::UnkownField(inst, field.clone())); @@ -662,7 +662,7 @@ impl VM { if top_type != Type::Unknown => {} (a, b) if a != &b => { error!(self, ErrorKind::TypeMismatch(a.clone(), b.clone()), - "Cannot assign missmatching types."); + "Cannot assign mismatching types."); } _ => {} } -- cgit v1.2.1 From 2fa8665203f9e1998c0c3aae6714605f2db2fd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Thu, 18 Feb 2021 20:06:29 +0100 Subject: UnkownField -> UnknownField --- src/error.rs | 4 ++-- src/vm.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/error.rs b/src/error.rs index b897c80..c2ad228 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,7 +20,7 @@ pub enum ErrorKind { /// (External function, parameters) ExternTypeMismatch(String, Vec), ValueError(Op, Vec), - UnkownField(Value, String), + UnknownField(Value, String), ArgumentCount(usize, usize), /// (Indexed value, length, index) @@ -96,7 +96,7 @@ impl fmt::Display for ErrorKind { ErrorKind::IndexError(value, slot) => { write!(f, "Cannot index value '{:?}' with type '{:?}'.", value, slot) } - ErrorKind::UnkownField(obj, field) => { + ErrorKind::UnknownField(obj, field) => { write!(f, "Cannot find field '{}' on {:?}", field, obj) } ErrorKind::ArgumentCount(expected, given) => { diff --git a/src/vm.rs b/src/vm.rs index a1c58d9..468e5df 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -313,7 +313,7 @@ impl VM { let slot = ty.fields.get(field).unwrap().0; self.push(values.borrow()[slot].clone()); } else { - error!(self, ErrorKind::UnkownField(inst, field.clone())); + error!(self, ErrorKind::UnknownField(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::UnkownField(inst, field.clone())); + error!(self, ErrorKind::UnknownField(inst, field.clone())); } } @@ -592,7 +592,7 @@ impl VM { } else { let field = field.clone(); self.push(Value::Nil); - error!(self, ErrorKind::UnkownField(inst, field)); + error!(self, ErrorKind::UnknownField(inst, field)); } } @@ -608,7 +608,7 @@ impl VM { "Types of field and variable do not match."); } } else { - error!(self, ErrorKind::UnkownField(inst, field.clone())); + error!(self, ErrorKind::UnknownField(inst, field.clone())); } } -- cgit v1.2.1 From fdc57adf5645a27bc26976375bd48b0c5316b9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Fri, 19 Feb 2021 18:04:38 +0100 Subject: Update src/lib.rs --- src/vm.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/vm.rs b/src/vm.rs index 468e5df..020e90c 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -280,7 +280,7 @@ impl VM { Value::Function(ups, block) }, value => error!(self, - ErrorKind::RuntimeTypeError(op, vec![value.clone()]), + ErrorKind::ValueError(op, vec![value.clone()]), format!("Not a function {:?}.", value)), }; self.constants[slot] = constant; @@ -601,7 +601,7 @@ impl VM { let field = self.string(field); if let Value::Instance(ty, _) = inst { - let ty = &self.blobs[ty].fields.get(field).unwrap().1; + let ty = &ty.fields.get(field).unwrap().1; let expected = Type::from(&value); if ty != &expected { error!(self, ErrorKind::TypeMismatch(expected, ty.clone()), -- cgit v1.2.1