From 2a0e4c9b789240e3dee82d20c758fc845328938f Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 2 Sep 2024 03:58:53 -0400 Subject: [PATCH] Clean up context garbage collection --- dust-lang/src/analyzer.rs | 3 --- dust-lang/src/context.rs | 25 +++++++++---------------- dust-lang/src/core_library.rs | 6 +++--- dust-lang/src/parser.rs | 8 ++++---- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/dust-lang/src/analyzer.rs b/dust-lang/src/analyzer.rs index caced44..23bcf43 100644 --- a/dust-lang/src/analyzer.rs +++ b/dust-lang/src/analyzer.rs @@ -135,7 +135,6 @@ impl<'a> Analyzer<'a> { StructType::Unit { name: name.inner.clone(), }, - statement.position(), )?; } StructDefinition::Tuple { name, items } => { @@ -147,7 +146,6 @@ impl<'a> Analyzer<'a> { name: name.inner.clone(), fields, }, - statement.position(), )?; } StructDefinition::Fields { name, fields } => { @@ -164,7 +162,6 @@ impl<'a> Analyzer<'a> { name: name.inner.clone(), fields, }, - statement.position(), )?; } }; diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index 2998bc9..d28a03a 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -5,9 +5,9 @@ use std::{ sync::{Arc, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard}, }; -use crate::{ast::Span, Constructor, Identifier, StructType, Type, Value}; +use crate::{Constructor, Identifier, StructType, Type, Value}; -pub type Associations = HashMap; +pub type Associations = HashMap; /// Garbage-collecting context for variables. #[derive(Debug, Clone)] @@ -64,7 +64,7 @@ impl Context { pub fn get( &self, identifier: &Identifier, - ) -> Result, ContextError> { + ) -> Result, ContextError> { let associations = self.associations.read()?; Ok(associations.get(identifier).cloned()) @@ -223,20 +223,13 @@ impl Context { &self, identifier: Identifier, struct_type: StructType, - position: Span, ) -> Result<(), ContextError> { log::trace!("Setting {identifier} to constructor of type {struct_type}"); let mut variables = self.associations.write()?; let last_position = variables .get(&identifier) - .map(|(_, last_position)| { - if last_position.1 > position.1 { - *last_position - } else { - position - } - }) + .map(|(_, last_position)| *last_position) .unwrap_or_default(); variables.insert( @@ -248,13 +241,13 @@ impl Context { } /// Collects garbage up to the given position, removing all variables with lesser positions. - pub fn collect_garbage(&self, position_end: usize) -> Result<(), ContextError> { - log::trace!("Collecting garbage up to {position_end}"); + pub fn collect_garbage(&self, position: usize) -> Result<(), ContextError> { + log::trace!("Collecting garbage up to {position}"); let mut variables = self.associations.write()?; variables.retain(|identifier, (_, last_used)| { - let should_drop = position_end >= last_used.1; + let should_drop = position >= *last_used; if should_drop { log::trace!("Removing {identifier}"); @@ -274,12 +267,12 @@ impl Context { pub fn update_last_position( &self, identifier: &Identifier, - position: Span, + position: usize, ) -> Result { let mut associations = self.associations.write()?; if let Some((_, last_position)) = associations.get_mut(identifier) { - if position.1 > last_position.1 { + if position > *last_position { log::trace!("Updating {identifier}'s last position to {position:?}"); *last_position = position; diff --git a/dust-lang/src/core_library.rs b/dust-lang/src/core_library.rs index b634d5e..d4c75bd 100644 --- a/dust-lang/src/core_library.rs +++ b/dust-lang/src/core_library.rs @@ -13,7 +13,7 @@ pub fn core_library<'a>() -> &'a Context { ContextData::VariableValue(Value::function(Function::BuiltIn( BuiltInFunction::ToString, ))), - (0, 0), + 0, ), ), ( @@ -22,7 +22,7 @@ pub fn core_library<'a>() -> &'a Context { ContextData::VariableValue(Value::function(Function::BuiltIn( BuiltInFunction::ReadLine, ))), - (0, 0), + 0, ), ), ( @@ -31,7 +31,7 @@ pub fn core_library<'a>() -> &'a Context { ContextData::VariableValue(Value::function(Function::BuiltIn( BuiltInFunction::WriteLine, ))), - (0, 0), + 0, ), ), ])) diff --git a/dust-lang/src/parser.rs b/dust-lang/src/parser.rs index 2150c6a..705ecac 100644 --- a/dust-lang/src/parser.rs +++ b/dust-lang/src/parser.rs @@ -455,7 +455,7 @@ impl<'src> Parser<'src> { let identifier = Identifier::new(text); if let ParserMode::Condition = self.mode { - context.update_last_position(&identifier, start_position)?; + context.update_last_position(&identifier, start_position.1)?; return Ok(Expression::identifier(identifier, start_position)); } @@ -496,7 +496,7 @@ impl<'src> Parser<'src> { let position = (start_position.0, self.current_position.1); - context.update_last_position(&identifier, position)?; + context.update_last_position(&identifier, position.1)?; return Ok(Expression::r#struct( StructExpression::Fields { name, fields }, @@ -504,7 +504,7 @@ impl<'src> Parser<'src> { )); } - context.update_last_position(&identifier, start_position)?; + context.update_last_position(&identifier, start_position.1)?; Ok(Expression::identifier(identifier, start_position)) } @@ -988,7 +988,7 @@ impl<'src> Parser<'src> { self.next_token()?; - context.update_last_position(&identifier, position)?; + context.update_last_position(&identifier, position.1)?; Ok(Node::new(identifier, position)) } else {