mech_compare/
lib.rs

1#![no_main]
2#![allow(warnings)]
3#[macro_use]
4extern crate mech_core;
5extern crate paste;
6
7use mech_core::*;
8
9#[cfg(feature = "vector3")]
10use nalgebra::Vector3;
11#[cfg(feature = "vectord")]
12use nalgebra::DVector;
13#[cfg(feature = "vector2")]
14use nalgebra::Vector2;
15#[cfg(feature = "vector4")]
16use nalgebra::Vector4;
17#[cfg(feature = "rowdvector")]
18use nalgebra::RowDVector;
19#[cfg(feature = "row_vectord")]
20use nalgebra::RowDVector;
21#[cfg(feature = "matrix1")]
22use nalgebra::Matrix1;
23#[cfg(feature = "matrix3")]
24use nalgebra::Matrix3;
25#[cfg(feature = "matrix4")]
26use nalgebra::Matrix4;
27#[cfg(feature = "row_vector3")]
28use nalgebra::RowVector3;
29#[cfg(feature = "row_vector4")]
30use nalgebra::RowVector4;
31#[cfg(feature = "row_vector2")]
32use nalgebra::RowVector2;
33#[cfg(feature = "matrixd")]
34use nalgebra::DMatrix;
35#[cfg(feature = "matrix2x3")]
36use nalgebra::Matrix2x3;
37#[cfg(feature = "matrix3x2")]
38use nalgebra::Matrix3x2;
39#[cfg(feature = "matrix2")]
40use nalgebra::Matrix2;
41
42use paste::paste;
43
44#[cfg(feature = "gt")]
45pub mod gt;
46#[cfg(feature = "lt")]
47pub mod lt;
48#[cfg(feature = "lte")]
49pub mod lte;
50#[cfg(feature = "gte")]
51pub mod gte;
52#[cfg(feature = "eq")]
53pub mod eq;
54#[cfg(feature = "neq")]
55pub mod neq;
56
57#[cfg(feature = "gt")]
58pub use self::gt::*;
59#[cfg(feature = "lt")]
60pub use self::lt::*;
61#[cfg(feature = "lte")]
62pub use self::lte::*;
63#[cfg(feature = "gte")]
64pub use self::gte::*;
65#[cfg(feature = "eq")]
66pub use self::eq::*;
67#[cfg(feature = "neq")]
68pub use self::neq::*;
69
70// ----------------------------------------------------------------------------
71// Compare Library
72// ----------------------------------------------------------------------------
73
74#[macro_export]
75macro_rules! impl_compare_binop {
76  ($struct_name:ident, $arg1_type:ty, $arg2_type:ty, $out_type:ty, $op:ident, $feature_flag:expr) => {
77    #[derive(Debug)]
78    struct $struct_name<T> {
79      lhs: Ref<$arg1_type>,
80      rhs: Ref<$arg2_type>,
81      out: Ref<$out_type>,
82    }
83    impl<T> MechFunctionFactory for $struct_name<T>
84    where
85      T: std::fmt::Debug + Clone + Sync + Send + 'static + 
86      ConstElem + CompileConst + AsValueKind +
87      PartialEq + PartialOrd,
88      Ref<$out_type>: ToValue
89    {
90      fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
91        match args {
92          FunctionArgs::Binary(out, arg1, arg2) => {
93            let lhs: Ref<$arg1_type> = unsafe { arg1.as_unchecked() }.clone();
94            let rhs: Ref<$arg2_type> = unsafe { arg2.as_unchecked() }.clone();
95            let out: Ref<$out_type> = unsafe { out.as_unchecked() }.clone();
96            Ok(Box::new(Self {lhs, rhs, out }))
97          },
98          _ => Err(MechError{file: file!().to_string(), tokens: vec![], msg: format!("{} requires 2 arguments, got {:?}", stringify!($struct_name), args), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments})
99        }
100      }
101    }
102    impl<T> MechFunctionImpl for $struct_name<T>
103    where
104      T: std::fmt::Debug + Clone + Sync + Send + 'static + 
105      PartialEq + PartialOrd,
106      Ref<$out_type>: ToValue
107    {
108    fn solve(&self) {
109      let lhs_ptr = self.lhs.as_ptr();
110      let rhs_ptr = self.rhs.as_ptr();
111      let out_ptr = self.out.as_mut_ptr();
112      $op!(lhs_ptr,rhs_ptr,out_ptr);
113    }
114    fn out(&self) -> Value { self.out.to_value() }
115    fn to_string(&self) -> String { format!("{:#?}", self) }
116  }
117  #[cfg(feature = "compiler")]
118  impl<T> MechFunctionCompiler for $struct_name<T> 
119  where
120    T: ConstElem + CompileConst + AsValueKind
121  {
122    fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
123      let name = format!("{}<{}>", stringify!($struct_name), T::as_value_kind());
124      compile_binop!(name, self.out, self.lhs, self.rhs, ctx, $feature_flag);
125    }
126  }
127  register_fxn_descriptor!($struct_name, bool, "bool", String, "string", u8, "u8", i8, "i8", u16, "u16", i16, "i16", u32, "u32", i32, "i32", u64, "u64", i64, "i64", u128, "u128", i128, "i128", F32, "f32", F64, "f64", R64, "r64", C64, "c64");
128};}
129  
130
131#[macro_export]
132macro_rules! impl_compare_fxns {
133  ($lib:ident) => {
134    impl_fxns!($lib,T,bool,impl_compare_binop);
135  }
136}