1
0

Fix context bug

This commit is contained in:
Jeff 2024-09-03 00:09:32 -04:00
parent 0edb42836d
commit 0aebd81665
3 changed files with 21 additions and 3 deletions

View File

@ -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(),

View File

@ -42,7 +42,14 @@ impl Context {
/// Returns the number of associated identifiers in the context.
pub fn association_count(&self) -> Result<usize, ContextError> {
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()?;

View File

@ -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 {