Use plugins to define functions.

This commit is contained in:
Stephen Chung 2020-10-19 23:49:01 +08:00
parent ccba5f2188
commit 13f1ae577b
4 changed files with 101 additions and 106 deletions

View File

@ -45,12 +45,6 @@ where
Ok(StepRange::<T>(from, to, step)) Ok(StepRange::<T>(from, to, step))
} }
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
lib.set_iterator::<Range<INT>>();
lib.set_fn_2("range", get_range::<INT>);
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) {
macro_rules! reg_range { macro_rules! reg_range {
($lib:expr, $x:expr, $( $y:ty ),*) => ( ($lib:expr, $x:expr, $( $y:ty ),*) => (
$( $(
@ -60,6 +54,22 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
) )
} }
macro_rules! reg_step {
($lib:expr, $x:expr, $( $y:ty ),*) => (
$(
$lib.set_iterator::<StepRange<$y>>();
$lib.set_fn_3($x, get_step_range::<$y>);
)*
)
}
def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
lib.set_iterator::<Range<INT>>();
lib.set_fn_2("range", get_range::<INT>);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64); reg_range!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
if cfg!(not(target_arch = "wasm32")) { if cfg!(not(target_arch = "wasm32")) {
@ -70,16 +80,9 @@ def_package!(crate:BasicIteratorPackage:"Basic range iterators.", lib, {
lib.set_iterator::<StepRange<INT>>(); lib.set_iterator::<StepRange<INT>>();
lib.set_fn_3("range", get_step_range::<INT>); lib.set_fn_3("range", get_step_range::<INT>);
if cfg!(not(feature = "only_i32")) && cfg!(not(feature = "only_i64")) { #[cfg(not(feature = "only_i32"))]
macro_rules! reg_step { #[cfg(not(feature = "only_i64"))]
($lib:expr, $x:expr, $( $y:ty ),*) => ( {
$(
$lib.set_iterator::<StepRange<$y>>();
$lib.set_fn_3($x, get_step_range::<$y>);
)*
)
}
reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64); reg_step!(lib, "range", i8, u8, i16, u16, i32, i64, u32, u64);
if cfg!(not(target_arch = "wasm32")) { if cfg!(not(target_arch = "wasm32")) {

View File

@ -45,6 +45,20 @@ macro_rules! reg_functions {
} }
def_package!(crate:BasicMathPackage:"Basic mathematic functions.", lib, { 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"))] #[cfg(not(feature = "no_float"))]
{ {
// Floating point functions // 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 += 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<Dynamic, Box<EvalAltResult>> {
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::<Dynamic>::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<Dynamic, Box<EvalAltResult>> {
parse_int_radix(s, 10)
}
}
#[cfg(not(feature = "no_float"))] #[cfg(not(feature = "no_float"))]
#[export_module] #[export_module]
mod trig_functions { mod trig_functions {
@ -199,7 +227,6 @@ mod float_functions {
Ok((x.trunc() as INT).into()) Ok((x.trunc() as INT).into())
} }
} }
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn parse_float(s: &str) -> Result<Dynamic, Box<EvalAltResult>> { pub fn parse_float(s: &str) -> Result<Dynamic, Box<EvalAltResult>> {
s.trim() 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(feature = "only_i64"))]
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
gen_conversion_functions!(num_128_to_int => to_int (i128, u128) -> INT); 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<Dynamic, Box<EvalAltResult>> {
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::<Dynamic>::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<Dynamic, Box<EvalAltResult>> {
parse_int_radix(s, 10)
}

View File

@ -47,16 +47,10 @@ macro_rules! reg_debug_functions {
} }
def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, { def_package!(crate:BasicStringPackage:"Basic string utilities, including printing.", lib, {
reg_print_functions!(lib += print_basic; INT, bool, char, FnPtr); combine_with_exported_module!(lib, "print_debug", print_debug_functions);
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);
reg_print_functions!(lib += print_basic; INT, bool, char, FnPtr);
reg_debug_functions!(lib += debug_basic; INT, bool, Unit, char, ImmutableString); 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_i32"))]
#[cfg(not(feature = "only_i64"))] #[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_print_functions!(lib += print_array; Array);
reg_debug_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<T: Display>(x: &mut T) -> ImmutableString {
x.to_string().into()
}
fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
format!("{:?}", x).into()
}
gen_functions!(print_basic => to_string(INT, bool, char, FnPtr)); gen_functions!(print_basic => to_string(INT, bool, char, FnPtr));
gen_functions!(debug_basic => to_debug(INT, bool, Unit, char, ImmutableString)); 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)); gen_functions!(print_array => to_debug(Array));
// Register print and debug // Register print and debug
#[export_fn] #[export_module]
fn print_empty_string() -> ImmutableString { mod print_debug_functions {
#[rhai_fn(name = "print", name = "debug")]
pub fn print_empty_string() -> ImmutableString {
"".to_string().into() "".to_string().into()
} }
#[export_fn] #[rhai_fn(name = "print", name = "to_string")]
fn print_unit(_x: ()) -> ImmutableString { pub fn print_unit(_x: ()) -> ImmutableString {
"".to_string().into() "".to_string().into()
} }
#[export_fn] #[rhai_fn(name = "print", name = "to_string")]
fn print_string(s: ImmutableString) -> ImmutableString { pub fn print_string(s: ImmutableString) -> ImmutableString {
s s
} }
#[export_fn] #[rhai_fn(name = "debug")]
fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString { pub fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
to_string(f) to_string(f)
} }
fn to_string<T: Display>(x: &mut T) -> ImmutableString {
x.to_string().into()
}
fn to_debug<T: Debug>(x: &mut T) -> ImmutableString {
format!("{:?}", x).into()
}
#[cfg(not(feature = "no_object"))] #[cfg(not(feature = "no_object"))]
mod format_map { pub mod map_functions {
use super::*; use super::*;
#[export_fn] #[rhai_fn(name = "print", name = "debug", name = "to_string")]
pub fn format_map(x: &mut Map) -> ImmutableString { pub fn format_map(x: &mut Map) -> ImmutableString {
format!("#{:?}", x).into() format!("#{:?}", x).into()
} }
} }
}

View File

@ -247,6 +247,7 @@ mod string_functions {
pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) { pub fn replace_char(s: &mut ImmutableString, find: char, sub: char) {
*s = s.replace(&find.to_string(), &sub.to_string()).into(); *s = s.replace(&find.to_string(), &sub.to_string()).into();
} }
#[rhai_fn(return_raw)] #[rhai_fn(return_raw)]
pub fn pad( pub fn pad(
_context: NativeCallContext, _context: NativeCallContext,
@ -363,7 +364,6 @@ mod string_functions {
pub fn prepend(x: &mut Array, y: &str) -> String { pub fn prepend(x: &mut Array, y: &str) -> String {
format!("{:?}{}", x, y) format!("{:?}{}", x, y)
} }
pub fn split(s: &str, delimiter: ImmutableString) -> Array { pub fn split(s: &str, delimiter: ImmutableString) -> Array {
s.split(delimiter.as_str()) s.split(delimiter.as_str())
.map(Into::<Dynamic>::into) .map(Into::<Dynamic>::into)