From 61e7079a0049c41c61674f58f41d4808c6ee6d48 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 30 Jan 2024 09:46:49 -0500 Subject: [PATCH] Fix ranges --- src/abstract_tree/for.rs | 10 ++----- src/abstract_tree/value_node.rs | 47 ++++++++++++++++++++++++++++----- src/value/mod.rs | 17 +++++++----- src/value/range.rs | 15 ----------- 4 files changed, 53 insertions(+), 36 deletions(-) delete mode 100644 src/value/range.rs diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index 57f9e7d..7123fb1 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -56,16 +56,10 @@ impl AbstractTree for For { if let Value::Range(range) = expression_run { if self.is_async { - let mut iterable = Vec::with_capacity((range.end - range.start) as usize); - - for i in range.start..range.end { - iterable.push(Value::Integer(i)); - } - - iterable.par_iter().try_for_each(|value| { + range.into_par_iter().try_for_each(|integer| { let iter_context = Map::clone_from(context)?; - iter_context.set(key.clone(), value.clone())?; + iter_context.set(key.clone(), Value::Integer(integer))?; self.block.run(source, &iter_context).map(|_value| ()) })?; diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 20e081f..b49a1c6 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -1,14 +1,14 @@ -use std::{collections::BTreeMap, sync::Arc}; +use std::{cmp::Ordering, collections::BTreeMap, ops::Range, sync::Arc}; use serde::{Deserialize, Serialize}; use crate::{ AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier, - List, Map, Range, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, - TypeSpecification, Value, + List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, TypeSpecification, + Value, }; -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] pub enum ValueNode { Boolean(String), Float(String), @@ -20,7 +20,7 @@ pub enum ValueNode { Map(BTreeMap)>), BuiltInValue(BuiltInValue), Structure(BTreeMap, Type)>), - Range(Range), + Range(Range), } impl AbstractTree for ValueNode { @@ -257,7 +257,7 @@ impl AbstractTree for ValueNode { Value::TypeDefinition(TypeDefintion::Structure(Structure::new(value_map))) } - ValueNode::Range(range) => Value::Range(*range), + ValueNode::Range(range) => Value::Range(range.clone()), }; Ok(value) @@ -397,3 +397,38 @@ impl Format for ValueNode { } } } + +impl Ord for ValueNode { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + match (self, other) { + (ValueNode::Boolean(left), ValueNode::Boolean(right)) => left.cmp(right), + (ValueNode::Boolean(_), _) => Ordering::Greater, + (ValueNode::Float(left), ValueNode::Float(right)) => left.cmp(right), + (ValueNode::Float(_), _) => Ordering::Greater, + (ValueNode::Function(left), ValueNode::Function(right)) => left.cmp(right), + (ValueNode::Function(_), _) => Ordering::Greater, + (ValueNode::Integer(left), ValueNode::Integer(right)) => left.cmp(right), + (ValueNode::Integer(_), _) => Ordering::Greater, + (ValueNode::String(left), ValueNode::String(right)) => left.cmp(right), + (ValueNode::String(_), _) => Ordering::Greater, + (ValueNode::List(left), ValueNode::List(right)) => left.cmp(right), + (ValueNode::List(_), _) => Ordering::Greater, + (ValueNode::Option(left), ValueNode::Option(right)) => left.cmp(right), + (ValueNode::Option(_), _) => Ordering::Greater, + (ValueNode::Map(left), ValueNode::Map(right)) => left.cmp(right), + (ValueNode::Map(_), _) => Ordering::Greater, + (ValueNode::BuiltInValue(left), ValueNode::BuiltInValue(right)) => left.cmp(right), + (ValueNode::BuiltInValue(_), _) => Ordering::Greater, + (ValueNode::Structure(left), ValueNode::Structure(right)) => left.cmp(right), + (ValueNode::Structure(_), _) => Ordering::Greater, + (ValueNode::Range(left), ValueNode::Range(right)) => left.clone().cmp(right.clone()), + (ValueNode::Range(_), _) => Ordering::Less, + } + } +} + +impl PartialOrd for ValueNode { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} diff --git a/src/value/mod.rs b/src/value/mod.rs index cecde8d..7f5d6e6 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -15,18 +15,16 @@ use std::{ convert::TryFrom, fmt::{self, Display, Formatter}, marker::PhantomData, - ops::{Add, AddAssign, Div, Mul, Rem, Sub, SubAssign}, + ops::{Add, AddAssign, Div, Mul, Range, Rem, Sub, SubAssign}, }; pub use self::{ - function::Function, list::List, map::Map, range::Range, structure::Structure, - type_definition::TypeDefintion, + function::Function, list::List, map::Map, structure::Structure, type_definition::TypeDefintion, }; pub mod function; pub mod list; pub mod map; -pub mod range; pub mod structure; pub mod type_definition; @@ -44,7 +42,7 @@ pub enum Value { Float(f64), Integer(i64), Boolean(bool), - Range(Range), + Range(Range), Option(Option>), TypeDefinition(TypeDefintion), } @@ -483,7 +481,12 @@ impl Ord for Value { (Value::Function(_), _) => Ordering::Greater, (Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right), (Value::TypeDefinition(_), _) => Ordering::Greater, - (Value::Range(left), Value::Range(right)) => left.cmp(right), + (Value::Range(left), Value::Range(right)) => { + let left_len = left.end - left.start; + let right_len = right.end - right.start; + + left_len.cmp(&right_len) + } (Value::Range(_), _) => Ordering::Greater, (Value::Option(left), Value::Option(right)) => left.cmp(right), (Value::Option(_), _) => Ordering::Less, @@ -538,7 +541,7 @@ impl Display for Value { Value::Map(map) => write!(f, "{map}"), Value::Function(function) => write!(f, "{function}"), Value::TypeDefinition(structure) => write!(f, "{structure}"), - Value::Range(range) => write!(f, "{range}"), + Value::Range(range) => write!(f, "{}..{}", range.start, range.end), } } } diff --git a/src/value/range.rs b/src/value/range.rs deleted file mode 100644 index f4589cd..0000000 --- a/src/value/range.rs +++ /dev/null @@ -1,15 +0,0 @@ -use std::fmt::{self, Display, Formatter}; - -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] -pub struct Range { - pub start: i64, - pub end: i64, -} - -impl Display for Range { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}..{}", self.start, self.end) - } -}