diff --git a/ndc_lib/src/interpreter/mod.rs b/ndc_lib/src/interpreter/mod.rs index 86e10db5..ba016353 100644 --- a/ndc_lib/src/interpreter/mod.rs +++ b/ndc_lib/src/interpreter/mod.rs @@ -65,8 +65,12 @@ impl Interpreter { } } + let checkpoint = self.analyser.checkpoint(); for e in &mut expressions { - self.analyser.analyse(e)?; + if let Err(e) = self.analyser.analyse(e) { + self.analyser.restore(checkpoint); + return Err(e.into()); + } } //dbg!(&expressions); diff --git a/ndc_lib/src/interpreter/semantic/analyser.rs b/ndc_lib/src/interpreter/semantic/analyser.rs index c0249ca0..44f0ac91 100644 --- a/ndc_lib/src/interpreter/semantic/analyser.rs +++ b/ndc_lib/src/interpreter/semantic/analyser.rs @@ -14,6 +14,14 @@ impl Analyser { pub fn from_scope_tree(scope_tree: ScopeTree) -> Self { Self { scope_tree } } + + pub fn checkpoint(&self) -> ScopeTree { + self.scope_tree.clone() + } + + pub fn restore(&mut self, checkpoint: ScopeTree) { + self.scope_tree = checkpoint; + } } impl Analyser { @@ -559,7 +567,7 @@ fn extract_argument_arity(arguments: &ExpressionLocation) -> usize { values.len() } -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct ScopeTree { current_scope_idx: usize, global_scope: Scope, @@ -749,7 +757,7 @@ impl ScopeTree { } } -#[derive(Debug)] +#[derive(Debug, Clone)] struct Scope { parent_idx: Option, identifiers: Vec<(String, StaticType)>,