rhai/src/packages/logic.rs

88 lines
2.6 KiB
Rust
Raw Normal View History

2020-04-21 17:01:10 +02:00
use crate::def_package;
2020-08-14 07:43:26 +02:00
use crate::plugin::*;
2020-04-24 06:39:24 +02:00
2020-08-14 07:43:26 +02:00
macro_rules! gen_cmp_functions {
2020-08-20 16:11:41 +02:00
($root:ident => $($arg_type:ident),+) => {
mod $root { $(
2020-08-14 07:43:26 +02:00
pub mod $arg_type {
use crate::plugin::*;
2020-08-20 16:11:41 +02:00
#[export_module]
pub mod functions {
#[rhai_fn(name = "<")]
#[inline(always)]
pub fn lt(x: $arg_type, y: $arg_type) -> bool {
x < y
}
#[rhai_fn(name = "<=")]
#[inline(always)]
pub fn lte(x: $arg_type, y: $arg_type) -> bool {
x <= y
}
#[rhai_fn(name = ">")]
#[inline(always)]
pub fn gt(x: $arg_type, y: $arg_type) -> bool {
x > y
}
#[rhai_fn(name = ">=")]
#[inline(always)]
pub fn gte(x: $arg_type, y: $arg_type) -> bool {
x >= y
}
#[rhai_fn(name = "==")]
#[inline(always)]
pub fn eq(x: $arg_type, y: $arg_type) -> bool {
x == y
}
#[rhai_fn(name = "!=")]
#[inline(always)]
pub fn ne(x: $arg_type, y: $arg_type) -> bool {
x != y
}
2020-08-14 07:43:26 +02:00
}
}
)* }
2020-08-20 16:11:41 +02:00
};
}
2020-08-14 07:43:26 +02:00
macro_rules! reg_functions {
2020-08-24 16:37:44 +02:00
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
$mod_name.combine_flatten(exported_module!($root::$arg_type::functions));
)* }
}
2020-04-22 08:55:40 +02:00
def_package!(crate:LogicPackage:"Logical operators.", lib, {
2020-08-14 07:43:26 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
2020-08-20 16:11:41 +02:00
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, u32, u64);
2020-06-17 10:50:46 +02:00
2020-08-14 07:43:26 +02:00
#[cfg(not(target_arch = "wasm32"))]
2020-08-20 16:11:41 +02:00
reg_functions!(lib += num_128; i128, u128);
}
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "no_float"))]
2020-08-20 16:11:41 +02:00
reg_functions!(lib += float; f32);
2020-08-14 07:43:26 +02:00
set_exported_fn!(lib, "!", not);
2020-04-21 17:01:10 +02:00
});
2020-08-14 07:43:26 +02:00
// Logic operators
#[export_fn]
#[inline(always)]
fn not(x: bool) -> bool {
!x
}
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
2020-08-20 16:11:41 +02:00
gen_cmp_functions!(numbers => i8, u8, i16, u16, i32, u32, u64);
2020-08-14 07:43:26 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))]
2020-08-20 16:11:41 +02:00
gen_cmp_functions!(num_128 => i128, u128);
2020-08-14 07:43:26 +02:00
#[cfg(not(feature = "no_float"))]
2020-08-20 16:11:41 +02:00
gen_cmp_functions!(float => f32);