Add test; Make garbage collection work
This commit is contained in:
parent
6c699ec900
commit
dab3d2de8e
@ -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)? {
|
||||
|
@ -180,6 +180,8 @@ impl Context {
|
||||
/// representing whether or not the variable was found.
|
||||
pub fn add_allowance(&self, identifier: &Identifier) -> Result<bool, RwLockError> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,9 @@ impl UsageCounter {
|
||||
})))
|
||||
}
|
||||
|
||||
pub fn allowances(&self) -> Result<usize, RwLockError> {
|
||||
Ok(self.0.read()?.allowances)
|
||||
}
|
||||
|
||||
pub fn runtime_uses(&self) -> Result<usize, RwLockError> {
|
||||
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> {
|
||||
|
Loading…
Reference in New Issue
Block a user