diff options
| author | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-01-10 16:15:52 +0100 |
|---|---|---|
| committer | Edvard Thörnros <edvard.thornros@gmail.com> | 2021-01-10 16:15:52 +0100 |
| commit | d61370656d9f3deb39bb37f9c1d45e8ddc62efd5 (patch) | |
| tree | 9fa83578abc1e18c4415c97bbfce10886bbc2daa /src/error.rs | |
| parent | 64576ba8f08990dc8bc94ef1bec4dd502d5cef06 (diff) | |
| download | sylt-d61370656d9f3deb39bb37f9c1d45e8ddc62efd5.tar.gz | |
More errors!
Diffstat (limited to 'src/error.rs')
| -rw-r--r-- | src/error.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..277be5f --- /dev/null +++ b/src/error.rs @@ -0,0 +1,48 @@ +use std::fmt; +use std::path::PathBuf; +use crate::vm::{Op, Value}; + +#[derive(Debug)] +pub enum ErrorKind { + TypeError(Op, Vec<Value>), + AssertFailed(Value, Value), + SyntaxError(usize), +} + +#[derive(Debug)] +pub struct Error { + pub kind: ErrorKind, + pub file: PathBuf, + pub line: usize, + pub message: Option<String>, +} + +impl fmt::Display for ErrorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ErrorKind::TypeError(op, values) => { + let values = values + .iter() + .fold(String::new(), |a, v| { format!("{}, {:?}", a, v) }); + write!(f, "Cannot apply {:?} to values {}", op, values) + } + ErrorKind::AssertFailed(a, b) => { + write!(f, "Assertion failed, {:?} != {:?}.", a, b) + } + ErrorKind::SyntaxError(line) => { + write!(f, "Syntax error on line {}", line) + } + } + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let message = match &self.message { + Some(s) => format!("\n{}", s), + None => String::from(""), + }; + write!(f, "{:?}:{} [Runtime Error] {}{}", self.file, self.line, self.kind, message) + } +} + |
