Skip to main content

sql_fun_sqlast/sem/
operators.rs

1use crate::sem::TypeReference;
2
3/// Binary opreators
4#[derive(Debug, Clone, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
5pub struct BinaryOperatorDefinition {
6    left_arg_type: TypeReference,
7    right_arg_type: TypeReference,
8    result_type: TypeReference,
9    strict: bool,
10}
11
12impl BinaryOperatorDefinition {
13    /// create instance
14    #[must_use]
15    pub fn new(
16        left_arg_type: &TypeReference,
17        right_arg_type: &TypeReference,
18        result_type: &TypeReference,
19        strict: bool,
20    ) -> Self {
21        Self {
22            left_arg_type: left_arg_type.clone(),
23            right_arg_type: right_arg_type.clone(),
24            result_type: result_type.clone(),
25            strict,
26        }
27    }
28
29    /// operator return type
30    #[must_use]
31    pub fn get_return_type(&self) -> &TypeReference {
32        &self.result_type
33    }
34
35    /// strict
36    #[must_use]
37    pub fn is_strict(&self) -> bool {
38        self.strict
39    }
40
41    /// left argument type
42    #[must_use]
43    pub fn left_arg_type(&self) -> &TypeReference {
44        &self.left_arg_type
45    }
46
47    /// right argument type
48    #[must_use]
49    pub fn right_arg_type(&self) -> &TypeReference {
50        &self.right_arg_type
51    }
52}
53
54/// unary operator definitions
55#[derive(Debug, Clone, Eq, Hash, PartialEq, serde::Serialize, serde::Deserialize)]
56pub struct UnaryOperatorDefinition {
57    arg_type: TypeReference,
58    result_type: TypeReference,
59    strict: bool,
60}
61
62impl UnaryOperatorDefinition {
63    /// create instance
64    #[must_use]
65    pub fn new(arg_type: &TypeReference, result_type: &TypeReference, strict: bool) -> Self {
66        Self {
67            arg_type: arg_type.clone(),
68            result_type: result_type.clone(),
69            strict,
70        }
71    }
72
73    /// return type of operator
74    #[must_use]
75    pub fn get_return_type(&self) -> &TypeReference {
76        &self.result_type
77    }
78
79    /// strict
80    #[must_use]
81    pub fn is_strict(&self) -> bool {
82        self.strict
83    }
84
85    /// argument type
86    #[must_use]
87    pub fn arg_type(&self) -> &TypeReference {
88        &self.arg_type
89    }
90}
91
92/// Binary / Unary operator
93#[derive(Debug, Clone)]
94pub enum OperatorDefinition {
95    /// binary operator
96    Binary(Vec<BinaryOperatorDefinition>),
97    /// left assoc unary
98    LeftUnary(Vec<UnaryOperatorDefinition>),
99    /// right assoc unary
100    RightUnary(Vec<UnaryOperatorDefinition>),
101}
102
103impl OperatorDefinition {
104    /// binary
105    #[must_use]
106    pub fn binary(binaries: &[BinaryOperatorDefinition]) -> Self {
107        Self::Binary(Vec::from_iter(binaries.to_vec()))
108    }
109
110    /// left assoc unary
111    #[must_use]
112    pub fn left_unary(unaries: &[UnaryOperatorDefinition]) -> Self {
113        Self::LeftUnary(Vec::from_iter(unaries.to_vec()))
114    }
115
116    /// right assoc unary
117    #[must_use]
118    pub fn right_unary(unaries: &[UnaryOperatorDefinition]) -> Self {
119        Self::RightUnary(Vec::from_iter(unaries.to_vec()))
120    }
121}