1
0

Clean up context

This commit is contained in:
Jeff 2024-09-02 05:53:09 -04:00
parent d32061ebba
commit 4433c587f5
4 changed files with 30 additions and 30 deletions

View File

@ -13,7 +13,7 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{Context, ContextError}; use crate::{core_library, Context, ContextError};
pub type Span = (usize, usize); pub type Span = (usize, usize);
@ -68,6 +68,13 @@ impl AbstractSyntaxTree {
context: Context::new(), context: Context::new(),
} }
} }
pub fn with_core_library() -> Self {
Self {
statements: VecDeque::new(),
context: core_library().create_child(),
}
}
} }
impl Default for AbstractSyntaxTree { impl Default for AbstractSyntaxTree {

View File

@ -13,7 +13,7 @@ pub type Associations = HashMap<Identifier, (ContextData, usize)>;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Context { pub struct Context {
associations: Arc<RwLock<Associations>>, associations: Arc<RwLock<Associations>>,
parent: Arc<RwLock<Option<Context>>>, parent: Option<Box<Context>>,
} }
impl Context { impl Context {
@ -24,7 +24,7 @@ impl Context {
pub fn with_data(data: Associations) -> Self { pub fn with_data(data: Associations) -> Self {
Self { Self {
associations: Arc::new(RwLock::new(data)), 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 { pub fn create_child(&self) -> Self {
Self { Self {
associations: Arc::new(RwLock::new(HashMap::new())), 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. /// Returns the number of associated identifiers in the context.
pub fn association_count(&self) -> Result<usize, ContextError> { pub fn association_count(&self) -> Result<usize, ContextError> {
Ok(self.associations.read()?.len()) Ok(self.associations.read()?.len())
@ -53,7 +49,7 @@ impl Context {
pub fn contains(&self, identifier: &Identifier) -> Result<bool, ContextError> { pub fn contains(&self, identifier: &Identifier) -> Result<bool, ContextError> {
if self.associations.read()?.contains_key(identifier) { if self.associations.read()?.contains_key(identifier) {
Ok(true) Ok(true)
} else if let Some(parent) = self.parent.read().unwrap().as_ref() { } else if let Some(parent) = &self.parent {
parent.contains(identifier) parent.contains(identifier)
} else { } else {
Ok(false) 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) parent.get_type(identifier)
} else { } else {
Ok(None) Ok(None)
@ -92,7 +88,7 @@ impl Context {
pub fn get_data(&self, identifier: &Identifier) -> Result<Option<ContextData>, ContextError> { pub fn get_data(&self, identifier: &Identifier) -> Result<Option<ContextData>, ContextError> {
if let Some((variable_data, _)) = self.associations.read()?.get(identifier) { if let Some((variable_data, _)) = self.associations.read()?.get(identifier) {
Ok(Some(variable_data.clone())) 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) parent.get_data(identifier)
} else { } else {
Ok(None) Ok(None)
@ -108,7 +104,7 @@ impl Context {
self.associations.read()?.get(identifier) self.associations.read()?.get(identifier)
{ {
Ok(Some(value.clone())) 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) parent.get_variable_value(identifier)
} else { } else {
Ok(None) Ok(None)
@ -124,7 +120,7 @@ impl Context {
self.associations.read()?.get(identifier) self.associations.read()?.get(identifier)
{ {
Ok(Some(constructor.clone())) 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) parent.get_constructor(identifier)
} else { } else {
Ok(None) Ok(None)
@ -144,7 +140,7 @@ impl Context {
ContextData::ConstructorType(struct_type) => Ok(Some(struct_type.clone())), ContextData::ConstructorType(struct_type) => Ok(Some(struct_type.clone())),
_ => Ok(None), _ => 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) parent.get_constructor_type(identifier)
} else { } else {
Ok(None) Ok(None)
@ -280,7 +276,7 @@ impl Context {
Ok(true) Ok(true)
} else { } 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)? parent.contains(identifier)?
} else { } else {
false false
@ -300,9 +296,8 @@ impl Context {
/// Recovers the context from a poisoned state by recovering data from an error. /// 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 /// This method is not used.
/// they are infallible. pub fn _recover_from_poison(&mut self, error: &ContextError) {
pub fn recover_from_poison(&mut self, error: &ContextError) {
log::debug!("Context is recovering from poison error"); log::debug!("Context is recovering from poison error");
let ContextError::PoisonErrorRecovered(recovered) = error; let ContextError::PoisonErrorRecovered(recovered) = error;

View File

@ -11,8 +11,8 @@ use std::{
}; };
use crate::{ use crate::{
ast::*, core_library, Context, ContextError, DustError, Identifier, LexError, Lexer, Token, ast::*, Context, ContextError, DustError, Identifier, LexError, Lexer, Token, TokenKind,
TokenKind, TokenOwned, Type, TokenOwned, Type,
}; };
/// Parses the input into an abstract syntax tree. /// Parses the input into an abstract syntax tree.
@ -38,9 +38,7 @@ use crate::{
/// ); /// );
/// ``` /// ```
pub fn parse(source: &str) -> Result<AbstractSyntaxTree, DustError> { pub fn parse(source: &str) -> Result<AbstractSyntaxTree, DustError> {
let mut tree = AbstractSyntaxTree::new(); let mut tree = AbstractSyntaxTree::with_core_library();
tree.context.assign_parent(core_library().clone());
let lexer = Lexer::new(source); let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer); let mut parser = Parser::new(lexer);

View File

@ -357,20 +357,20 @@ impl Vm {
if let Some(ContextData::Constructor(constructor)) = get_data { if let Some(ContextData::Constructor(constructor)) = get_data {
let construct_result = constructor.construct_unit(); let construct_result = constructor.construct_unit();
match construct_result { return match construct_result {
Ok(value) => Ok(Evaluation::Return(Some(value))), Ok(value) => Ok(Evaluation::Return(Some(value))),
Err(ConstructError::ExpectedUnit) => Ok(Evaluation::Constructor(constructor)), Err(ConstructError::ExpectedUnit) => Ok(Evaluation::Constructor(constructor)),
Err(error) => Err(RuntimeError::ConstructError { Err(error) => Err(RuntimeError::ConstructError {
error, error,
position: identifier.position, 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( fn run_struct(