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 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/error.rs') 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()) -- 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 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/error.rs') 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") + } } } } -- 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/error.rs') 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/error.rs') 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 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 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/error.rs') 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()) -- 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 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/error.rs') 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) => { -- cgit v1.2.1