dust/dust-lang/src/context.rs

335 lines
9.8 KiB
Rust
Raw Normal View History

//! Garbage-collecting context for variables.
use std::{
collections::HashMap,
2024-08-20 15:07:13 +00:00
fmt::{self, Display, Formatter},
sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard},
};
2024-08-10 00:52:13 +00:00
2024-08-20 06:25:22 +00:00
use crate::{ast::Span, Constructor, Identifier, StructType, Type, Value};
2024-08-10 00:52:13 +00:00
2024-08-20 15:07:13 +00:00
pub type Associations = HashMap<Identifier, (ContextData, Span)>;
/// Garbage-collecting context for variables.
#[derive(Debug, Clone)]
2024-08-10 00:52:13 +00:00
pub struct Context {
2024-08-20 15:07:13 +00:00
associations: Arc<RwLock<Associations>>,
2024-08-10 00:52:13 +00:00
}
impl Context {
pub fn new() -> Self {
2024-08-20 15:07:13 +00:00
Self::with_data(HashMap::new())
}
pub fn with_data(data: Associations) -> Self {
2024-08-10 00:52:13 +00:00
Self {
2024-08-20 15:07:13 +00:00
associations: Arc::new(RwLock::new(data)),
2024-08-10 00:52:13 +00:00
}
}
/// Creates a deep copy of another context.
2024-08-20 15:07:13 +00:00
pub fn with_data_from(other: &Self) -> Result<Self, ContextError> {
Ok(Self::with_data(other.associations.read()?.clone()))
2024-08-12 12:54:21 +00:00
}
2024-08-20 15:07:13 +00:00
/// Returns the number of associated identifiers in the context.
pub fn association_count(&self) -> Result<usize, ContextError> {
Ok(self.associations.read()?.len())
2024-08-10 00:52:13 +00:00
}
2024-08-20 15:07:13 +00:00
/// Returns a boolean indicating whether the identifier is in the context.
pub fn contains(&self, identifier: &Identifier) -> Result<bool, ContextError> {
Ok(self.associations.read()?.contains_key(identifier))
2024-08-10 00:52:13 +00:00
}
2024-08-20 15:07:13 +00:00
/// Returns the full ContextData and Span if the context contains the given identifier.
pub fn get(
&self,
identifier: &Identifier,
) -> Result<Option<(ContextData, Span)>, ContextError> {
let associations = self.associations.read()?;
Ok(associations.get(identifier).cloned())
2024-08-10 00:52:13 +00:00
}
2024-08-20 15:07:13 +00:00
/// Returns the type associated with the given identifier.
pub fn get_type(&self, identifier: &Identifier) -> Result<Option<Type>, ContextError> {
let r#type = match self.associations.read()?.get(identifier) {
Some((ContextData::VariableType(r#type), _)) => r#type.clone(),
Some((ContextData::VariableValue(value), _)) => value.r#type(),
2024-08-20 06:25:22 +00:00
Some((ContextData::ConstructorType(struct_type), _)) => {
2024-08-20 15:07:13 +00:00
Type::Struct(struct_type.clone())
2024-08-20 06:25:22 +00:00
}
2024-08-20 15:07:13 +00:00
_ => return Ok(None),
};
Ok(Some(r#type))
}
2024-08-20 15:07:13 +00:00
/// Returns the ContextData associated with the identifier.
pub fn get_data(&self, identifier: &Identifier) -> Result<Option<ContextData>, ContextError> {
match self.associations.read()?.get(identifier) {
Some((variable_data, _)) => Ok(Some(variable_data.clone())),
_ => Ok(None),
}
}
2024-08-20 15:07:13 +00:00
/// Returns the value associated with the identifier.
pub fn get_variable_value(
&self,
identifier: &Identifier,
) -> Result<Option<Value>, ContextError> {
match self.associations.read().unwrap().get(identifier) {
Some((ContextData::VariableValue(value), _)) => Ok(Some(value.clone())),
_ => Ok(None),
2024-08-20 06:25:22 +00:00
}
}
2024-08-20 15:07:13 +00:00
/// Returns the constructor associated with the identifier.
pub fn get_constructor(
&self,
identifier: &Identifier,
) -> Result<Option<Constructor>, ContextError> {
match self.associations.read().unwrap().get(identifier) {
Some((ContextData::Constructor(constructor), _)) => Ok(Some(constructor.clone())),
_ => Ok(None),
2024-08-10 00:52:13 +00:00
}
}
2024-08-20 15:07:13 +00:00
/// Associates an identifier with a variable type, with a position given for garbage collection.
pub fn set_variable_type(
&self,
identifier: Identifier,
r#type: Type,
position: Span,
) -> Result<(), ContextError> {
log::trace!("Setting {identifier} to type {type} at {position:?}");
2024-08-20 15:07:13 +00:00
self.associations
.write()?
2024-08-20 06:25:22 +00:00
.insert(identifier, (ContextData::VariableType(r#type), position));
2024-08-20 15:07:13 +00:00
Ok(())
2024-08-10 00:52:13 +00:00
}
2024-08-20 15:07:13 +00:00
/// Associates an identifier with a variable value.
pub fn set_variable_value(
&self,
identifier: Identifier,
value: Value,
) -> Result<(), ContextError> {
log::trace!("Setting {identifier} to value {value}");
2024-08-20 15:07:13 +00:00
let mut associations = self.associations.write()?;
2024-08-20 15:07:13 +00:00
let last_position = associations
.get(&identifier)
.map(|(_, last_position)| *last_position)
.unwrap_or_default();
2024-08-20 15:07:13 +00:00
associations.insert(
2024-08-20 06:25:22 +00:00
identifier,
(ContextData::VariableValue(value), last_position),
);
2024-08-20 15:07:13 +00:00
Ok(())
2024-08-20 06:25:22 +00:00
}
2024-08-20 15:07:13 +00:00
/// Associates an identifier with a constructor.
pub fn set_constructor(
&self,
identifier: Identifier,
constructor: Constructor,
) -> Result<(), ContextError> {
2024-08-20 07:28:13 +00:00
log::trace!("Setting {identifier} to constructor {constructor}");
2024-08-20 06:25:22 +00:00
2024-08-20 15:07:13 +00:00
let mut associations = self.associations.write()?;
2024-08-20 06:25:22 +00:00
2024-08-20 15:07:13 +00:00
let last_position = associations
2024-08-20 06:25:22 +00:00
.get(&identifier)
.map(|(_, last_position)| *last_position)
.unwrap_or_default();
2024-08-20 15:07:13 +00:00
associations.insert(
2024-08-20 06:25:22 +00:00
identifier,
(ContextData::Constructor(constructor), last_position),
);
2024-08-20 15:07:13 +00:00
Ok(())
2024-08-20 06:25:22 +00:00
}
2024-08-20 15:07:13 +00:00
/// Associates an identifier with a constructor type, with a position given for garbage
/// collection.
2024-08-20 06:25:22 +00:00
pub fn set_constructor_type(
&self,
identifier: Identifier,
struct_type: StructType,
position: Span,
2024-08-20 15:07:13 +00:00
) -> Result<(), ContextError> {
2024-08-20 07:28:13 +00:00
log::trace!("Setting {identifier} to constructor of type {struct_type}");
2024-08-20 06:25:22 +00:00
2024-08-20 15:07:13 +00:00
let mut variables = self.associations.write()?;
2024-08-20 06:25:22 +00:00
variables.insert(
identifier,
(ContextData::ConstructorType(struct_type), position),
);
2024-08-20 15:07:13 +00:00
Ok(())
2024-08-10 00:52:13 +00:00
}
/// Collects garbage up to the given position, removing all variables with lesser positions.
2024-08-20 15:07:13 +00:00
pub fn collect_garbage(&self, position: Span) -> Result<(), ContextError> {
log::trace!("Collecting garbage up to {position:?}");
2024-08-20 15:07:13 +00:00
let mut variables = self.associations.write()?;
variables.retain(|identifier, (_, last_used)| {
let should_drop = position.0 > last_used.0 && position.1 > last_used.1;
if should_drop {
log::trace!("Removing {identifier}");
}
!should_drop
});
variables.shrink_to_fit();
2024-08-20 15:07:13 +00:00
Ok(())
2024-08-10 00:52:13 +00:00
}
2024-08-10 08:45:30 +00:00
2024-08-20 15:07:13 +00:00
/// Updates an associated identifier's last known position, allowing it to live longer in the
/// program. Returns a boolean indicating whether the identifier.
pub fn update_last_position(
&self,
identifier: &Identifier,
position: Span,
) -> Result<bool, ContextError> {
if let Some((_, last_position)) = self.associations.write()?.get_mut(identifier) {
*last_position = position;
log::trace!("Updating {identifier}'s last position to {position:?}");
2024-08-10 08:45:30 +00:00
2024-08-20 15:07:13 +00:00
Ok(true)
2024-08-10 08:45:30 +00:00
} else {
2024-08-20 15:07:13 +00:00
Ok(false)
2024-08-10 08:45:30 +00:00
}
}
/// Recovers the context from a poisoned state by recovering data from an error.
///
/// This method is not used. The context's other methods do not return poison errors because
/// they are infallible.
2024-08-20 15:07:13 +00:00
pub fn recover_from_poison(&mut self, error: &ContextError) {
log::debug!("Context is recovering from poison error");
2024-08-20 15:07:13 +00:00
let ContextError::PoisonErrorRecovered(recovered) = error;
let mut new_associations = HashMap::new();
2024-08-20 15:07:13 +00:00
for (identifier, (context_data, position)) in recovered.as_ref() {
new_associations.insert(identifier.clone(), (context_data.clone(), *position));
}
2024-08-20 15:07:13 +00:00
self.associations = Arc::new(RwLock::new(new_associations));
}
2024-08-10 00:52:13 +00:00
}
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
2024-08-20 06:25:22 +00:00
pub enum ContextData {
Constructor(Constructor),
ConstructorType(StructType),
VariableValue(Value),
VariableType(Type),
2024-08-10 00:52:13 +00:00
}
2024-08-20 15:07:13 +00:00
#[derive(Debug, Clone)]
pub enum ContextError {
PoisonErrorRecovered(Arc<Associations>),
}
impl From<PoisonError<RwLockWriteGuard<'_, Associations>>> for ContextError {
fn from(error: PoisonError<RwLockWriteGuard<'_, Associations>>) -> Self {
let associations = error.into_inner().clone();
Self::PoisonErrorRecovered(Arc::new(associations))
}
}
impl From<PoisonError<RwLockReadGuard<'_, Associations>>> for ContextError {
fn from(error: PoisonError<RwLockReadGuard<'_, Associations>>) -> Self {
let associations = error.into_inner().clone();
Self::PoisonErrorRecovered(Arc::new(associations))
}
}
impl PartialEq for ContextError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::PoisonErrorRecovered(left), Self::PoisonErrorRecovered(right)) => {
Arc::ptr_eq(left, right)
}
}
}
}
impl Display for ContextError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::PoisonErrorRecovered(associations) => {
write!(
f,
"Context poisoned with {} associations recovered",
associations.len()
)
}
}
}
}
#[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() {
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
";
2024-08-12 12:54:21 +00:00
let context = Context::new();
2024-08-12 02:47:52 +00:00
2024-08-12 12:54:21 +00:00
run_with_context(source, context.clone()).unwrap();
2024-08-12 02:47:52 +00:00
2024-08-20 15:07:13 +00:00
assert_eq!(context.association_count().unwrap(), 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 = "
y = 1
z = 0
2024-08-12 02:47:52 +00:00
while z < 10 {
z = z + y
2024-08-12 02:47:52 +00:00
}
";
2024-08-12 12:54:21 +00:00
let context = Context::new();
2024-08-12 02:47:52 +00:00
2024-08-12 12:54:21 +00:00
run_with_context(source, context.clone()).unwrap();
2024-08-12 02:47:52 +00:00
2024-08-20 15:07:13 +00:00
assert_eq!(context.association_count().unwrap(), 0);
2024-08-12 02:47:52 +00:00
}
}