Refactor.

This commit is contained in:
Stephen Chung 2020-08-24 22:37:44 +08:00
parent 2fbc1b7910
commit 9ab3d87cfc
6 changed files with 85 additions and 74 deletions

View File

@ -265,9 +265,9 @@ macro_rules! gen_signed_functions {
}
macro_rules! reg_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+ ) => {
$($mod_name.combine_flatten(exported_module!($root::$arg_type::functions));)*
}
($mod_name:ident += $root:ident ; $($arg_type:ident),+ ) => { $(
$mod_name.combine_flatten(exported_module!($root::$arg_type::functions));
)* }
}
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
@ -288,7 +288,10 @@ def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
// Basic arithmetic for floating-point
#[cfg(not(feature = "no_float"))]
lib.combine_flatten(exported_module!(float_functions));
{
lib.combine_flatten(exported_module!(f32_functions));
lib.combine_flatten(exported_module!(f64_functions));
}
});
gen_arithmetic_functions!(arith_basic => INT);
@ -315,7 +318,7 @@ gen_signed_functions!(signed_num_128 => i128);
#[cfg(not(feature = "no_float"))]
#[export_module]
mod float_functions {
mod f32_functions {
#[rhai_fn(name = "+")]
#[inline(always)]
pub fn add(x: f32, y: f32) -> f32 {
@ -343,38 +346,15 @@ mod float_functions {
}
#[rhai_fn(name = "-")]
#[inline(always)]
pub fn neg_f32(x: f32) -> f32 {
pub fn neg(x: f32) -> f32 {
-x
}
#[rhai_fn(name = "-")]
#[inline(always)]
pub fn neg_f64(x: f64) -> f64 {
-x
}
#[rhai_fn(name = "abs")]
#[inline(always)]
pub fn abs_f32(x: f32) -> f32 {
pub fn abs(x: f32) -> f32 {
x.abs()
}
#[rhai_fn(name = "abs")]
#[inline(always)]
pub fn abs_f64(x: f64) -> f64 {
x.abs()
}
#[rhai_fn(name = "sign")]
#[inline]
pub fn sign_f32(x: f32) -> INT {
if x == 0.0 {
0
} else if x < 0.0 {
-1
} else {
1
}
}
#[rhai_fn(name = "sign")]
#[inline]
pub fn sign_f64(x: f64) -> INT {
pub fn sign(x: f32) -> INT {
if x == 0.0 {
0
} else if x < 0.0 {
@ -385,8 +365,45 @@ mod float_functions {
}
#[rhai_fn(name = "~", return_raw)]
#[inline(always)]
pub fn pow_f_f(x: FLOAT, y: FLOAT) -> Result<Dynamic, Box<EvalAltResult>> {
Ok(x.powf(y).into())
pub fn pow_f_f(x: f32, y: f32) -> Result<Dynamic, Box<EvalAltResult>> {
Ok(Dynamic::from(x.powf(y)))
}
#[rhai_fn(name = "~", return_raw)]
#[inline]
pub fn pow_f_i(x: f32, y: INT) -> Result<Dynamic, Box<EvalAltResult>> {
if cfg!(not(feature = "unchecked")) && y > (i32::MAX as INT) {
EvalAltResult::ErrorArithmetic(
format!("Number raised to too large an index: {} ~ {}", x, y),
Position::none(),
)
.into()
} else {
Ok(Dynamic::from(x.powi(y as i32)))
}
}
}
#[cfg(not(feature = "no_float"))]
#[export_module]
mod f64_functions {
#[rhai_fn(name = "-")]
#[inline(always)]
pub fn neg(x: f64) -> f64 {
-x
}
#[inline(always)]
pub fn abs(x: f64) -> f64 {
x.abs()
}
#[inline]
pub fn sign(x: f64) -> INT {
if x == 0.0 {
0
} else if x < 0.0 {
-1
} else {
1
}
}
#[rhai_fn(name = "~", return_raw)]
#[inline]

View File

@ -11,6 +11,9 @@ use crate::plugin::*;
#[cfg(not(feature = "unchecked"))]
use crate::{result::EvalAltResult, token::Position};
#[cfg(not(feature = "no_object"))]
use crate::engine::Map;
use crate::stdlib::{any::TypeId, boxed::Box};
#[cfg(not(feature = "unchecked"))]
@ -41,47 +44,35 @@ macro_rules! gen_array_functions {
}
macro_rules! reg_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
$(set_exported_fn!($mod_name, "push", $root::$arg_type::push_func);)*
$(set_exported_fn!($mod_name, "insert", $root::$arg_type::insert_func);)*
}
}
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
set_exported_fn!($mod_name, "push", $root::$arg_type::push_func);
set_exported_fn!($mod_name, "insert", $root::$arg_type::insert_func);
macro_rules! reg_pad {
($lib:expr, $($par:ty),*) => {
$({
$lib.set_raw_fn("pad",
&[TypeId::of::<Array>(), TypeId::of::<INT>(), TypeId::of::<$par>()],
pad::<$par>
);
})*
};
$mod_name.set_raw_fn("pad",
&[TypeId::of::<Array>(), TypeId::of::<INT>(), TypeId::of::<$arg_type>()],
pad::<$arg_type>);
)* }
}
def_package!(crate:BasicArrayPackage:"Basic array utilities.", lib, {
lib.combine_flatten(exported_module!(array_functions));
reg_functions!(lib += basic; INT, bool, char, ImmutableString, FnPtr, Array, Unit);
reg_pad!(lib, INT, bool, char, ImmutableString, FnPtr, Array, Unit);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_functions!(lib += numbers; i8, u8, i16, u16, i32, i64, u32, u64);
reg_pad!(lib, u8, i16, u16, i32, u32, i64, u64);
#[cfg(not(target_arch = "wasm32"))]
{
reg_functions!(lib += num_128; i128, u128);
reg_pad!(lib, i128, u128);
}
}
#[cfg(not(feature = "no_float"))]
{
reg_functions!(lib += float; f32, f64);
reg_pad!(lib, f32, f64);
}
#[cfg(not(feature = "no_object"))]
reg_functions!(lib += map; Map);
// Register array iterator
lib.set_iter(
@ -207,3 +198,6 @@ gen_array_functions!(num_128 => i128, u128);
#[cfg(not(feature = "no_float"))]
gen_array_functions!(float => f32, f64);
#[cfg(not(feature = "no_object"))]
gen_array_functions!(map => Map);

View File

@ -46,9 +46,9 @@ macro_rules! gen_cmp_functions {
}
macro_rules! reg_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
$($mod_name.combine_flatten(exported_module!($root::$arg_type::functions));)*
}
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
$mod_name.combine_flatten(exported_module!($root::$arg_type::functions));
)* }
}
def_package!(crate:LogicPackage:"Logical operators.", lib, {

View File

@ -41,9 +41,9 @@ macro_rules! gen_conversion_functions {
}
macro_rules! reg_functions {
($mod_name:ident += $root:ident :: $func_name:ident ( $($arg_type:ident),+ ) ) => {
$(set_exported_fn!($mod_name, stringify!($func_name), $root::$arg_type::$func_name);)*
}
($mod_name:ident += $root:ident :: $func_name:ident ( $($arg_type:ident),+ ) ) => { $(
set_exported_fn!($mod_name, stringify!($func_name), $root::$arg_type::$func_name);
)* }
}
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, {

View File

@ -37,16 +37,16 @@ macro_rules! gen_functions {
}
macro_rules! reg_print_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
$(set_exported_fn!($mod_name, FN_TO_STRING, $root::$arg_type::to_string_func);)*
$(set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func);)*
}
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
set_exported_fn!($mod_name, FN_TO_STRING, $root::$arg_type::to_string_func);
set_exported_fn!($mod_name, KEYWORD_PRINT, $root::$arg_type::to_string_func);
)* }
}
macro_rules! reg_debug_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
$(set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func);)*
}
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
set_exported_fn!($mod_name, KEYWORD_DEBUG, $root::$arg_type::to_string_func);
)* }
}
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {

View File

@ -38,10 +38,10 @@ macro_rules! gen_concat_functions {
}
macro_rules! reg_functions {
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => {
$(set_exported_fn!($mod_name, "+", $root::$arg_type::append_func);)*
$(set_exported_fn!($mod_name, "+", $root::$arg_type::prepend_func);)*
}
($mod_name:ident += $root:ident ; $($arg_type:ident),+) => { $(
set_exported_fn!($mod_name, "+", $root::$arg_type::append_func);
set_exported_fn!($mod_name, "+", $root::$arg_type::prepend_func);
)* }
}
def_package!(crate:MoreStringPackage:"Additional string utilities, including string building.", lib, {