From d243c030e83476045d50b40890274678d824aa4e Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 11 Feb 2024 15:26:09 -0500 Subject: [PATCH] Fix Block Debug formatting --- src/abstract_tree/block.rs | 24 ++++++++++++++++++++---- src/abstract_tree/for.rs | 8 ++++---- src/context.rs | 29 +++++++++++++---------------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 47c1cb8..e0fe1e8 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -1,4 +1,7 @@ -use std::sync::RwLock; +use std::{ + fmt::{self, Formatter}, + sync::RwLock, +}; use rayon::prelude::*; use serde::{Deserialize, Serialize}; @@ -16,7 +19,7 @@ use crate::{ /// results in an error. Note that this will be the first statement to encounter /// an error at runtime, not necessarilly the first statement as they are /// written. -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Block { is_async: bool, statements: Vec, @@ -44,7 +47,7 @@ impl AbstractTree for Block { node.child_count() - 2 }; let mut statements = Vec::with_capacity(statement_count); - let block_context = Context::inherit_from(context)?; + let block_context = Context::with_variables_from(context)?; for index in 1..node.child_count() - 1 { let child_node = node.child(index).unwrap(); @@ -75,7 +78,11 @@ impl AbstractTree for Block { Ok(()) } - fn run(&self, source: &str, _context: &Context) -> Result { + fn run(&self, source: &str, context: &Context) -> Result { + self.context.inherit_from(context)?; + + println!("{:?}", self.context); + if self.is_async { let statements = &self.statements; let final_result = RwLock::new(Ok(Value::none())); @@ -162,3 +169,12 @@ impl Format for Block { output.push('}'); } } + +impl fmt::Debug for Block { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.debug_struct("Block") + .field("is_async", &self.is_async) + .field("statements", &self.statements) + .finish() + } +} diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index 892a2ab..a768150 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -83,14 +83,14 @@ impl AbstractTree for For { if let Value::Range(range) = expression_run { if self.is_async { range.into_par_iter().try_for_each(|integer| { - let iter_context = Context::inherit_from(context)?; + let iter_context = Context::with_variables_from(context)?; iter_context.set_value(key.clone(), Value::Integer(integer))?; self.block.run(source, &iter_context).map(|_value| ()) })?; } else { - let loop_context = Context::inherit_from(context)?; + let loop_context = Context::with_variables_from(context)?; for i in range { loop_context.set_value(key.clone(), Value::Integer(i))?; @@ -106,14 +106,14 @@ impl AbstractTree for For { if self.is_async { values.par_iter().try_for_each(|value| { - let iter_context = Context::inherit_from(context)?; + let iter_context = Context::with_variables_from(context)?; iter_context.set_value(key.clone(), value.clone())?; self.block.run(source, &iter_context).map(|_value| ()) })?; } else { - let loop_context = Context::inherit_from(context)?; + let loop_context = Context::with_variables_from(context)?; for value in values.iter() { loop_context.set_value(key.clone(), value.clone())?; diff --git a/src/context.rs b/src/context.rs index f09f283..13a07e2 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,13 +1,12 @@ use std::{ cmp::Ordering, collections::BTreeMap, - fmt::{self, Debug, Display, Formatter}, sync::{Arc, RwLock, RwLockReadGuard}, }; use crate::{error::rw_lock_error::RwLockError, Type, Value}; -#[derive(Clone)] +#[derive(Clone, Debug)] pub enum ValueData { Value { inner: Value, @@ -77,7 +76,7 @@ impl Ord for ValueData { } } -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Context { inner: Arc>>, } @@ -93,7 +92,7 @@ impl Context { Ok(self.inner.read()?) } - pub fn inherit_from(other: &Context) -> Result { + pub fn with_variables_from(other: &Context) -> Result { let mut new_variables = BTreeMap::new(); for (identifier, value_data) in other.inner.read()?.iter() { @@ -105,6 +104,16 @@ impl Context { }) } + pub fn inherit_from(&self, other: &Context) -> Result<(), RwLockError> { + let mut self_variables = self.inner.write()?; + + for (identifier, value_data) in other.inner.read()?.iter() { + self_variables.insert(identifier.clone(), value_data.clone()); + } + + Ok(()) + } + pub fn get_value(&self, key: &str) -> Result, RwLockError> { if let Some(value_data) = self.inner.read()?.get(key) { if let ValueData::Value { inner, .. } = value_data { @@ -192,15 +201,3 @@ impl Ord for Context { left.cmp(&right) } } - -impl Debug for Context { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{self}") - } -} - -impl Display for Context { - fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { - todo!() - } -}