1
0

Add test; Make garbage collection work

This commit is contained in:
Jeff 2024-02-18 00:32:03 -05:00
parent 6c699ec900
commit dab3d2de8e
3 changed files with 38 additions and 16 deletions

View File

@ -63,8 +63,6 @@ impl AbstractTree for Assignment {
context.set_type(self.identifier.clone(), r#type)?; context.set_type(self.identifier.clone(), r#type)?;
} }
self.identifier.validate(source, context)?;
if let Some(type_specification) = &self.type_specification { if let Some(type_specification) = &self.type_specification {
match self.operator { match self.operator {
AssignmentOperator::Equal => { AssignmentOperator::Equal => {
@ -135,13 +133,9 @@ impl AbstractTree for Assignment {
let new_value = match self.operator { let new_value = match self.operator {
AssignmentOperator::PlusEqual => { AssignmentOperator::PlusEqual => {
if let Some(left) = context.get_value(&self.identifier)? { let left = self.identifier.run(source, context)?;
left.add(right, self.syntax_position)?
} else { left.add(right, self.syntax_position)?
return Err(RuntimeError::ValidationFailure(
ValidationError::VariableIdentifierNotFound(self.identifier.clone()),
));
}
} }
AssignmentOperator::MinusEqual => { AssignmentOperator::MinusEqual => {
if let Some(left) = context.get_value(&self.identifier)? { if let Some(left) = context.get_value(&self.identifier)? {

View File

@ -180,6 +180,8 @@ impl Context {
/// representing whether or not the variable was found. /// representing whether or not the variable was found.
pub fn add_allowance(&self, identifier: &Identifier) -> Result<bool, RwLockError> { pub fn add_allowance(&self, identifier: &Identifier) -> Result<bool, RwLockError> {
if let Some((_value_data, counter)) = self.inner.read()?.get(identifier) { if let Some((_value_data, counter)) = self.inner.read()?.get(identifier) {
log::debug!("Adding allowance for {identifier}.");
counter.add_allowance()?; counter.add_allowance()?;
Ok(true) Ok(true)
@ -207,7 +209,13 @@ impl Context {
return Ok(None); 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)?; self.unset(identifier)?;
} }
@ -371,3 +379,26 @@ impl Display for Context {
writeln!(f, "}}") 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);
}
}

View File

@ -16,12 +16,9 @@ impl UsageCounter {
}))) })))
} }
pub fn allowances(&self) -> Result<usize, RwLockError> { pub fn get_counts(&self) -> Result<(usize, usize), RwLockError> {
Ok(self.0.read()?.allowances) let inner = self.0.read()?;
} Ok((inner.allowances, inner.runtime_uses))
pub fn runtime_uses(&self) -> Result<usize, RwLockError> {
Ok(self.0.read()?.runtime_uses)
} }
pub fn add_allowance(&self) -> Result<(), RwLockError> { pub fn add_allowance(&self) -> Result<(), RwLockError> {