2020-10-28 15:30:35 +01:00
|
|
|
use crate::dynamic::Variant;
|
2020-10-18 15:47:34 +02:00
|
|
|
use crate::stdlib::{
|
|
|
|
boxed::Box,
|
|
|
|
ops::{Add, Range},
|
2021-03-01 02:30:23 +01:00
|
|
|
string::ToString,
|
2020-10-18 15:47:34 +02:00
|
|
|
};
|
2021-02-27 08:27:40 +01:00
|
|
|
use crate::{def_package, EvalAltResult, Position, INT};
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-10-18 11:02:17 +02:00
|
|
|
fn get_range<T: Variant + Clone>(from: T, to: T) -> Result<Range<T>, Box<EvalAltResult>> {
|
2020-05-13 13:21:42 +02:00
|
|
|
Ok(from..to)
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
// Register range function with step
|
|
|
|
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
|
|
|
|
struct StepRange<T>(T, T, T)
|
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd;
|
|
|
|
|
2021-02-27 08:27:40 +01:00
|
|
|
impl<T> StepRange<T>
|
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd,
|
|
|
|
{
|
|
|
|
pub fn new(from: T, to: T, step: T) -> Result<Self, Box<EvalAltResult>> {
|
|
|
|
if &from + &step == from {
|
|
|
|
Err(Box::new(EvalAltResult::ErrorArithmetic(
|
|
|
|
"invalid step value".to_string(),
|
|
|
|
Position::NONE,
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(Self(from, to, step))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:11:25 +02:00
|
|
|
impl<T> Iterator for StepRange<T>
|
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd,
|
|
|
|
{
|
|
|
|
type Item = T;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<T> {
|
2021-02-27 08:27:40 +01:00
|
|
|
if self.0 == self.1 {
|
|
|
|
None
|
|
|
|
} else if self.0 < self.1 {
|
2020-04-20 18:11:25 +02:00
|
|
|
let v = self.0.clone();
|
2021-02-27 08:27:40 +01:00
|
|
|
let n = self.0.add(&self.2);
|
|
|
|
self.0 = if n >= self.1 { self.1.clone() } else { n };
|
2020-04-20 18:11:25 +02:00
|
|
|
Some(v)
|
|
|
|
} else {
|
2021-02-27 08:27:40 +01:00
|
|
|
let v = self.0.clone();
|
|
|
|
let n = self.0.add(&self.2);
|
|
|
|
self.0 = if n <= self.1 { self.1.clone() } else { n };
|
|
|
|
Some(v)
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 11:02:17 +02:00
|
|
|
fn get_step_range<T>(from: T, to: T, step: T) -> Result<StepRange<T>, Box<EvalAltResult>>
|
2020-05-13 13:21:42 +02:00
|
|
|
where
|
|
|
|
for<'a> &'a T: Add<&'a T, Output = T>,
|
|
|
|
T: Variant + Clone + PartialOrd,
|
|
|
|
{
|
2021-02-27 08:27:40 +01:00
|
|
|
StepRange::<T>::new(from, to, step)
|
2020-05-13 13:21:42 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
macro_rules! reg_range {
|
|
|
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
|
|
|
$(
|
|
|
|
$lib.set_iterator::<Range<$y>>();
|
2020-11-22 10:21:34 +01:00
|
|
|
let hash = $lib.set_fn_2($x, get_range::<$y>);
|
2021-03-01 08:58:11 +01:00
|
|
|
$lib.update_fn_metadata(hash, &[
|
2020-11-22 15:15:17 +01:00
|
|
|
concat!("from: ", stringify!($y)),
|
|
|
|
concat!("to: ", stringify!($y)),
|
|
|
|
concat!("Iterator<Item=", stringify!($y), ">")
|
|
|
|
]);
|
2020-10-19 17:49:01 +02:00
|
|
|
)*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
macro_rules! reg_stepped_range {
|
2020-10-19 17:49:01 +02:00
|
|
|
($lib:expr, $x:expr, $( $y:ty ),*) => (
|
|
|
|
$(
|
|
|
|
$lib.set_iterator::<StepRange<$y>>();
|
2020-11-22 10:21:34 +01:00
|
|
|
let hash = $lib.set_fn_3($x, get_step_range::<$y>);
|
2021-03-01 08:58:11 +01:00
|
|
|
$lib.update_fn_metadata(hash, &[
|
2020-11-22 15:15:17 +01:00
|
|
|
concat!("from: ", stringify!($y)),
|
|
|
|
concat!("to: ", stringify!($y)),
|
2021-02-19 16:13:53 +01:00
|
|
|
concat!("step: ", stringify!($y)),
|
|
|
|
concat!("Iterator<Item=", stringify!($y), ">")
|
2020-11-22 15:15:17 +01:00
|
|
|
]);
|
2020-10-19 17:49:01 +02:00
|
|
|
)*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:21:42 +02:00
|
|
|
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
|
2021-02-19 16:13:53 +01:00
|
|
|
reg_range!(lib, "range", INT);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
2021-02-19 16:13:53 +01:00
|
|
|
reg_range!(lib, "range", i8, u8, i16, u16, i32, u32, i64, u64);
|
2020-06-17 10:50:46 +02:00
|
|
|
|
2020-07-31 16:30:23 +02:00
|
|
|
if cfg!(not(target_arch = "wasm32")) {
|
|
|
|
reg_range!(lib, "range", i128, u128);
|
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
|
2021-02-19 16:13:53 +01:00
|
|
|
reg_stepped_range!(lib, "range", INT);
|
2020-04-21 17:01:10 +02:00
|
|
|
|
2020-10-19 17:49:01 +02:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
2021-02-19 16:13:53 +01:00
|
|
|
reg_stepped_range!(lib, "range", i8, u8, i16, u16, i32, u32, i64, u64);
|
2020-06-17 10:50:46 +02:00
|
|
|
|
2020-07-31 16:30:23 +02:00
|
|
|
if cfg!(not(target_arch = "wasm32")) {
|
2021-02-19 16:13:53 +01:00
|
|
|
reg_stepped_range!(lib, "range", i128, u128);
|
2020-07-31 16:30:23 +02:00
|
|
|
}
|
2020-04-20 18:11:25 +02:00
|
|
|
}
|
2020-04-21 17:01:10 +02:00
|
|
|
});
|