Skip to content

Commit

Permalink
error handing
Browse files Browse the repository at this point in the history
  • Loading branch information
nulluser0 committed Jun 18, 2024
1 parent 3ce890f commit 689b08a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/backend/eval/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Expressions

use std::{cell::RefCell, collections::HashMap, rc::Rc};

use crate::{
Expand Down
2 changes: 2 additions & 0 deletions src/backend/eval/statements.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Statements

use std::{cell::RefCell, rc::Rc};

use crate::{
Expand Down
68 changes: 45 additions & 23 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,83 +13,105 @@ 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,
position: usize,
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),
}

0 comments on commit 689b08a

Please sign in to comment.