2024-08-10 01:12:36 +00:00
|
|
|
//! Garbage-collecting context for variables.
|
2024-08-12 08:10:07 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2024-08-10 00:52:13 +00:00
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
use crate::{Identifier, Span, Type, Value};
|
2024-08-10 00:52:13 +00:00
|
|
|
|
2024-08-10 01:12:36 +00:00
|
|
|
/// Garbage-collecting context for variables.
|
|
|
|
#[derive(Debug, Clone)]
|
2024-08-10 00:52:13 +00:00
|
|
|
pub struct Context {
|
2024-08-12 08:10:07 +00:00
|
|
|
variables: Arc<RwLock<HashMap<Identifier, (VariableData, Span)>>>,
|
|
|
|
is_garbage_collected_to: Arc<RwLock<usize>>,
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2024-08-12 08:10:07 +00:00
|
|
|
variables: Arc::new(RwLock::new(HashMap::new())),
|
|
|
|
is_garbage_collected_to: Arc::new(RwLock::new(0)),
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn variable_count(&self) -> usize {
|
|
|
|
self.variables.read().unwrap().len()
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn contains(&self, identifier: &Identifier) -> bool {
|
|
|
|
self.variables.read().unwrap().contains_key(identifier)
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn get(&self, identifier: &Identifier) -> Option<(VariableData, Span)> {
|
|
|
|
self.variables.read().unwrap().get(identifier).cloned()
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn get_type(&self, identifier: &Identifier) -> Option<Type> {
|
|
|
|
match self.variables.read().unwrap().get(identifier) {
|
|
|
|
Some((VariableData::Type(r#type), _)) => Some(r#type.clone()),
|
2024-08-12 02:41:40 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn get_variable_data(&self, identifier: &Identifier) -> Option<VariableData> {
|
|
|
|
match self.variables.read().unwrap().get(identifier) {
|
|
|
|
Some((variable_data, _)) => Some(variable_data.clone()),
|
2024-08-12 02:41:40 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn get_value(&self, identifier: &Identifier) -> Option<Value> {
|
|
|
|
match self.variables.read().unwrap().get(identifier) {
|
|
|
|
Some((VariableData::Value(value), _)) => Some(value.clone()),
|
2024-08-10 00:52:13 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn set_type(&mut self, identifier: Identifier, r#type: Type, position: Span) {
|
|
|
|
log::trace!("Setting {identifier} to type {type} at {position:?}");
|
2024-08-12 02:41:40 +00:00
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
self.variables
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.insert(identifier, (VariableData::Type(r#type), position));
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_value(&mut self, identifier: Identifier, value: Value) {
|
2024-08-12 08:10:07 +00:00
|
|
|
log::trace!("Setting {identifier} to value {value}");
|
2024-08-12 02:41:40 +00:00
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
let mut variables = self.variables.write().unwrap();
|
|
|
|
|
|
|
|
let last_position = variables
|
|
|
|
.get(&identifier)
|
|
|
|
.map(|(_, last_position)| *last_position)
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
variables.insert(identifier, (VariableData::Value(value), last_position));
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn collect_garbage(&mut 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;
|
2024-08-12 02:41:40 +00:00
|
|
|
}
|
2024-08-12 08:10:07 +00:00
|
|
|
|
|
|
|
let mut variables = self.variables.write().unwrap();
|
|
|
|
|
|
|
|
variables.retain(|identifier, (_, last_used)| {
|
|
|
|
let should_drop = current_position >= last_used.1;
|
|
|
|
|
|
|
|
if should_drop {
|
|
|
|
log::trace!("Removing {identifier}");
|
|
|
|
}
|
|
|
|
|
|
|
|
!should_drop
|
|
|
|
});
|
|
|
|
variables.shrink_to_fit();
|
|
|
|
|
|
|
|
*is_garbage_collected_to = current_position;
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
2024-08-10 08:45:30 +00:00
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
pub fn update_last_position(&mut self, identifier: &Identifier, position: Span) -> bool {
|
|
|
|
if let Some((_, last_position)) = self.variables.write().unwrap().get_mut(identifier) {
|
|
|
|
*last_position = position;
|
|
|
|
|
|
|
|
log::trace!("Updating {identifier}'s last position to {position:?}");
|
2024-08-10 08:45:30 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 00:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Context {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-10 01:12:36 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2024-08-10 00:52:13 +00:00
|
|
|
pub enum VariableData {
|
|
|
|
Value(Value),
|
|
|
|
Type(Type),
|
|
|
|
}
|
|
|
|
|
2024-08-12 02:41:40 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::vm::run_with_context;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2024-08-12 02:47:52 +00:00
|
|
|
#[test]
|
2024-08-12 09:44:05 +00:00
|
|
|
fn context_removes_variables() {
|
2024-08-12 08:10:07 +00:00
|
|
|
env_logger::builder().is_test(true).try_init().unwrap();
|
|
|
|
|
2024-08-12 02:47:52 +00:00
|
|
|
let source = "
|
|
|
|
x = 5
|
|
|
|
y = 10
|
|
|
|
z = x + y
|
|
|
|
z
|
|
|
|
";
|
|
|
|
let mut context = Context::new();
|
|
|
|
|
|
|
|
run_with_context(source, &mut context).unwrap();
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
assert_eq!(context.variable_count(), 0);
|
2024-08-12 02:47:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-08-12 09:44:05 +00:00
|
|
|
fn garbage_collector_does_not_break_loops() {
|
2024-08-12 02:47:52 +00:00
|
|
|
let source = "
|
2024-08-12 08:10:07 +00:00
|
|
|
y = 1
|
|
|
|
z = 0
|
2024-08-12 02:47:52 +00:00
|
|
|
while z < 10 {
|
2024-08-12 08:10:07 +00:00
|
|
|
z = z + y
|
2024-08-12 02:47:52 +00:00
|
|
|
}
|
|
|
|
";
|
|
|
|
let mut context = Context::new();
|
|
|
|
|
|
|
|
run_with_context(source, &mut context).unwrap();
|
|
|
|
|
2024-08-12 08:10:07 +00:00
|
|
|
assert_eq!(context.variable_count(), 0);
|
2024-08-12 02:47:52 +00:00
|
|
|
}
|
2024-08-12 02:41:40 +00:00
|
|
|
}
|