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
+4 -5
View File
@@ -6,19 +6,19 @@ mod result;
use crate::{
backend::{
environment::Environment,
environment::EnvironmentStack,
interpreter::{EvaluateInterpreter, Interpreter},
},
frontend::{
lexer::Lexer,
parser::Parser,
source_registry::{SourceFile, SourceId, SourcePosition, SourceRegistry, SourceSlice},
source_registry::{SourceId, SourceRegistry, SourceSlice},
},
prompt::prompt_lines,
result::{LoxError, LoxResult},
};
use std::env;
use std::fs;
use std::{env, error::Error};
fn main() -> LoxResult<()> {
let args: Vec<String> = env::args().collect();
@@ -83,8 +83,7 @@ impl LoxInterpreter {
Ok(asts)
})
.and_then(|stmts| {
let mut env = Environment::new();
let mut interpreter = Interpreter::new(&mut env);
let mut interpreter = Interpreter::new();
let mut result = None;
for (index, stmt) in stmts.iter().enumerate() {
println!("executing stmt {}: {:?}", index, stmt);