From dab3d2de8ec13518f594a4a5248e7c7c729156a5 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 18 Feb 2024 00:32:03 -0500 Subject: [PATCH] Add test; Make garbage collection work --- src/abstract_tree/assignment.rs | 12 +++--------- src/context/mod.rs | 33 ++++++++++++++++++++++++++++++++- src/context/usage_counter.rs | 9 +++------ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/abstract_tree/assignment.rs b/src/abstract_tree/assignment.rs index d6fa062..963eaf6 100644 --- a/src/abstract_tree/assignment.rs +++ b/src/abstract_tree/assignment.rs @@ -63,8 +63,6 @@ impl AbstractTree for Assignment { context.set_type(self.identifier.clone(), r#type)?; } - self.identifier.validate(source, context)?; - if let Some(type_specification) = &self.type_specification { match self.operator { AssignmentOperator::Equal => { @@ -135,13 +133,9 @@ impl AbstractTree for Assignment { let new_value = match self.operator { AssignmentOperator::PlusEqual => { - if let Some(left) = context.get_value(&self.identifier)? { - left.add(right, self.syntax_position)? - } else { - return Err(RuntimeError::ValidationFailure( - ValidationError::VariableIdentifierNotFound(self.identifier.clone()), - )); - } + let left = self.identifier.run(source, context)?; + + left.add(right, self.syntax_position)? } AssignmentOperator::MinusEqual => { if let Some(left) = context.get_value(&self.identifier)? { diff --git a/src/context/mod.rs b/src/context/mod.rs index 11a92b4..5cb618c 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -180,6 +180,8 @@ impl Context { /// representing whether or not the variable was found. pub fn add_allowance(&self, identifier: &Identifier) -> Result { if let Some((_value_data, counter)) = self.inner.read()?.get(identifier) { + log::debug!("Adding allowance for {identifier}."); + counter.add_allowance()?; Ok(true) @@ -207,7 +209,13 @@ impl Context { return Ok(None); }; - if counter.allowances() == counter.runtime_uses() { + counter.add_runtime_use()?; + + log::debug!("Adding runtime use for {identifier}."); + + let (allowances, runtime_uses) = counter.get_counts()?; + + if allowances == runtime_uses { self.unset(identifier)?; } @@ -371,3 +379,26 @@ impl Display for Context { writeln!(f, "}}") } } + +#[cfg(test)] +mod tests { + use crate::*; + + #[test] + fn drops_variables() { + let context = Context::new(); + + interpret_with_context( + " + x = 1 + y = 2 + + z = x + y + ", + context.clone(), + ) + .unwrap(); + + assert_eq!(context.inner.read().unwrap().len(), 1); + } +} diff --git a/src/context/usage_counter.rs b/src/context/usage_counter.rs index 624eb29..53b268d 100644 --- a/src/context/usage_counter.rs +++ b/src/context/usage_counter.rs @@ -16,12 +16,9 @@ impl UsageCounter { }))) } - pub fn allowances(&self) -> Result { - Ok(self.0.read()?.allowances) - } - - pub fn runtime_uses(&self) -> Result { - Ok(self.0.read()?.runtime_uses) + pub fn get_counts(&self) -> Result<(usize, usize), RwLockError> { + let inner = self.0.read()?; + Ok((inner.allowances, inner.runtime_uses)) } pub fn add_allowance(&self) -> Result<(), RwLockError> {