From 4433c587f5829071925f22583614519542f17c65 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 2 Sep 2024 05:53:09 -0400 Subject: [PATCH] Clean up context --- dust-lang/src/ast/mod.rs | 9 ++++++++- dust-lang/src/context.rs | 29 ++++++++++++----------------- dust-lang/src/parser.rs | 8 +++----- dust-lang/src/vm.rs | 14 +++++++------- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/dust-lang/src/ast/mod.rs b/dust-lang/src/ast/mod.rs index 3906081..6afaafc 100644 --- a/dust-lang/src/ast/mod.rs +++ b/dust-lang/src/ast/mod.rs @@ -13,7 +13,7 @@ use std::{ use serde::{Deserialize, Serialize}; -use crate::{Context, ContextError}; +use crate::{core_library, Context, ContextError}; pub type Span = (usize, usize); @@ -68,6 +68,13 @@ impl AbstractSyntaxTree { context: Context::new(), } } + + pub fn with_core_library() -> Self { + Self { + statements: VecDeque::new(), + context: core_library().create_child(), + } + } } impl Default for AbstractSyntaxTree { diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index d28a03a..86cf219 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -13,7 +13,7 @@ pub type Associations = HashMap; #[derive(Debug, Clone)] pub struct Context { associations: Arc>, - parent: Arc>>, + parent: Option>, } impl Context { @@ -24,7 +24,7 @@ impl Context { pub fn with_data(data: Associations) -> Self { Self { associations: Arc::new(RwLock::new(data)), - parent: Arc::new(RwLock::new(None)), + parent: None, } } @@ -36,14 +36,10 @@ impl Context { pub fn create_child(&self) -> Self { Self { associations: Arc::new(RwLock::new(HashMap::new())), - parent: Arc::new(RwLock::new(Some(self.clone()))), + parent: Some(Box::new(self.clone())), } } - pub fn assign_parent(&self, parent: Self) { - self.parent.write().unwrap().replace(parent); - } - /// Returns the number of associated identifiers in the context. pub fn association_count(&self) -> Result { Ok(self.associations.read()?.len()) @@ -53,7 +49,7 @@ impl Context { pub fn contains(&self, identifier: &Identifier) -> Result { if self.associations.read()?.contains_key(identifier) { Ok(true) - } else if let Some(parent) = self.parent.read().unwrap().as_ref() { + } else if let Some(parent) = &self.parent { parent.contains(identifier) } else { Ok(false) @@ -81,7 +77,7 @@ impl Context { _ => {} } - if let Some(parent) = self.parent.read().unwrap().as_ref() { + if let Some(parent) = &self.parent { parent.get_type(identifier) } else { Ok(None) @@ -92,7 +88,7 @@ impl Context { pub fn get_data(&self, identifier: &Identifier) -> Result, ContextError> { if let Some((variable_data, _)) = self.associations.read()?.get(identifier) { Ok(Some(variable_data.clone())) - } else if let Some(parent) = self.parent.read().unwrap().as_ref() { + } else if let Some(parent) = &self.parent { parent.get_data(identifier) } else { Ok(None) @@ -108,7 +104,7 @@ impl Context { self.associations.read()?.get(identifier) { Ok(Some(value.clone())) - } else if let Some(parent) = self.parent.read().unwrap().as_ref() { + } else if let Some(parent) = &self.parent { parent.get_variable_value(identifier) } else { Ok(None) @@ -124,7 +120,7 @@ impl Context { self.associations.read()?.get(identifier) { Ok(Some(constructor.clone())) - } else if let Some(parent) = self.parent.read().unwrap().as_ref() { + } else if let Some(parent) = &self.parent { parent.get_constructor(identifier) } else { Ok(None) @@ -144,7 +140,7 @@ impl Context { ContextData::ConstructorType(struct_type) => Ok(Some(struct_type.clone())), _ => Ok(None), } - } else if let Some(parent) = self.parent.read().unwrap().as_ref() { + } else if let Some(parent) = &self.parent { parent.get_constructor_type(identifier) } else { Ok(None) @@ -280,7 +276,7 @@ impl Context { Ok(true) } else { - let ancestor_contains = if let Some(parent) = self.parent.read().unwrap().as_ref() { + let ancestor_contains = if let Some(parent) = &self.parent { parent.contains(identifier)? } else { false @@ -300,9 +296,8 @@ impl Context { /// 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. - pub fn recover_from_poison(&mut self, error: &ContextError) { + /// This method is not used. + pub fn _recover_from_poison(&mut self, error: &ContextError) { log::debug!("Context is recovering from poison error"); let ContextError::PoisonErrorRecovered(recovered) = error; diff --git a/dust-lang/src/parser.rs b/dust-lang/src/parser.rs index 705ecac..460c6c4 100644 --- a/dust-lang/src/parser.rs +++ b/dust-lang/src/parser.rs @@ -11,8 +11,8 @@ use std::{ }; use crate::{ - ast::*, core_library, Context, ContextError, DustError, Identifier, LexError, Lexer, Token, - TokenKind, TokenOwned, Type, + ast::*, Context, ContextError, DustError, Identifier, LexError, Lexer, Token, TokenKind, + TokenOwned, Type, }; /// Parses the input into an abstract syntax tree. @@ -38,9 +38,7 @@ use crate::{ /// ); /// ``` pub fn parse(source: &str) -> Result { - let mut tree = AbstractSyntaxTree::new(); - - tree.context.assign_parent(core_library().clone()); + let mut tree = AbstractSyntaxTree::with_core_library(); let lexer = Lexer::new(source); let mut parser = Parser::new(lexer); diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index f0d72ec..94b7f60 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -357,20 +357,20 @@ impl Vm { if let Some(ContextData::Constructor(constructor)) = get_data { let construct_result = constructor.construct_unit(); - match construct_result { + return match construct_result { Ok(value) => Ok(Evaluation::Return(Some(value))), Err(ConstructError::ExpectedUnit) => Ok(Evaluation::Constructor(constructor)), Err(error) => Err(RuntimeError::ConstructError { error, position: identifier.position, }), - } - } else { - Err(RuntimeError::UnassociatedIdentifier { - identifier: identifier.inner, - position: identifier.position, - }) + }; } + + Err(RuntimeError::UnassociatedIdentifier { + identifier: identifier.inner, + position: identifier.position, + }) } fn run_struct(