Nicer formatting/less indentation of module plugin blocks.

This commit is contained in:
Stephen Chung 2020-08-28 23:13:38 +08:00
parent b4406f2302
commit 8736919cea
6 changed files with 280 additions and 312 deletions

View File

@ -17,8 +17,7 @@ use crate::stdlib::format;
macro_rules! gen_arithmetic_functions {
($root:ident => $($arg_type:ident),+) => {
pub mod $root { $(
pub mod $arg_type {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_module]
@ -211,15 +210,13 @@ macro_rules! gen_arithmetic_functions {
x ^ y
}
}
}
)* }
})* }
}
}
macro_rules! gen_signed_functions {
($root:ident => $($arg_type:ident),+) => {
pub mod $root { $(
pub mod $arg_type {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_module]
@ -259,8 +256,7 @@ macro_rules! gen_signed_functions {
}
}
}
}
)* }
})* }
}
}

View File

@ -23,30 +23,33 @@ pub type Unit = ();
macro_rules! gen_array_functions {
($root:ident => $($arg_type:ident),+ ) => {
pub mod $root { $(
pub mod $arg_type {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_fn]
#[inline(always)]
pub fn push_func(list: &mut Array, item: $arg_type) {
super::super::push(list, item);
pub fn push(list: &mut Array, item: $arg_type) {
list.push(Dynamic::from(item));
}
#[export_fn]
#[inline(always)]
pub fn insert_func(list: &mut Array, len: INT, item: $arg_type) {
super::super::insert(list, len, item);
pub fn insert(list: &mut Array, position: INT, item: $arg_type) {
if position <= 0 {
list.insert(0, Dynamic::from(item));
} else if (position as usize) >= list.len() - 1 {
push(list, item);
} else {
list.insert(position as usize, Dynamic::from(item));
}
}
)* }
})* }
}
}
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);
set_exported_fn!($mod_name, "push", $root::$arg_type::push);
set_exported_fn!($mod_name, "insert", $root::$arg_type::insert);
$mod_name.set_raw_fn("pad",
&[TypeId::of::<Array>(), TypeId::of::<INT>(), TypeId::of::<$arg_type>()],
@ -138,20 +141,6 @@ mod array_functions {
}
}
// Register array utility functions
#[inline(always)]
fn push<T: Variant + Clone>(list: &mut Array, item: T) {
list.push(Dynamic::from(item));
}
fn insert<T: Variant + Clone>(list: &mut Array, position: INT, item: T) {
if position <= 0 {
list.insert(0, Dynamic::from(item));
} else if (position as usize) >= list.len() - 1 {
push(list, item);
} else {
list.insert(position as usize, Dynamic::from(item));
}
}
fn pad<T: Variant + Clone>(
_engine: &Engine,
_: &Module,

View File

@ -3,9 +3,8 @@ use crate::plugin::*;
macro_rules! gen_cmp_functions {
($root:ident => $($arg_type:ident),+) => {
mod $root { $(
pub mod $arg_type {
use crate::plugin::*;
mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_module]
pub mod functions {
@ -40,8 +39,7 @@ macro_rules! gen_cmp_functions {
x != y
}
}
}
)* }
})* }
};
}

View File

@ -26,8 +26,7 @@ pub const MAX_INT: INT = i64::MAX;
macro_rules! gen_conversion_functions {
($root:ident => $func_name:ident ( $($arg_type:ident),+ ) -> $result_type:ty) => {
pub mod $root { $(
pub mod $arg_type {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_fn]
@ -35,8 +34,7 @@ macro_rules! gen_conversion_functions {
pub fn $func_name(x: $arg_type) -> $result_type {
x as $result_type
}
}
)* }
})* }
}
}

View File

@ -22,8 +22,7 @@ type Unit = ();
macro_rules! gen_functions {
($root:ident => $fn_name:ident ( $($arg_type:ident),+ )) => {
pub mod $root { $(
pub mod $arg_type {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_fn]
@ -31,8 +30,7 @@ macro_rules! gen_functions {
pub fn to_string_func(x: &mut $arg_type) -> ImmutableString {
super::super::$fn_name(x)
}
}
)* }
})* }
}
}
@ -128,12 +126,12 @@ gen_functions!(print_array => to_debug(Array));
#[export_fn]
#[inline(always)]
fn print_empty_string() -> ImmutableString {
"".to_string().into()
String::new().into()
}
#[export_fn]
#[inline(always)]
fn print_unit(_x: ()) -> ImmutableString {
"".to_string().into()
String::new().into()
}
#[export_fn]
#[inline(always)]
@ -143,7 +141,7 @@ fn print_string(s: ImmutableString) -> ImmutableString {
#[export_fn]
#[inline(always)]
fn debug_fn_ptr(f: &mut FnPtr) -> ImmutableString {
f.to_string().into()
to_string(f)
}
#[inline(always)]
fn to_string<T: Display>(x: &mut T) -> ImmutableString {

View File

@ -12,28 +12,26 @@ use crate::utils::StaticVec;
use crate::{result::EvalAltResult, token::Position};
use crate::stdlib::{
any::TypeId, boxed::Box, fmt::Display, format, mem, string::String, string::ToString, vec::Vec,
any::TypeId, boxed::Box, format, mem, string::String, string::ToString, vec::Vec,
};
macro_rules! gen_concat_functions {
($root:ident => $($arg_type:ident),+ ) => {
pub mod $root { $(
pub mod $arg_type {
pub mod $root { $(pub mod $arg_type {
use super::super::*;
#[export_fn]
#[inline(always)]
#[inline]
pub fn append_func(x: &mut ImmutableString, y: $arg_type) -> String {
super::super::add_append(x, y)
format!("{}{}", x, y)
}
#[export_fn]
#[inline(always)]
#[inline]
pub fn prepend_func(x: &mut $arg_type, y: ImmutableString) -> String {
super::super::add_prepend(x, y)
format!("{}{}", x, y)
}
}
)* }
})* }
}
}
@ -116,15 +114,6 @@ def_package!(crate:MoreStringPackage:"Additional string utilities, including str
);
});
#[inline]
fn add_prepend<T: Display>(x: &mut T, y: ImmutableString) -> String {
format!("{}{}", x, y)
}
#[inline]
fn add_append<T: Display>(x: &mut ImmutableString, y: T) -> String {
format!("{}{}", x, y)
}
gen_concat_functions!(basic => INT, bool, char, FnPtr);
#[cfg(not(feature = "only_i32"))]