labast/src/errors.rs

63 lines
2.5 KiB
Rust
Raw Normal View History

2024-02-05 07:05:48 +00:00
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!"),
}
}
}
2024-02-05 07:05:48 +00:00
pub enum RunError {
RequestArgsInMainModule,
ExecuteItself(String),
FailToReadFile,
FailToGetCharFromConsole,
InvalidInputUShortInt,
PickTooDeep,
PickOutOfBounds,
FailToReadLineFromConsole,
UnknownLabel(u16),
InvalidExpressionUnknownOperator(String),
AttemptToInsertHashOrStringInst,
AttemptToInsertEmptyConst,
AttemptToInsertALabel
2024-02-05 07:05:48 +00:00
}
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"),
2024-02-05 07:05:48 +00:00
}
}
}
pub enum ParseError {
ArgumentNotRequired(String, usize, String)
2024-02-05 07:05:48 +00:00
}
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),
2024-02-05 07:05:48 +00:00
}
}
}