1
0

Add test; Clean up context

This commit is contained in:
Jeff 2024-08-12 10:29:06 -04:00
parent 0ba54e9717
commit 2a0737fd45
2 changed files with 7 additions and 13 deletions

View File

@ -10,23 +10,18 @@ use crate::{Identifier, Span, Type, Value};
#[derive(Debug, Clone)]
pub struct Context {
variables: Arc<RwLock<HashMap<Identifier, (VariableData, Span)>>>,
is_garbage_collected_to: Arc<RwLock<usize>>,
}
impl Context {
pub fn new() -> Self {
Self {
variables: Arc::new(RwLock::new(HashMap::new())),
is_garbage_collected_to: Arc::new(RwLock::new(0)),
}
}
pub fn with_variables_from(other: &Self) -> Self {
Self {
variables: Arc::new(RwLock::new(other.variables.read().unwrap().clone())),
is_garbage_collected_to: Arc::new(RwLock::new(
*other.is_garbage_collected_to.read().unwrap(),
)),
}
}
@ -88,12 +83,6 @@ impl Context {
pub fn collect_garbage(&self, current_position: usize) {
log::trace!("Collecting garbage up to {current_position}");
let mut is_garbage_collected_to = self.is_garbage_collected_to.write().unwrap();
if current_position < *is_garbage_collected_to {
return;
}
let mut variables = self.variables.write().unwrap();
variables.retain(|identifier, (_, last_used)| {
@ -106,8 +95,6 @@ impl Context {
!should_drop
});
variables.shrink_to_fit();
*is_garbage_collected_to = current_position;
}
pub fn update_last_position(&self, identifier: &Identifier, position: Span) -> bool {

View File

@ -693,6 +693,13 @@ impl Display for VmError {
mod tests {
use super::*;
#[test]
fn range() {
let input = "1..5";
assert_eq!(run(input), Ok(Some(Value::range(1..5))));
}
#[test]
fn negate_expression() {
let input = "x = -42; -x";