From 689b08a0f077fac40978a70cfc37b65a6b335698 Mon Sep 17 00:00:00 2001 From: nulluser0 Date: Tue, 18 Jun 2024 22:36:02 +1000 Subject: [PATCH] error handing --- src/backend/eval/expressions.rs | 2 + src/backend/eval/statements.rs | 2 + src/errors.rs | 68 ++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/src/backend/eval/expressions.rs b/src/backend/eval/expressions.rs index 4af8fe7..dcdcf89 100644 --- a/src/backend/eval/expressions.rs +++ b/src/backend/eval/expressions.rs @@ -1,3 +1,5 @@ +// Expressions + use std::{cell::RefCell, collections::HashMap, rc::Rc}; use crate::{ diff --git a/src/backend/eval/statements.rs b/src/backend/eval/statements.rs index ddfe4a0..ba1e1d6 100644 --- a/src/backend/eval/statements.rs +++ b/src/backend/eval/statements.rs @@ -1,3 +1,5 @@ +// Statements + use std::{cell::RefCell, rc::Rc}; use crate::{ diff --git a/src/errors.rs b/src/errors.rs index de84673..629466e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -13,45 +13,42 @@ pub enum Error { // // #[error("Type Error: {0}")] // TODO: TypeError(TypeError), - - // #[error("Runtime Error: {0}")] - // TODO: RuntimeError(RuntimeError), + // + #[error("Runtime Error: {0}")] + RuntimeError(RuntimeError), } // Lexer-specific Errors #[derive(Error, Debug)] pub enum LexerError { - #[error("Unrecognized character '{character}' at {line}:{col}")] + #[error("at position {line}:{col}: Unrecognized character '{character}'")] UnrecognizedCharacter { character: char, line: usize, col: usize, }, - #[error("Invalid number format: {invalid_string} at {line}:{col}")] + #[error("at position {line}:{col}: Invalid number format: {invalid_string}")] InvalidNumberFormat { invalid_string: String, line: usize, col: usize, }, - #[error("Unterminated string literal at string beginning at {line}:{col}")] + #[error("at position {line}:{col}: Unterminated string literal at string beginning")] UnterminatedStringLiteral { line: usize, col: usize }, - #[error("Unexpected end of file at {line}:{col}")] + #[error("at position {line}:{col}: Unexpected end of file")] UnexpectedEOF { line: usize, col: usize }, - #[error("Internal error: {0}")] + #[error("Internal error. Please report this!: {0}")] InternalError(String), } // Parser-specific Errors #[derive(Error, Debug)] pub enum ParserError { - #[error("Lexer Error: {0}")] - LexerErrorAsParseError(LexerError), - - #[error("Parser Error: at position {position}: {message}. Expected {expected:?}, but found {found:?}")] + #[error("at position {position}: {message}. Expected {expected:?}, but found {found:?}")] UnexpectedToken { expected: Token, found: Token, @@ -59,37 +56,62 @@ pub enum ParserError { message: String, }, - #[error("Parser Error: `async` must be followed by `fn`")] + #[error("`async` must be followed by `fn`")] MissingAsyncFn, - #[error("Parser Error: at position {0}: `break` found outside of loop context")] + #[error("at position {0}: `break` found outside of loop context")] BreakOutsideLoop(usize), - #[error("Parser Error: at position {0}: `return` found outside of function context")] + #[error("at position {0}: `return` found outside of function context")] ReturnOutsideFunction(usize), - #[error("Parser Error: at position {0}: Missing function identifier")] + #[error("at position {0}: Missing function identifier")] MissingFunctionIdentifier(usize), - #[error("Parser Error: at position {0}: Invalid function parameter")] + #[error("at position {0}: Invalid function parameter")] InvalidFunctionParameter(usize), - #[error("Parser Error: at position {0}: Cannot use `mut` with `const`")] + #[error("at position {0}: Cannot use `mut` with `const`")] MutConstConflict(usize), - #[error("Parser Error: at position {0}: Missing identifier")] + #[error("at position {0}: Missing identifier")] MissingIdentifier(usize), - #[error("Parser Error: at position {0}: `const` must have an assigned value")] + #[error("at position {0}: `const` must have an assigned value")] ConstWithoutValue(usize), - #[error("Parser Error: at position {0}: Object literal key expected")] + #[error("at position {0}: Object literal key expected")] ObjectLiteralKeyExpected(usize), - #[error("Parser Error: at position {0}: Invalid operator {1:?}")] + #[error("at position {0}: Invalid operator {1:?}")] InvalidOperator(usize, Token), - #[error("Parser Error: at position {0}: Invalid property access using dot operator {1:?}")] + #[error("at position {0}: Invalid property access using dot operator {1:?}")] InvalidDotProperty(usize, Expr), // Add other error variants here if needed } + +// Interpreter-specific Errors +#[derive(Error, Debug)] +pub enum RuntimeError { + #[error("Undefined variable '{0}'")] + UndefinedVariable(String), + + #[error("Type mismatch: expected {expected}, found {found}")] + TypeError { expected: String, found: String }, + + #[error("Division by zero")] + DivisionByZero, + + #[error("Function '{0}' not callable")] + NotCallable(String), + + #[error("Index out of bounds: {0}")] + IndexOutOfBounds(usize), + + #[error("Property '{0}' not found")] + PropertyNotFound(String), + + #[error("Other runtime error: {0}")] + RuntimeError(String), +}