From bba863687df342005e229d1e839d33fae2de5410 Mon Sep 17 00:00:00 2001 From: Emmanuel BERNAT Date: Sun, 1 Sep 2024 09:51:03 +0200 Subject: [PATCH] =?UTF-8?q?Fin=20de=20l'=C3=A9valuation=20des=20blocs=20d'?= =?UTF-8?q?instructions=20avec=20gestion=20d'une=20pile=20d'environnements?= =?UTF-8?q?=20d'ex=C3=A9cution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/environment.rs | 4 ++-- src/interpreter.rs | 6 ++++-- src/main.rs | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/environment.rs b/src/environment.rs index e37805d..4f0e66f 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -9,10 +9,10 @@ pub struct Environment { } impl Environment { - pub fn new() -> Self { + pub fn new( enclosing: Option> ) -> Self { Environment { values : HashMap::new(), - enclosing: None, + enclosing: enclosing, } } diff --git a/src/interpreter.rs b/src/interpreter.rs index f188de8..684fc9b 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -14,7 +14,7 @@ pub struct Interpreter { impl Interpreter { pub fn new() -> Self { Interpreter { - environment: Environment::new() + environment: Environment::new(None) } } @@ -217,7 +217,9 @@ impl StatementVisitor<()> for Interpreter { } 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 ); } } diff --git a/src/main.rs b/src/main.rs index 0a3cae2..27d3792 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,4 +95,4 @@ fn run(src: String) -> i32 { // https://github.com/munificent/craftinginterpreters/wiki/Lox-implementations#rust // 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