updated a bunch of stuff

This commit is contained in:
Moritz Gmeiner 2024-09-01 19:16:30 +02:00
commit 67bb5fe8fd
24 changed files with 683 additions and 702 deletions

View file

@ -10,35 +10,43 @@ use crate::{LoxClass, ResolverError};
#[derive(Error, Debug)]
pub enum RuntimeError {
#[error("Unary operator {op} had invalid argument {arg}")]
#[error("RuntimeError: Unary operator {op} had invalid argument {arg}")]
UnaryOpInvalidArgument { op: UnaryOp, arg: Value },
#[error("Binary operator {op} had invalid arguments {left} and {right}")]
BinaryOpInvalidArguments { left: Value, op: BinaryOp, right: Value },
#[error("Division by zero")]
#[error("RuntimeError: Binary operator {op} had invalid arguments {left} and {right}")]
BinaryOpInvalidArguments {
left: Value,
op: BinaryOp,
right: Value,
},
#[error("RuntimeError: Division by zero")]
DivisionByZero,
#[error("Local {name} is not defined")]
#[error("RuntimeError: Local {name} is not defined")]
NameNotDefined { name: String },
#[error("Global {name} is not defined")]
#[error("RuntimeError: Global {name} is not defined")]
GlobalNotDefined { name: String },
#[error("{callee} is not callable")]
#[error("RuntimeError: {callee} is not callable")]
NotCallable { callee: Value },
#[error("{name}() takes {arity} args, but {given} were given.")]
WrongArity { name: String, arity: usize, given: usize },
#[error("Extern function call to {name} failed: {msg}")]
#[error("RuntimeError: {name}() takes {arity} args, but {given} were given.")]
WrongArity {
name: String,
arity: usize,
given: usize,
},
#[error("RuntimeError: Extern function call to {name} failed: {msg}")]
ExtFunCallFailed { name: String, msg: String },
#[error("Uncaught break statement")]
#[error("RuntimeError: Uncaught break statement")]
Break,
#[error("Uncaught return statement")]
#[error("RuntimeError: Uncaught return statement")]
Return { value: Value },
#[error("Exit with exit code {exit_code}")]
#[error("RuntimeError: Exit with exit code {exit_code}")]
Exit { exit_code: i32 },
#[error("Only objects have attributes")]
#[error("RuntimeError: Only objects have attributes")]
InvalidGetTarget,
#[error("Only objects have attributes")]
#[error("RuntimeError: Only objects have attributes")]
InvalidSetTarget,
#[error("Class {0} has no property {name}", class.name())]
UndefinedAttribute { class: Rc<LoxClass>, name: String },
#[error("{value} is not a valid superclass")]
#[error("RuntimeError: Class {0} has no property {name}", class.name())]
UndefinedAttribute { class: Rc<LoxClass>, name: Box<str> },
#[error("RuntimeError: {value} is not a valid superclass")]
InvalidSuperclass { value: Value },
}
@ -57,7 +65,7 @@ pub enum LoxError {
Exit { exit_code: i32 },
}
fn format_multiple_errors(errs: &Vec<impl std::error::Error>) -> String {
fn format_multiple_errors(errs: &[impl std::error::Error]) -> String {
let msg = if errs.len() == 1 {
errs[0].to_string()
} else {
@ -81,7 +89,9 @@ impl From<Vec<ParserError>> for LoxError {
impl From<ResolverError> for LoxError {
fn from(resolver_err: ResolverError) -> Self {
LoxError::ResolverError { inner: resolver_err }
LoxError::ResolverError {
inner: resolver_err,
}
}
}