mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 12:22:42 +00:00
58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
|
|
use itertools::Itertools;
|
||
|
|
use rlox2_frontend::lexer::LexerError;
|
||
|
|
use thiserror::Error;
|
||
|
|
|
||
|
|
use crate::{Opcode, Value};
|
||
|
|
|
||
|
|
#[derive(Error, Debug)]
|
||
|
|
pub enum CompileError {}
|
||
|
|
|
||
|
|
#[derive(Error, Debug)]
|
||
|
|
pub enum RuntimeError {
|
||
|
|
#[error("Opcopde {opcode} had invalid operand {operand}")]
|
||
|
|
UnaryInvalidOperand { opcode: Opcode, operand: Value },
|
||
|
|
#[error("Opcopde {opcode} had invalid operands {left} and {right}")]
|
||
|
|
BinaryInvalidOperand { opcode: Opcode, left: Value, right: Value },
|
||
|
|
#[error("Division by zero")]
|
||
|
|
DivisionByZero,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Error, Debug)]
|
||
|
|
pub enum InterpretError {
|
||
|
|
#[error("{0}", format_multiple_errors(inner))]
|
||
|
|
LexerError { inner: Vec<LexerError> },
|
||
|
|
#[error("{inner}")]
|
||
|
|
CompileError { inner: CompileError },
|
||
|
|
#[error("{inner}")]
|
||
|
|
RuntimeError { inner: RuntimeError },
|
||
|
|
#[error("Called exit() with exit code {exit_code}")]
|
||
|
|
Exit { exit_code: i32 },
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<Vec<LexerError>> for InterpretError {
|
||
|
|
fn from(lexer_errs: Vec<LexerError>) -> Self {
|
||
|
|
InterpretError::LexerError { inner: lexer_errs }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<CompileError> for InterpretError {
|
||
|
|
fn from(compile_err: CompileError) -> Self {
|
||
|
|
InterpretError::CompileError { inner: compile_err }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl From<RuntimeError> for InterpretError {
|
||
|
|
fn from(runtime_err: RuntimeError) -> Self {
|
||
|
|
InterpretError::RuntimeError { inner: runtime_err }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn format_multiple_errors(errs: &Vec<impl std::error::Error>) -> String {
|
||
|
|
let msg = if errs.len() == 1 {
|
||
|
|
errs[0].to_string()
|
||
|
|
} else {
|
||
|
|
errs.iter().map(|err| err.to_string()).join("\n")
|
||
|
|
};
|
||
|
|
|
||
|
|
msg
|
||
|
|
}
|