Refactor environment to use stack-based scoping
The commit rewrites the environment to use a stack of hash maps for managing variable scopes. This replaces the old parent-reference approach with a simpler and more efficient stack-based model. Key changes: - Rename Environment to EnvironmentStack - Store scopes in a Vec of HashMaps - Add push/pop scope operations for block handling - Update interpreter to properly manage scope lifetimes - Clean up error handling with helper functions
This commit is contained in:
+19
-16
@@ -402,45 +402,48 @@ impl std::error::Error for LoxError {}
|
||||
pub type LoxResult<T> = Result<T, LoxError>;
|
||||
|
||||
/// Helper function to create a lexical error
|
||||
pub fn lexical_error(source_slice: SourceSlice, message: impl Into<String>) -> LoxError {
|
||||
LoxError::LexicalError {
|
||||
pub fn lexical_error<T>(source_slice: SourceSlice, message: impl Into<String>) -> LoxResult<T> {
|
||||
Err(LoxError::LexicalError {
|
||||
source_slice,
|
||||
message: message.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper function to create a parse error
|
||||
pub fn parse_error(source_slice: SourceSlice, message: impl Into<String>) -> LoxError {
|
||||
LoxError::ParseError {
|
||||
pub fn parse_error<T>(source_slice: SourceSlice, message: impl Into<String>) -> LoxResult<T> {
|
||||
Err(LoxError::ParseError {
|
||||
source_slice,
|
||||
message: message.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper function to create a runtime error
|
||||
pub fn runtime_error(source_slice: SourceSlice, message: impl Into<String>) -> LoxError {
|
||||
LoxError::RuntimeError {
|
||||
#[allow(dead_code)]
|
||||
pub fn runtime_error<T>(source_slice: SourceSlice, message: impl Into<String>) -> LoxResult<T> {
|
||||
Err(LoxError::RuntimeError {
|
||||
source_slice,
|
||||
message: message.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper function to create a type mismatch error
|
||||
pub fn type_mismatch_error(
|
||||
#[allow(dead_code)]
|
||||
pub fn type_mismatch_error<T>(
|
||||
source_slice: SourceSlice,
|
||||
expected: impl Into<String>,
|
||||
found: impl Into<String>,
|
||||
) -> LoxError {
|
||||
LoxError::TypeMismatch {
|
||||
) -> LoxResult<T> {
|
||||
Err(LoxError::TypeMismatch {
|
||||
source_slice,
|
||||
expected: expected.into(),
|
||||
found: found.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper function to create an IO error
|
||||
pub fn io_error(message: impl Into<String>) -> LoxError {
|
||||
LoxError::IoError {
|
||||
#[allow(dead_code)]
|
||||
pub fn io_error<T>(message: impl Into<String>) -> LoxResult<T> {
|
||||
Err(LoxError::IoError {
|
||||
message: message.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user