Fix Block Debug formatting
This commit is contained in:
parent
b1266df835
commit
d243c030e8
@ -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<Statement>,
|
||||
@ -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<Value, RuntimeError> {
|
||||
fn run(&self, source: &str, context: &Context) -> Result<Value, RuntimeError> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
@ -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())?;
|
||||
|
@ -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<RwLock<BTreeMap<String, ValueData>>>,
|
||||
}
|
||||
@ -93,7 +92,7 @@ impl Context {
|
||||
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();
|
||||
|
||||
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> {
|
||||
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!()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user