2022-01-07 12:19:01 +08:00
|
|
|
use crate::def_package;
|
2020-08-14 13:43:26 +08:00
|
|
|
use crate::plugin::*;
|
2021-04-17 15:15:54 +08:00
|
|
|
#[cfg(feature = "no_std")]
|
|
|
|
use std::prelude::v1::*;
|
2020-04-24 12:39:24 +08:00
|
|
|
|
2020-11-04 12:34:34 +08:00
|
|
|
#[cfg(any(
|
|
|
|
not(feature = "no_float"),
|
|
|
|
all(not(feature = "only_i32"), not(feature = "only_i64"))
|
|
|
|
))]
|
2020-08-14 13:43:26 +08:00
|
|
|
macro_rules! gen_cmp_functions {
|
2020-08-20 22:11:41 +08:00
|
|
|
($root:ident => $($arg_type:ident),+) => {
|
2020-08-28 23:13:38 +08:00
|
|
|
mod $root { $(pub mod $arg_type {
|
|
|
|
use super::super::*;
|
2020-04-21 00:11:25 +08:00
|
|
|
|
2020-08-28 23:13:38 +08:00
|
|
|
#[export_module]
|
|
|
|
pub mod functions {
|
2021-02-24 15:45:29 +08:00
|
|
|
#[rhai_fn(name = "<")] pub fn lt(x: $arg_type, y: $arg_type) -> bool { x < y }
|
|
|
|
#[rhai_fn(name = "<=")] pub fn lte(x: $arg_type, y: $arg_type) -> bool { x <= y }
|
|
|
|
#[rhai_fn(name = ">")] pub fn gt(x: $arg_type, y: $arg_type) -> bool { x > y }
|
|
|
|
#[rhai_fn(name = ">=")] pub fn gte(x: $arg_type, y: $arg_type) -> bool { x >= y }
|
|
|
|
#[rhai_fn(name = "==")] pub fn eq(x: $arg_type, y: $arg_type) -> bool { x == y }
|
|
|
|
#[rhai_fn(name = "!=")] pub fn ne(x: $arg_type, y: $arg_type) -> bool { x != y }
|
2020-08-14 13:43:26 +08:00
|
|
|
}
|
2020-08-28 23:13:38 +08:00
|
|
|
})* }
|
2020-08-20 22:11:41 +08:00
|
|
|
};
|
2020-04-21 00:11:25 +08:00
|
|
|
}
|
|
|
|
|
2020-11-04 12:34:34 +08:00
|
|
|
#[cfg(any(
|
|
|
|
not(feature = "no_float"),
|
|
|
|
all(not(feature = "only_i32"), not(feature = "only_i64"))
|
|
|
|
))]
|
2020-08-14 13:43:26 +08:00
|
|
|
macro_rules! reg_functions {
|
2020-08-24 22:37:44 +08:00
|
|
|
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
|
2020-09-13 22:12:11 +08:00
|
|
|
combine_with_exported_module!($mod_name, "logic", $root::$arg_type::functions);
|
2020-08-24 22:37:44 +08:00
|
|
|
)* }
|
2020-04-21 00:11:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-20 11:42:39 +08:00
|
|
|
def_package! {
|
|
|
|
/// Package of basic logic operators.
|
|
|
|
crate::LogicPackage => |lib| {
|
|
|
|
lib.standard = true;
|
2021-11-05 23:22:05 +08:00
|
|
|
|
2021-12-20 11:42:39 +08:00
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
|
|
|
{
|
|
|
|
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64);
|
2020-06-17 16:50:46 +08:00
|
|
|
|
2022-01-12 08:12:28 +08:00
|
|
|
#[cfg(not(target_family = "wasm"))]
|
|
|
|
|
2021-12-20 11:42:39 +08:00
|
|
|
reg_functions!(lib += num_128; i128, u128);
|
|
|
|
}
|
2020-04-21 00:11:25 +08:00
|
|
|
|
2021-12-20 11:42:39 +08:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
|
|
|
#[cfg(not(feature = "f32_float"))]
|
|
|
|
reg_functions!(lib += float; f32);
|
|
|
|
combine_with_exported_module!(lib, "f32", f32_functions);
|
2020-11-04 12:34:34 +08:00
|
|
|
|
2021-12-20 11:42:39 +08:00
|
|
|
#[cfg(feature = "f32_float")]
|
|
|
|
reg_functions!(lib += float; f64);
|
|
|
|
combine_with_exported_module!(lib, "f64", f64_functions);
|
|
|
|
}
|
2022-01-20 08:16:38 +08:00
|
|
|
|
|
|
|
combine_with_exported_module!(lib, "logic", logic_functions);
|
2021-12-20 11:42:39 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-14 13:43:26 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
2020-08-20 22:11:41 +08:00
|
|
|
gen_cmp_functions!(numbers => i8, u8, i16, u16, i32, u32, u64);
|
2020-08-14 13:43:26 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "only_i32"))]
|
|
|
|
#[cfg(not(feature = "only_i64"))]
|
2022-01-12 08:12:28 +08:00
|
|
|
#[cfg(not(target_family = "wasm"))]
|
|
|
|
|
2020-08-20 22:11:41 +08:00
|
|
|
gen_cmp_functions!(num_128 => i128, u128);
|
2020-08-14 13:43:26 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
2020-11-04 12:34:34 +08:00
|
|
|
#[cfg(not(feature = "f32_float"))]
|
2020-08-20 22:11:41 +08:00
|
|
|
gen_cmp_functions!(float => f32);
|
2020-11-04 12:34:34 +08:00
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[cfg(feature = "f32_float")]
|
|
|
|
gen_cmp_functions!(float => f64);
|
2021-02-13 23:01:34 +08:00
|
|
|
|
2022-01-20 08:16:38 +08:00
|
|
|
#[export_module]
|
|
|
|
mod logic_functions {
|
|
|
|
#[rhai_fn(name = "!")]
|
|
|
|
pub fn not(x: bool) -> bool {
|
|
|
|
!x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:33:27 +08:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[export_module]
|
|
|
|
mod f32_functions {
|
2022-01-07 12:19:01 +08:00
|
|
|
use crate::INT;
|
|
|
|
|
2021-02-18 14:33:27 +08:00
|
|
|
#[rhai_fn(name = "==")]
|
|
|
|
pub fn eq_if(x: INT, y: f32) -> bool {
|
|
|
|
(x as f32) == (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "==")]
|
|
|
|
pub fn eq_fi(x: f32, y: INT) -> bool {
|
|
|
|
(x as f32) == (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "!=")]
|
|
|
|
pub fn neq_if(x: INT, y: f32) -> bool {
|
|
|
|
(x as f32) != (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "!=")]
|
|
|
|
pub fn neq_fi(x: f32, y: INT) -> bool {
|
|
|
|
(x as f32) != (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">")]
|
|
|
|
pub fn gt_if(x: INT, y: f32) -> bool {
|
|
|
|
(x as f32) > (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">")]
|
|
|
|
pub fn gt_fi(x: f32, y: INT) -> bool {
|
|
|
|
(x as f32) > (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">=")]
|
|
|
|
pub fn gte_if(x: INT, y: f32) -> bool {
|
|
|
|
(x as f32) >= (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">=")]
|
|
|
|
pub fn gte_fi(x: f32, y: INT) -> bool {
|
|
|
|
(x as f32) >= (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<")]
|
|
|
|
pub fn lt_if(x: INT, y: f32) -> bool {
|
|
|
|
(x as f32) < (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<")]
|
|
|
|
pub fn lt_fi(x: f32, y: INT) -> bool {
|
|
|
|
(x as f32) < (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<=")]
|
|
|
|
pub fn lte_if(x: INT, y: f32) -> bool {
|
|
|
|
(x as f32) <= (y as f32)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<=")]
|
|
|
|
pub fn lte_fi(x: f32, y: INT) -> bool {
|
|
|
|
(x as f32) <= (y as f32)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
#[export_module]
|
|
|
|
mod f64_functions {
|
2022-01-07 12:19:01 +08:00
|
|
|
use crate::INT;
|
|
|
|
|
2021-02-18 14:33:27 +08:00
|
|
|
#[rhai_fn(name = "==")]
|
|
|
|
pub fn eq_if(x: INT, y: f64) -> bool {
|
|
|
|
(x as f64) == (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "==")]
|
|
|
|
pub fn eq_fi(x: f64, y: INT) -> bool {
|
|
|
|
(x as f64) == (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "!=")]
|
|
|
|
pub fn neq_if(x: INT, y: f64) -> bool {
|
|
|
|
(x as f64) != (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "!=")]
|
|
|
|
pub fn neq_fi(x: f64, y: INT) -> bool {
|
|
|
|
(x as f64) != (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">")]
|
|
|
|
pub fn gt_if(x: INT, y: f64) -> bool {
|
|
|
|
(x as f64) > (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">")]
|
|
|
|
pub fn gt_fi(x: f64, y: INT) -> bool {
|
|
|
|
(x as f64) > (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">=")]
|
|
|
|
pub fn gte_if(x: INT, y: f64) -> bool {
|
|
|
|
(x as f64) >= (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = ">=")]
|
|
|
|
pub fn gte_fi(x: f64, y: INT) -> bool {
|
|
|
|
(x as f64) >= (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<")]
|
|
|
|
pub fn lt_if(x: INT, y: f64) -> bool {
|
|
|
|
(x as f64) < (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<")]
|
|
|
|
pub fn lt_fi(x: f64, y: INT) -> bool {
|
|
|
|
(x as f64) < (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<=")]
|
|
|
|
pub fn lte_if(x: INT, y: f64) -> bool {
|
|
|
|
(x as f64) <= (y as f64)
|
|
|
|
}
|
|
|
|
#[rhai_fn(name = "<=")]
|
|
|
|
pub fn lte_fi(x: f64, y: INT) -> bool {
|
|
|
|
(x as f64) <= (y as f64)
|
|
|
|
}
|
|
|
|
}
|