bc_envelope/extension/expressions/
functions.rs

1use std::sync::{Mutex, Once};
2
3use paste::paste;
4
5use super::{Function, FunctionsStore};
6
7/// A macro that declares a function at compile time.
8#[macro_export]
9macro_rules! function_constant {
10    ($const_name:ident, $value:expr, $name:expr) => {
11        paste! {
12            pub const [<$const_name _VALUE>]: u64 = $value;
13        }
14        pub const $const_name: Function =
15            Function::new_with_static_name($value, $name);
16    };
17}
18
19function_constant!(ADD, 1, "add"); // addition
20function_constant!(SUB, 2, "sub"); // subtraction
21function_constant!(MUL, 3, "mul"); // multiplication
22function_constant!(DIV, 4, "div"); // division
23function_constant!(NEG, 5, "neg"); // unary negation
24function_constant!(LT, 6, "lt"); // less than
25function_constant!(LE, 7, "le"); // less than or equal to
26function_constant!(GT, 8, "gt"); // greater than
27function_constant!(GE, 9, "ge"); // greater than or equal to
28function_constant!(EQ, 10, "eq"); // equal to
29function_constant!(NE, 11, "ne"); // not equal to
30function_constant!(AND, 12, "and"); // logical and
31function_constant!(OR, 13, "or"); // logical or
32function_constant!(XOR, 14, "xor"); // logical exclusive or
33function_constant!(NOT, 15, "not"); // logical not
34
35#[doc(hidden)]
36#[derive(Debug)]
37pub struct LazyFunctions {
38    init: Once,
39    data: Mutex<Option<FunctionsStore>>,
40}
41
42impl LazyFunctions {
43    pub fn get(&self) -> std::sync::MutexGuard<'_, Option<FunctionsStore>> {
44        self.init.call_once(|| {
45            let m = FunctionsStore::new([ADD, SUB, MUL, DIV]);
46            *self.data.lock().unwrap() = Some(m);
47        });
48        self.data.lock().unwrap()
49    }
50}
51
52/// The global shared store of known functions.
53pub static GLOBAL_FUNCTIONS: LazyFunctions =
54    LazyFunctions { init: Once::new(), data: Mutex::new(None) };