Fin de l'évaluation des blocs d'instructions avec gestion d'une pile d'environnements d'exécution
This commit is contained in:
@@ -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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user