1
0

Fix Block Debug formatting

This commit is contained in:
Jeff 2024-02-11 15:26:09 -05:00
parent b1266df835
commit d243c030e8
3 changed files with 37 additions and 24 deletions

View File

@ -1,4 +1,7 @@
use std::sync::RwLock; use std::{
fmt::{self, Formatter},
sync::RwLock,
};
use rayon::prelude::*; use rayon::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -16,7 +19,7 @@ use crate::{
/// results in an error. Note that this will be the first statement to encounter /// 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 /// an error at runtime, not necessarilly the first statement as they are
/// written. /// written.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub struct Block { pub struct Block {
is_async: bool, is_async: bool,
statements: Vec<Statement>, statements: Vec<Statement>,
@ -44,7 +47,7 @@ impl AbstractTree for Block {
node.child_count() - 2 node.child_count() - 2
}; };
let mut statements = Vec::with_capacity(statement_count); 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 { for index in 1..node.child_count() - 1 {
let child_node = node.child(index).unwrap(); let child_node = node.child(index).unwrap();
@ -75,7 +78,11 @@ impl AbstractTree for Block {
Ok(()) Ok(())
} }
fn run(&self, source: &str, _context: &Context) -> Result<Value, RuntimeError> { fn run(&self, source: &str, context: &Context) -> Result<Value, RuntimeError> {
self.context.inherit_from(context)?;
println!("{:?}", self.context);
if self.is_async { if self.is_async {
let statements = &self.statements; let statements = &self.statements;
let final_result = RwLock::new(Ok(Value::none())); let final_result = RwLock::new(Ok(Value::none()));
@ -162,3 +169,12 @@ impl Format for Block {
output.push('}'); 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()
}
}

View File

@ -83,14 +83,14 @@ impl AbstractTree for For {
if let Value::Range(range) = expression_run { if let Value::Range(range) = expression_run {
if self.is_async { if self.is_async {
range.into_par_iter().try_for_each(|integer| { 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))?; iter_context.set_value(key.clone(), Value::Integer(integer))?;
self.block.run(source, &iter_context).map(|_value| ()) self.block.run(source, &iter_context).map(|_value| ())
})?; })?;
} else { } else {
let loop_context = Context::inherit_from(context)?; let loop_context = Context::with_variables_from(context)?;
for i in range { for i in range {
loop_context.set_value(key.clone(), Value::Integer(i))?; loop_context.set_value(key.clone(), Value::Integer(i))?;
@ -106,14 +106,14 @@ impl AbstractTree for For {
if self.is_async { if self.is_async {
values.par_iter().try_for_each(|value| { 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())?; iter_context.set_value(key.clone(), value.clone())?;
self.block.run(source, &iter_context).map(|_value| ()) self.block.run(source, &iter_context).map(|_value| ())
})?; })?;
} else { } else {
let loop_context = Context::inherit_from(context)?; let loop_context = Context::with_variables_from(context)?;
for value in values.iter() { for value in values.iter() {
loop_context.set_value(key.clone(), value.clone())?; loop_context.set_value(key.clone(), value.clone())?;

View File

@ -1,13 +1,12 @@
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
collections::BTreeMap, collections::BTreeMap,
fmt::{self, Debug, Display, Formatter},
sync::{Arc, RwLock, RwLockReadGuard}, sync::{Arc, RwLock, RwLockReadGuard},
}; };
use crate::{error::rw_lock_error::RwLockError, Type, Value}; use crate::{error::rw_lock_error::RwLockError, Type, Value};
#[derive(Clone)] #[derive(Clone, Debug)]
pub enum ValueData { pub enum ValueData {
Value { Value {
inner: Value, inner: Value,
@ -77,7 +76,7 @@ impl Ord for ValueData {
} }
} }
#[derive(Clone)] #[derive(Clone, Debug)]
pub struct Context { pub struct Context {
inner: Arc<RwLock<BTreeMap<String, ValueData>>>, inner: Arc<RwLock<BTreeMap<String, ValueData>>>,
} }
@ -93,7 +92,7 @@ impl Context {
Ok(self.inner.read()?) Ok(self.inner.read()?)
} }
pub fn inherit_from(other: &Context) -> Result<Context, RwLockError> { pub fn with_variables_from(other: &Context) -> Result<Context, RwLockError> {
let mut new_variables = BTreeMap::new(); let mut new_variables = BTreeMap::new();
for (identifier, value_data) in other.inner.read()?.iter() { 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<Option<Value>, RwLockError> { pub fn get_value(&self, key: &str) -> Result<Option<Value>, RwLockError> {
if let Some(value_data) = self.inner.read()?.get(key) { if let Some(value_data) = self.inner.read()?.get(key) {
if let ValueData::Value { inner, .. } = value_data { if let ValueData::Value { inner, .. } = value_data {
@ -192,15 +201,3 @@ impl Ord for Context {
left.cmp(&right) 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!()
}
}