Fin de l'évaluation des blocs d'instructions avec gestion d'une pile d'environnements d'exécution

This commit is contained in:
2024-09-01 09:51:03 +02:00
parent 6ed3e4c68c
commit bba863687d
3 changed files with 7 additions and 5 deletions

View File

@@ -9,10 +9,10 @@ pub struct Environment {
} }
impl Environment { impl Environment {
pub fn new() -> Self { pub fn new( enclosing: Option<Box<Environment>> ) -> Self {
Environment { Environment {
values : HashMap::new(), values : HashMap::new(),
enclosing: None, enclosing: enclosing,
} }
} }

View File

@@ -14,7 +14,7 @@ pub struct Interpreter {
impl Interpreter { impl Interpreter {
pub fn new() -> Self { pub fn new() -> Self {
Interpreter { Interpreter {
environment: Environment::new() environment: Environment::new(None)
} }
} }
@@ -217,7 +217,9 @@ impl StatementVisitor<()> for Interpreter {
} }
fn visit_block_stmt(&mut self, b: &BlockStatement) { fn visit_block_stmt(&mut self, b: &BlockStatement) {
self.execute_block(&b.statements, &Environment::new() ); let mut new_env = Environment::new(Some(Box::new(self.environment.clone())));
self.execute_block(&b.statements, &new_env );
} }
} }

View File

@@ -95,4 +95,4 @@ fn run(src: String) -> i32 {
// https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust // https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust
// Pause : // Pause :
// http://www.craftinginterpreters.com/statements-and-state.html#block-syntax-and-semantics (visiteur du block statement dans l'interpréteur) // http://www.craftinginterpreters.com/control-flow.html