From 0aebd816654f0141e97e12b81d9b807f0f7258ab Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 3 Sep 2024 00:09:32 -0400 Subject: [PATCH] Fix context bug --- dust-lang/src/analyzer.rs | 4 ++++ dust-lang/src/context.rs | 11 +++++++++-- dust-lang/src/parser.rs | 9 ++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/dust-lang/src/analyzer.rs b/dust-lang/src/analyzer.rs index 23bcf43..ae71266 100644 --- a/dust-lang/src/analyzer.rs +++ b/dust-lang/src/analyzer.rs @@ -176,6 +176,8 @@ impl<'a> Analyzer<'a> { expression: &Expression, context: &Context, ) -> Result<(), ContextError> { + log::trace!("Analyzing expression {expression}"); + match expression { Expression::Block(block_expression) => { self.analyze_block(&block_expression.inner, context)?; @@ -343,6 +345,8 @@ impl<'a> Analyzer<'a> { Expression::Identifier(identifier) => { let context_data = context.get_data(&identifier.inner)?; + println!("{:?}", context_data); + if let Some(ContextData::Reserved) | None = context_data { self.errors.push(AnalysisError::UndefinedVariable { identifier: identifier.clone(), diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index cbb8024..5dc5d7a 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -42,7 +42,14 @@ impl Context { /// Returns the number of associated identifiers in the context. pub fn association_count(&self) -> Result { - Ok(self.associations.read()?.len()) + let own_count = self.associations.read()?.len(); + let ancestor_count = if let Some(parent) = &self.parent { + parent.association_count()? + } else { + 0 + }; + + Ok(own_count + ancestor_count) } /// Returns a boolean indicating whether the identifier is in the context. @@ -274,7 +281,7 @@ impl Context { if found { Ok(true) } else if let Some(parent) = &self.parent { - let found_in_ancestor = parent.update_last_position(identifier, position)?; + let found_in_ancestor = parent.update_position_if_found(identifier, position)?; if !found_in_ancestor { let mut associations = self.associations.write()?; diff --git a/dust-lang/src/parser.rs b/dust-lang/src/parser.rs index 460c6c4..85bd001 100644 --- a/dust-lang/src/parser.rs +++ b/dust-lang/src/parser.rs @@ -1018,9 +1018,16 @@ impl<'src> Parser<'src> { }); } + let context = context.create_child(); + + log::trace!( + "Creating new block context with {} associations", + context.association_count()? + ); + let mut ast = AbstractSyntaxTree { statements: VecDeque::new(), - context: context.create_child(), + context, }; loop {