numext_fixed_uint_hack/
lib.rsextern crate nfuint_core;
extern crate proc_macro;
use quote::quote;
use syn::parse_macro_input;
macro_rules! impl_func {
($(($name:ident, $type:ident),)+) => {
$(impl_func!($name, $type);)+
};
($(($name:ident, $type:ident)),+) => {
$(impl_func!($name, $type);)+
};
($name:ident, $type:ident) => {
#[proc_macro]
pub fn $name(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as syn::LitStr);
let expanded = {
let input = input.value().replace("_", "");
if input.is_empty() {
panic!("Input is empty.");
}
let (value_result, input_type) = if input.len() < 3 {
(nfuint_core::$type::from_dec_str(&input), "decimal")
} else {
match &input[..2] {
"0b" => (nfuint_core::$type::from_bin_str(&input[2..]), "binary"),
"0o" => (nfuint_core::$type::from_oct_str(&input[2..]), "octal"),
"0x" => (nfuint_core::$type::from_hex_str(&input[2..]), "hexadecimal"),
_ => (nfuint_core::$type::from_dec_str(&input), "decimal"),
}
};
let value = value_result.unwrap_or_else(|err| {
panic!("Failed to parse the input {} string: {}", input_type, err);
});
let eval_str = format!("{:?}", value);
let eval_ts: proc_macro2::TokenStream = eval_str.parse().unwrap_or_else(|_| {
panic!("Failed to parse the string [{}] to TokenStream.", eval_str);
});
quote!(#eval_ts)
};
expanded.into()
}
};
}
impl_func!(
(u128, U128),
(u160, U160),
(u224, U224),
(u256, U256),
(u384, U384),
(u512, U512),
(u520, U520),
(u1024, U1024),
(u2048, U2048),
(u4096, U4096),
);