From 13f1ae577b6b2883302c911a3cd597e8dba26785 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Mon, 19 Oct 2020 23:49:01 +0800 Subject: [PATCH] Use plugins to define functions. --- src/packages/iter_basic.rs | 45 ++++++++++--------- src/packages/math_basic.rs | 83 ++++++++++++++++++------------------ src/packages/string_basic.rs | 77 +++++++++++++++------------------ src/packages/string_more.rs | 2 +- 4 files changed, 101 insertions(+), 106 deletions(-) diff --git a/src/packages/iter_basic.rs b/src/packages/iter_basic.rs index 21dc8e86..46ab0119 100644 --- a/src/packages/iter_basic.rs +++ b/src/packages/iter_basic.rs @@ -45,21 +45,31 @@ where Ok(StepRange::(from, to, step)) } +macro_rules! reg_range { + ($lib:expr, $x:expr, $( $y:ty ),*) => ( + $( + $lib.set_iterator::>(); + $lib.set_fn_2($x, get_range::<$y>); + )* + ) +} + +macro_rules! reg_step { + ($lib:expr, $x:expr, $( $y:ty ),*) => ( + $( + $lib.set_iterator::>(); + $lib.set_fn_3($x, get_step_range::<$y>); + )* + ) +} + def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { lib.set_iterator::>(); - lib.set_fn_2("range", get_range::); - if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { - macro_rules! reg_range { - ($lib:expr, $x:expr, $( $y:ty ),*) => ( - $( - $lib.set_iterator::>(); - $lib.set_fn_2($x, get_range::<$y>); - )* - ) - } - + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64); if cfg!(not(target_arch = "wasm32")) { @@ -70,16 +80,9 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, { lib.set_iterator::>(); lib.set_fn_3("range", get_step_range::); - if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { - macro_rules! reg_step { - ($lib:expr, $x:expr, $( $y:ty ),*) => ( - $( - $lib.set_iterator::>(); - $lib.set_fn_3($x, get_step_range::<$y>); - )* - ) - } - + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64); if cfg!(not(target_arch = "wasm32")) { diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index ae665b18..7b905ca9 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -45,6 +45,20 @@ macro_rules! reg_functions { } def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { + // Integer functions + combine_with_exported_module!(lib, "int", int_functions); + + reg_functions!(lib += basic_to_int::to_int(char)); + + #[cfg(not(feature = "only_i32"))] + #[cfg(not(feature = "only_i64"))] + { + reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64)); + + #[cfg(not(target_arch = "wasm32"))] + reg_functions!(lib += num_128_to_int::to_int(i128, u128)); + } + #[cfg(not(feature = "no_float"))] { // Floating point functions @@ -64,22 +78,36 @@ def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { reg_functions!(lib += num_128_to_float::to_float(i128, u128)); } } - - reg_functions!(lib += basic_to_int::to_int(char)); - - set_exported_fn!(lib, "parse_int", parse_int); - set_exported_fn!(lib, "parse_int", parse_int_radix); - - #[cfg(not(feature = "only_i32"))] - #[cfg(not(feature = "only_i64"))] - { - reg_functions!(lib += numbers_to_int::to_int(i8, u8, i16, u16, i32, u32, i64, u64)); - - #[cfg(not(target_arch = "wasm32"))] - reg_functions!(lib += num_128_to_int::to_int(i128, u128)); - } }); +#[export_module] +mod int_functions { + #[rhai_fn(name = "parse_int", return_raw)] + pub fn parse_int_radix(s: &str, radix: INT) -> Result> { + if radix < 2 || radix > 36 { + return EvalAltResult::ErrorArithmetic( + format!("Invalid radix: '{}'", radix), + Position::none(), + ) + .into(); + } + + INT::from_str_radix(s.trim(), radix as u32) + .map(Into::::into) + .map_err(|err| { + EvalAltResult::ErrorArithmetic( + format!("Error parsing integer number '{}': {}", s, err), + Position::none(), + ) + .into() + }) + } + #[rhai_fn(name = "parse_int", return_raw)] + pub fn parse_int(s: &str) -> Result> { + parse_int_radix(s, 10) + } +} + #[cfg(not(feature = "no_float"))] #[export_module] mod trig_functions { @@ -199,7 +227,6 @@ mod float_functions { Ok((x.trunc() as INT).into()) } } - #[rhai_fn(return_raw)] pub fn parse_float(s: &str) -> Result> { s.trim() @@ -239,29 +266,3 @@ gen_conversion_functions!(numbers_to_int => to_int (i8, u8, i16, u16, i32, u32, #[cfg(not(feature = "only_i64"))] #[cfg(not(target_arch = "wasm32"))] gen_conversion_functions!(num_128_to_int => to_int (i128, u128) -> INT); - -#[export_fn(return_raw)] -fn parse_int_radix(s: &str, radix: INT) -> Result> { - if radix < 2 || radix > 36 { - return EvalAltResult::ErrorArithmetic( - format!("Invalid radix: '{}'", radix), - Position::none(), - ) - .into(); - } - - INT::from_str_radix(s.trim(), radix as u32) - .map(Into::::into) - .map_err(|err| { - EvalAltResult::ErrorArithmetic( - format!("Error parsing integer number '{}': {}", s, err), - Position::none(), - ) - .into() - }) -} - -#[export_fn(return_raw)] -fn parse_int(s: &str) -> Result> { - parse_int_radix(s, 10) -} diff --git a/src/packages/string_basic.rs b/src/packages/string_basic.rs index 2733532d..988e1dac 100644 --- a/src/packages/string_basic.rs +++ b/src/packages/string_basic.rs @@ -47,16 +47,10 @@ macro_rules! reg_debug_functions { } def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, { - reg_print_functions!(lib += print_basic; INT, bool, char, FnPtr); - set_exported_fn!(lib, KEYWORD_PRINT, print_empty_string); - set_exported_fn!(lib, KEYWORD_PRINT, print_unit); - set_exported_fn!(lib, FN_TO_STRING, print_unit); - set_exported_fn!(lib, KEYWORD_PRINT, print_string); - set_exported_fn!(lib, FN_TO_STRING, print_string); + combine_with_exported_module!(lib, "print_debug", print_debug_functions); + reg_print_functions!(lib += print_basic; INT, bool, char, FnPtr); reg_debug_functions!(lib += debug_basic; INT, bool, Unit, char, ImmutableString); - set_exported_fn!(lib, KEYWORD_DEBUG, print_empty_string); - set_exported_fn!(lib, KEYWORD_DEBUG, debug_fn_ptr); #[cfg(not(feature = "only_i32"))] #[cfg(not(feature = "only_i64"))] @@ -82,15 +76,15 @@ def_package!(crate:BasicStringPackage:"Basic string utilities, including printin reg_print_functions!(lib += print_array; Array); reg_debug_functions!(lib += print_array; Array); } - - #[cfg(not(feature = "no_object"))] - { - set_exported_fn!(lib, KEYWORD_PRINT, format_map::format_map); - set_exported_fn!(lib, FN_TO_STRING, format_map::format_map); - set_exported_fn!(lib, KEYWORD_DEBUG, format_map::format_map); - } }); +fn to_string(x: &mut T) -> ImmutableString { + x.to_string().into() +} +fn to_debug(x: &mut T) -> ImmutableString { + format!("{:?}", x).into() +} + gen_functions!(print_basic => to_string(INT, bool, char, FnPtr)); gen_functions!(debug_basic => to_debug(INT, bool, Unit, char, ImmutableString)); @@ -122,35 +116,32 @@ gen_functions!(debug_float => to_debug(f32, f64)); gen_functions!(print_array => to_debug(Array)); // Register print and debug -#[export_fn] -fn print_empty_string() -> ImmutableString { - "".to_string().into() -} -#[export_fn] -fn print_unit(_x: ()) -> ImmutableString { - "".to_string().into() -} -#[export_fn] -fn print_string(s: ImmutableString) -> ImmutableString { - s -} -#[export_fn] -fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString { - to_string(f) -} -fn to_string(x: &mut T) -> ImmutableString { - x.to_string().into() -} -fn to_debug(x: &mut T) -> ImmutableString { - format!("{:?}", x).into() -} +#[export_module] +mod print_debug_functions { + #[rhai_fn(name = "print", name = "debug")] + pub fn print_empty_string() -> ImmutableString { + "".to_string().into() + } + #[rhai_fn(name = "print", name = "to_string")] + pub fn print_unit(_x: ()) -> ImmutableString { + "".to_string().into() + } + #[rhai_fn(name = "print", name = "to_string")] + pub fn print_string(s: ImmutableString) -> ImmutableString { + s + } + #[rhai_fn(name = "debug")] + pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString { + to_string(f) + } -#[cfg(not(feature = "no_object"))] -mod format_map { - use super::*; + #[cfg(not(feature = "no_object"))] + pub mod map_functions { + use super::*; - #[export_fn] - pub fn format_map(x: &mut Map) -> ImmutableString { - format!("#{:?}", x).into() + #[rhai_fn(name = "print", name = "debug", name = "to_string")] + pub fn format_map(x: &mut Map) -> ImmutableString { + format!("#{:?}", x).into() + } } } diff --git a/src/packages/string_more.rs b/src/packages/string_more.rs index 06217d11..1d515558 100644 --- a/src/packages/string_more.rs +++ b/src/packages/string_more.rs @@ -247,6 +247,7 @@ mod string_functions { pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) { *s = s.replace(&find.to_string(), &sub.to_string()).into(); } + #[rhai_fn(return_raw)] pub fn pad( _context: NativeCallContext, @@ -363,7 +364,6 @@ mod string_functions { pub fn prepend(x: &mut Array, y: &str) -> String { format!("{:?}{}", x, y) } - pub fn split(s: &str, delimiter: ImmutableString) -> Array { s.split(delimiter.as_str()) .map(Into::::into)