#![deny(clippy::all)]
#![warn(clippy::pedantic)]
use std::{convert::From, error, fmt};
mod compiler;
mod lox_core;
pub mod lox_std;
mod vm;
pub use lox_core::{NativeFun, Value};
pub use vm::VM;
use {
compiler::{CompileError, CompileErrorType, Compiler, Upvalue},
lox_core::{Chunk, Class, Fun, Gc, Instance, Op, UpvalueRef},
vm::RuntimeError,
};
#[derive(Debug)]
pub struct Error {
inner: Box<dyn error::Error>,
category: ErrorCategory,
line: usize,
}
impl Error {
pub fn category(&self) -> ErrorCategory {
self.category
}
pub fn line(&self) -> usize {
self.line
}
fn from_runtime_error(err: RuntimeError, line: Option<usize>) -> Self {
Self {
inner: Box::new(err),
category: ErrorCategory::Runtime,
line: line.unwrap_or(1),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&*self.inner)
}
}
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum ErrorCategory {
Compilation,
Runtime,
}
impl From<CompileError> for Error {
fn from(inner: CompileError) -> Self {
Self {
line: inner.line,
inner: Box::new(inner),
category: ErrorCategory::Compilation,
}
}
}