2024-01-12 19:11:33 +00:00
|
|
|
use proc_macro2::Ident;
|
|
|
|
use quote::quote;
|
|
|
|
use syn::{Path, Token};
|
|
|
|
|
|
|
|
pub(crate) struct MatWrapper {
|
|
|
|
pub column_type: Ident,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MatWrapper {
|
|
|
|
pub fn to_field_tokens(&self, wrapped_path: &Path, wrapper_ident: &Ident) -> proc_macro2::TokenStream {
|
|
|
|
quote! {
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.field("ZERO", #wrapper_ident(#wrapped_path::ZERO));
|
|
|
|
builder.field("IDENTITY", #wrapper_ident(#wrapped_path::IDENTITY));
|
|
|
|
builder.field("NAN", #wrapper_ident(#wrapped_path::NAN));
|
2024-01-12 19:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_method_tokens(&self, wrapped_path: &Path, wrapper_ident: &Ident) -> proc_macro2::TokenStream {
|
|
|
|
let column_type = &self.column_type;
|
|
|
|
|
|
|
|
let column_size = {
|
|
|
|
let ty_str = column_type.to_string();
|
|
|
|
ty_str[ty_str.len() - 1..].parse::<usize>()
|
|
|
|
.expect("Failure to parse number from token type")
|
|
|
|
};
|
|
|
|
let column_size_xtwo = column_size * 2;
|
|
|
|
|
|
|
|
let element_ty = quote!(f32);
|
|
|
|
|
|
|
|
quote! {
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.function("from_cols",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, (x_axis, y_axis): (#column_type, #column_type)| {
|
|
|
|
Ok(#wrapper_ident(#wrapped_path::from_cols(x_axis.0, y_axis.0)))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.function("from_cols_array",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, (arr,): ([#element_ty; #column_size_xtwo],)| {
|
|
|
|
Ok(#wrapper_ident(#wrapped_path::from_cols_array(&arr)))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.function("from_cols_array_2d",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, (arr,): ([[#element_ty; #column_size]; #column_size],)| {
|
|
|
|
Ok(#wrapper_ident(#wrapped_path::from_cols_array_2d(&arr)))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.function("from_diagonal",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, (diag,): (#column_type,)| {
|
|
|
|
Ok(#wrapper_ident(#wrapped_path::from_diagonal(diag.0)))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("col",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (idx,): (usize,)| {
|
|
|
|
Ok(#column_type(this.col(idx)))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("row",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (idx,): (usize,)| {
|
|
|
|
Ok(#column_type(this.row(idx)))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method_mut("set_col",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (idx, newc): (usize, #column_type)| {
|
|
|
|
let col = this.col_mut(idx);
|
|
|
|
*col = newc.0;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("is_finite",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (): ()| {
|
|
|
|
Ok(this.is_finite())
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("is_nan",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (): ()| {
|
|
|
|
Ok(this.is_nan())
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("transpose",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (): ()| {
|
|
|
|
Ok(#wrapper_ident(this.0.transpose()))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("determinant",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (): ()| {
|
|
|
|
Ok(this.determinant())
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("inverse",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (): ()| {
|
|
|
|
Ok(#wrapper_ident(this.inverse()))
|
|
|
|
});
|
|
|
|
|
2024-02-20 04:14:38 +00:00
|
|
|
builder.method("abs_diff_eq",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (rhs, max_abs_diff): (#wrapper_ident, f32)| {
|
|
|
|
Ok(this.abs_diff_eq(rhs.0, max_abs_diff))
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: After all DMat's are implemented
|
2024-02-20 04:14:38 +00:00
|
|
|
/* builder.method("as_dmat",
|
2024-01-12 19:11:33 +00:00
|
|
|
|_, this, (rhs, max_abs_diff): (#wrapper_ident, f32)| {
|
|
|
|
Ok(D#wrapper_ident(this.as_dmat))
|
|
|
|
}); */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl syn::parse::Parse for MatWrapper {
|
|
|
|
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
|
|
|
let mut column_type = None;
|
|
|
|
|
|
|
|
// cba to remove the use of this bool
|
|
|
|
let mut first = true;
|
|
|
|
|
|
|
|
while input.peek(Token![,]) || first {
|
|
|
|
if !first {
|
|
|
|
let _: Token![,] = input.parse()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.peek(syn::Ident) {
|
|
|
|
let ident: Ident = input.parse()?;
|
|
|
|
let ident_str = ident.to_string();
|
|
|
|
let ident_str = ident_str.as_str();
|
|
|
|
|
|
|
|
match ident_str {
|
|
|
|
"col_type" => {
|
|
|
|
let _eq: Token![=] = input.parse()?;
|
|
|
|
column_type = Some(input.parse()?);
|
|
|
|
},
|
|
|
|
_ => return Err(syn::Error::new_spanned(ident, "unknown matrix wrapper command")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
column_type: column_type.ok_or_else(|| syn::Error::new(input.span(),
|
|
|
|
"expected `col_type`"))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|