mech_logic/
lib.rs

1#![no_main]
2#![allow(warnings)]
3#[macro_use]
4extern crate mech_core;
5#[cfg(feature = "matrix")]
6extern crate nalgebra as na;
7extern crate paste;
8
9use mech_core::*;
10
11#[cfg(feature = "vector3")]
12use nalgebra::Vector3;
13#[cfg(feature = "vectord")]
14use nalgebra::DVector;
15#[cfg(feature = "vector2")]
16use nalgebra::Vector2;
17#[cfg(feature = "vector4")]
18use nalgebra::Vector4;
19#[cfg(feature = "rowdvector")]
20use nalgebra::RowDVector;
21#[cfg(feature = "row_vectord")]
22use nalgebra::RowDVector;
23#[cfg(feature = "matrix1")]
24use nalgebra::Matrix1;
25#[cfg(feature = "matrix3")]
26use nalgebra::Matrix3;
27#[cfg(feature = "matrix4")]
28use nalgebra::Matrix4;
29#[cfg(feature = "row_vector3")]
30use nalgebra::RowVector3;
31#[cfg(feature = "row_vector4")]
32use nalgebra::RowVector4;
33#[cfg(feature = "row_vector2")]
34use nalgebra::RowVector2;
35#[cfg(feature = "matrixd")]
36use nalgebra::DMatrix;
37#[cfg(feature = "matrix2x3")]
38use nalgebra::Matrix2x3;
39#[cfg(feature = "matrix3x2")]
40use nalgebra::Matrix3x2;
41#[cfg(feature = "matrix2")]
42use nalgebra::Matrix2;
43
44use paste::paste;
45#[cfg(feature = "matrix")]
46use mech_core::matrix::Matrix;
47
48#[cfg(feature = "or")]
49pub mod or;
50#[cfg(feature = "and")]
51pub mod and;
52#[cfg(feature = "not")]
53pub mod not;
54#[cfg(feature = "xor")]
55pub mod xor;
56
57#[cfg(feature = "or")]
58pub use self::or::*;
59#[cfg(feature = "and")]
60pub use self::and::*;
61#[cfg(feature = "not")]
62pub use self::not::*;
63#[cfg(feature = "xor")]
64pub use self::xor::*;
65
66// ----------------------------------------------------------------------------
67// Logic Library
68// ----------------------------------------------------------------------------
69
70#[macro_export]
71macro_rules! impl_logic_binop {
72  ($struct_name:ident, $arg1_type:ty, $arg2_type:ty, $out_type:ty, $op:ident, $feature_flag:expr) => {
73    #[derive(Debug)]
74    struct $struct_name {
75      lhs: Ref<$arg1_type>,
76      rhs: Ref<$arg2_type>,
77      out: Ref<$out_type>,
78    }
79    impl MechFunctionFactory for $struct_name
80    {
81      fn new(args: FunctionArgs) -> MResult<Box<dyn MechFunction>> {
82        match args {
83          FunctionArgs::Binary(out, arg1, arg2) => {
84            let lhs: Ref<$arg1_type> = unsafe { arg1.as_unchecked() }.clone();
85            let rhs: Ref<$arg2_type> = unsafe { arg2.as_unchecked() }.clone();
86            let out: Ref<$out_type> = unsafe { out.as_unchecked() }.clone();
87            Ok(Box::new(Self {lhs, rhs, out }))
88          },
89          _ => Err(MechError{file: file!().to_string(), tokens: vec![], msg: format!("{} requires 2 arguments, got {:?}", stringify!($struct_name), args), id: line!(), kind: MechErrorKind::IncorrectNumberOfArguments})
90        }
91      }
92    }
93    impl MechFunctionImpl for $struct_name
94    {
95    fn solve(&self) {
96      let lhs_ptr = self.lhs.as_ptr();
97      let rhs_ptr = self.rhs.as_ptr();
98      let out_ptr = self.out.as_mut_ptr();
99      $op!(lhs_ptr,rhs_ptr,out_ptr);
100    }
101    fn out(&self) -> Value { self.out.to_value() }
102    fn to_string(&self) -> String { format!("{:#?}", self) }
103  }
104  #[cfg(feature = "compiler")]
105  impl MechFunctionCompiler for $struct_name
106  {
107    fn compile(&self, ctx: &mut CompileCtx) -> MResult<Register> {
108      let name = format!("{}<bool>", stringify!($struct_name));
109      compile_binop!(name, self.out, self.lhs, self.rhs, ctx, $feature_flag);
110    }
111  }
112};}
113
114#[macro_export]
115macro_rules! impl_logic_fxns {
116  ($lib:ident) => {
117    impl_fxns!($lib,bool,bool,impl_logic_binop);
118  }
119}