labast/src/errors.rs

79 lines
2.8 KiB
Rust

use std::fmt::Display;
pub enum StackError {
StackOverflow,
StackUnderflow,
}
impl Display for StackError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
StackError::StackOverflow => write!(f, "Stack overflow!"),
StackError::StackUnderflow => write!(f, "Stack underflow!"),
}
}
}
pub enum RunError {
RequestArgsInMainModule,
ExecuteItself(String),
FailToReadFile,
FailToGetCharFromConsole,
InvalidInputUShortInt,
PickTooDeep,
PickOutOfBounds,
FailToReadLineFromConsole,
UnknownLabel(u16),
InvalidExpressionUnknownOperator(String),
AttemptToInsertHashOrStringInst,
AttemptToInsertEmptyConst,
AttemptToInsertALabel,
}
impl Display for RunError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RunError::RequestArgsInMainModule => {
write!(f, "Can't require arguments in the main module")
}
RunError::ExecuteItself(mod_name) => {
write!(f, "Module {}: Can't execute itself", mod_name)
}
RunError::FailToReadFile => write!(f, "Unable to read file"),
RunError::FailToGetCharFromConsole => write!(f, "Failed to get character from console"),
RunError::InvalidInputUShortInt => write!(
f,
"Invalid input. Please enter a valid unsigned short integer"
),
RunError::PickTooDeep => write!(f, "Picking too deep"),
RunError::PickOutOfBounds => write!(f, "Trying to get a value out of bounds"),
RunError::FailToReadLineFromConsole => write!(f, "Failed to read line from console"),
RunError::UnknownLabel(label_name) => write!(f, "Unknown label: {}", label_name),
RunError::InvalidExpressionUnknownOperator(name) => {
write!(f, "Invalid expression: unknown operator '{}'", name)
}
RunError::AttemptToInsertHashOrStringInst => {
write!(f, "Attempt to insert a hash/string-instruction")
}
RunError::AttemptToInsertEmptyConst => write!(f, "Attempt to insert an empty constant"),
RunError::AttemptToInsertALabel => write!(f, "Attempt to insert a label"),
}
}
}
pub enum ParseError {
ArgumentNotRequired(String, usize, String),
}
impl Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ParseError::ArgumentNotRequired(name, line_number, line) => write!(
f,
"ParseError:\n{1} | {2}\nerror: string argument for {0} not required",
name, line_number, line
),
}
}
}