I am trying to write a procedural macro.
#[proc_macro_attribute]
pub fn evaluate(args: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let stream = proc_macro2::TokenStream::from(args);
let operator = stream.into_iter().next().unwrap();
let result = quote! {
struct #name {}
impl Eval for #name {
fn evaluate(obj1: Value, obj2: Value) -> bool {
if obj1.is_i64() {
return obj1.as_i64() #operator obj2.as_i64();
}
return false;
};
}
};
result.into()
}
cargo expand shows that this is what get generated
struct GreaterThan {}
impl Eval for GreaterThan {
fn evaluate(obj1: Value, obj2: Value) -> bool {
if obj1.is_i64() {
return obj1.as_i64() > obj2.as_i64();
}
return false;
}
}
But when I compile I get this strange error
5 | #[evaluate(>)]
| ^^^^^^^^^^^^^^
| |
| item list starts here
| non-item starts here
| item list ends here
additionally if I use a double equals
#[evaluate(==)]
struct Equals {}
The code just uses a single equals in the result
return obj1.as_i64() = obj2.as_i64();
that is most probably because i am just using a single item int eh token tree with the .next() code, but how can I cater for single and double operators so this is still valid
return obj1.as_i64() #operator obj2.as_i64();
I know it's a bit of a mixed bag of questions here but I feel like I have lost my way and would greatly appreciate some help getting this macro up and running properly.