1
0

Fix ranges

This commit is contained in:
Jeff 2024-01-30 09:46:49 -05:00
parent e756c7eac1
commit 61e7079a00
4 changed files with 53 additions and 36 deletions

View File

@ -56,16 +56,10 @@ 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 {
let mut iterable = Vec::with_capacity((range.end - range.start) as usize); range.into_par_iter().try_for_each(|integer| {
for i in range.start..range.end {
iterable.push(Value::Integer(i));
}
iterable.par_iter().try_for_each(|value| {
let iter_context = Map::clone_from(context)?; 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| ()) self.block.run(source, &iter_context).map(|_value| ())
})?; })?;

View File

@ -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 serde::{Deserialize, Serialize};
use crate::{ use crate::{
AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier, AbstractTree, BuiltInValue, Error, Expression, Format, Function, FunctionNode, Identifier,
List, Map, Range, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, List, Map, Result, Statement, Structure, SyntaxNode, Type, TypeDefintion, TypeSpecification,
TypeSpecification, Value, Value,
}; };
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
pub enum ValueNode { pub enum ValueNode {
Boolean(String), Boolean(String),
Float(String), Float(String),
@ -20,7 +20,7 @@ pub enum ValueNode {
Map(BTreeMap<String, (Statement, Option<Type>)>), Map(BTreeMap<String, (Statement, Option<Type>)>),
BuiltInValue(BuiltInValue), BuiltInValue(BuiltInValue),
Structure(BTreeMap<String, (Option<Statement>, Type)>), Structure(BTreeMap<String, (Option<Statement>, Type)>),
Range(Range), Range(Range<i64>),
} }
impl AbstractTree for ValueNode { impl AbstractTree for ValueNode {
@ -257,7 +257,7 @@ impl AbstractTree for ValueNode {
Value::TypeDefinition(TypeDefintion::Structure(Structure::new(value_map))) Value::TypeDefinition(TypeDefintion::Structure(Structure::new(value_map)))
} }
ValueNode::Range(range) => Value::Range(*range), ValueNode::Range(range) => Value::Range(range.clone()),
}; };
Ok(value) 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<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

View File

@ -15,18 +15,16 @@ use std::{
convert::TryFrom, convert::TryFrom,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
marker::PhantomData, marker::PhantomData,
ops::{Add, AddAssign, Div, Mul, Rem, Sub, SubAssign}, ops::{Add, AddAssign, Div, Mul, Range, Rem, Sub, SubAssign},
}; };
pub use self::{ pub use self::{
function::Function, list::List, map::Map, range::Range, structure::Structure, function::Function, list::List, map::Map, structure::Structure, type_definition::TypeDefintion,
type_definition::TypeDefintion,
}; };
pub mod function; pub mod function;
pub mod list; pub mod list;
pub mod map; pub mod map;
pub mod range;
pub mod structure; pub mod structure;
pub mod type_definition; pub mod type_definition;
@ -44,7 +42,7 @@ pub enum Value {
Float(f64), Float(f64),
Integer(i64), Integer(i64),
Boolean(bool), Boolean(bool),
Range(Range), Range(Range<i64>),
Option(Option<Box<Value>>), Option(Option<Box<Value>>),
TypeDefinition(TypeDefintion), TypeDefinition(TypeDefintion),
} }
@ -483,7 +481,12 @@ impl Ord for Value {
(Value::Function(_), _) => Ordering::Greater, (Value::Function(_), _) => Ordering::Greater,
(Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right), (Value::TypeDefinition(left), Value::TypeDefinition(right)) => left.cmp(right),
(Value::TypeDefinition(_), _) => Ordering::Greater, (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::Range(_), _) => Ordering::Greater,
(Value::Option(left), Value::Option(right)) => left.cmp(right), (Value::Option(left), Value::Option(right)) => left.cmp(right),
(Value::Option(_), _) => Ordering::Less, (Value::Option(_), _) => Ordering::Less,
@ -538,7 +541,7 @@ impl Display for Value {
Value::Map(map) => write!(f, "{map}"), Value::Map(map) => write!(f, "{map}"),
Value::Function(function) => write!(f, "{function}"), Value::Function(function) => write!(f, "{function}"),
Value::TypeDefinition(structure) => write!(f, "{structure}"), Value::TypeDefinition(structure) => write!(f, "{structure}"),
Value::Range(range) => write!(f, "{range}"), Value::Range(range) => write!(f, "{}..{}", range.start, range.end),
} }
} }
} }

View File

@ -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)
}
}