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:
Giulio Agostini
2025-10-04 21:05:00 +02:00
parent 41253e932a
commit 827349cbad
11 changed files with 165 additions and 243 deletions
+3 -25
View File
@@ -3,10 +3,7 @@
//! This module provides elegant and functional printing capabilities for AST objects,
//! inspired by Rust's debug formatting but optimized for readability and analysis.
use crate::frontend::{
ast::{AstNode, AstNodeKind, Expr, Stmt},
source_registry::SourceSlice,
};
use crate::frontend::ast::{AstNode, AstNodeKind, Expr, Stmt};
use std::fmt::{self, Write};
/// Configuration for pretty printing output
@@ -39,6 +36,7 @@ impl Default for PrettyConfig {
}
}
#[allow(dead_code)]
impl PrettyConfig {
/// Create a compact configuration
pub fn compact() -> Self {
@@ -471,6 +469,7 @@ impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrint for A
}
// Extension trait for convenience methods
#[allow(dead_code)]
pub trait PrettyPrintExt {
fn pretty(&self) -> String;
fn pretty_compact(&self) -> String;
@@ -531,24 +530,3 @@ impl<T: AstNodeKind + fmt::Debug + fmt::Display + PrettyPrint> PrettyPrintExt fo
pretty_print_with_config(self, config)
}
}
/// Convenience functions for quick pretty printing
pub fn pretty_expr(expr: &Expr) -> String {
pretty_print(expr)
}
pub fn pretty_stmt(stmt: &Stmt) -> String {
pretty_print(stmt)
}
pub fn pretty_expr_compact(expr: &Expr) -> String {
pretty_print_with_config(expr, &PrettyConfig::compact())
}
pub fn pretty_stmt_compact(stmt: &Stmt) -> String {
pretty_print_with_config(stmt, &PrettyConfig::compact())
}
pub fn pretty_tree<T: PrettyPrint>(item: &T) -> String {
pretty_print_with_config(item, &PrettyConfig::tree())
}