slang_backend/value/operations/
comparison.rs

1use super::super::Value;
2
3/// Comparison operations on values
4pub trait ComparisonOps {
5    /// Tests if two values are equal.
6    ///
7    /// ### Arguments
8    /// * `other` - The other value to compare
9    ///
10    /// ### Returns
11    /// * The result of the equality comparison
12    /// * An error message if the types cannot be compared
13    fn equal(&self, other: &Self) -> Result<Self, String>
14    where
15        Self: Sized;
16
17    /// Tests if two values are not equal.
18    ///
19    /// ### Arguments
20    /// * `other` - The other value to compare
21    ///
22    /// ### Returns
23    /// * The result of the inequality comparison
24    /// * An error message if the types cannot be compared
25    fn not_equal(&self, other: &Self) -> Result<Self, String>
26    where
27        Self: Sized;
28
29    /// Tests if one value is less than another.
30    ///
31    /// ### Arguments
32    /// * `other` - The other value to compare
33    ///
34    /// ### Returns
35    /// * The result of the less-than comparison
36    /// * An error message if the types cannot be compared
37    fn less_than(&self, other: &Self) -> Result<Self, String>
38    where
39        Self: Sized;
40
41    /// Tests if one value is less than or equal to another.
42    ///
43    /// ### Arguments
44    /// * `other` - The other value to compare
45    ///
46    /// ### Returns
47    /// * The result of the less-than-or-equal comparison
48    /// * An error message if the types cannot be compared
49    fn less_than_equal(&self, other: &Self) -> Result<Self, String>
50    where
51        Self: Sized;
52
53    /// Tests if one value is greater than another.
54    ///
55    /// ### Arguments
56    /// * `other` - The other value to compare
57    ///
58    /// ### Returns
59    /// * The result of the greater-than comparison
60    /// * An error message if the types cannot be compared
61    fn greater_than(&self, other: &Self) -> Result<Self, String>
62    where
63        Self: Sized;
64
65    /// Tests if one value is greater than or equal to another.
66    ///
67    /// ### Arguments
68    /// * `other` - The other value to compare
69    ///
70    /// ### Returns
71    /// * The result of the greater-than-or-equal comparison
72    /// * An error message if the types cannot be compared
73    fn greater_than_equal(&self, other: &Self) -> Result<Self, String>
74    where
75        Self: Sized;
76}
77
78impl ComparisonOps for Value {
79    fn equal(&self, other: &Self) -> Result<Value, String> {
80        match (self, other) {
81            (Value::I32(a), Value::I32(b)) => Ok(Value::Boolean(a == b)),
82            (Value::I64(a), Value::I64(b)) => Ok(Value::Boolean(a == b)),
83            (Value::U32(a), Value::U32(b)) => Ok(Value::Boolean(a == b)),
84            (Value::U64(a), Value::U64(b)) => Ok(Value::Boolean(a == b)),
85            (Value::F32(a), Value::F32(b)) => Ok(Value::Boolean(a == b)),
86            (Value::F64(a), Value::F64(b)) => Ok(Value::Boolean(a == b)),
87            (Value::Boolean(a), Value::Boolean(b)) => Ok(Value::Boolean(a == b)),
88            (Value::String(a), Value::String(b)) => Ok(Value::Boolean(a == b)),
89            (Value::Function(a), Value::Function(b)) => {
90                // Functions are equal if they have the same name and code offset
91                Ok(Value::Boolean(a.name == b.name && a.code_offset == b.code_offset))
92            },
93            (Value::NativeFunction(a), Value::NativeFunction(b)) => {
94                // Native functions are equal if they have the same name
95                Ok(Value::Boolean(a.name == b.name))
96            },
97            _ => Err("Cannot compare these types with ==".to_string()),
98        }
99    }
100
101    fn not_equal(&self, other: &Self) -> Result<Value, String> {
102        match (self, other) {
103            (Value::I32(a), Value::I32(b)) => Ok(Value::Boolean(a != b)),
104            (Value::I64(a), Value::I64(b)) => Ok(Value::Boolean(a != b)),
105            (Value::U32(a), Value::U32(b)) => Ok(Value::Boolean(a != b)),
106            (Value::U64(a), Value::U64(b)) => Ok(Value::Boolean(a != b)),
107            (Value::F32(a), Value::F32(b)) => Ok(Value::Boolean(a != b)),
108            (Value::F64(a), Value::F64(b)) => Ok(Value::Boolean(a != b)),
109            (Value::Boolean(a), Value::Boolean(b)) => Ok(Value::Boolean(a != b)),
110            (Value::String(a), Value::String(b)) => Ok(Value::Boolean(a != b)),
111            (Value::Function(a), Value::Function(b)) => {
112                // Functions are not equal if they have different names or code offsets
113                Ok(Value::Boolean(a.name != b.name || a.code_offset != b.code_offset))
114            },
115            (Value::NativeFunction(a), Value::NativeFunction(b)) => {
116                // Native functions are not equal if they have different names
117                Ok(Value::Boolean(a.name != b.name))
118            },
119            _ => Err("Cannot compare these types with !=".to_string()),
120        }
121    }
122
123    fn less_than(&self, other: &Self) -> Result<Value, String> {
124        match (self, other) {
125            (Value::I32(a), Value::I32(b)) => Ok(Value::Boolean(a < b)),
126            (Value::I64(a), Value::I64(b)) => Ok(Value::Boolean(a < b)),
127            (Value::U32(a), Value::U32(b)) => Ok(Value::Boolean(a < b)),
128            (Value::U64(a), Value::U64(b)) => Ok(Value::Boolean(a < b)),
129            (Value::F32(a), Value::F32(b)) => Ok(Value::Boolean(a < b)),
130            (Value::F64(a), Value::F64(b)) => Ok(Value::Boolean(a < b)),
131            _ => Err("Cannot compare these types with <".to_string()),
132        }
133    }
134
135    fn less_than_equal(&self, other: &Self) -> Result<Value, String> {
136        match (self, other) {
137            (Value::I32(a), Value::I32(b)) => Ok(Value::Boolean(a <= b)),
138            (Value::I64(a), Value::I64(b)) => Ok(Value::Boolean(a <= b)),
139            (Value::U32(a), Value::U32(b)) => Ok(Value::Boolean(a <= b)),
140            (Value::U64(a), Value::U64(b)) => Ok(Value::Boolean(a <= b)),
141            (Value::F32(a), Value::F32(b)) => Ok(Value::Boolean(a <= b)),
142            (Value::F64(a), Value::F64(b)) => Ok(Value::Boolean(a <= b)),
143            _ => Err("Cannot compare these types with <=".to_string()),
144        }
145    }
146
147    fn greater_than(&self, other: &Self) -> Result<Value, String> {
148        match (self, other) {
149            (Value::I32(a), Value::I32(b)) => Ok(Value::Boolean(a > b)),
150            (Value::I64(a), Value::I64(b)) => Ok(Value::Boolean(a > b)),
151            (Value::U32(a), Value::U32(b)) => Ok(Value::Boolean(a > b)),
152            (Value::U64(a), Value::U64(b)) => Ok(Value::Boolean(a > b)),
153            (Value::F32(a), Value::F32(b)) => Ok(Value::Boolean(a > b)),
154            (Value::F64(a), Value::F64(b)) => Ok(Value::Boolean(a > b)),
155            _ => Err("Cannot compare these types with >".to_string()),
156        }
157    }
158
159    fn greater_than_equal(&self, other: &Self) -> Result<Value, String> {
160        match (self, other) {
161            (Value::I32(a), Value::I32(b)) => Ok(Value::Boolean(a >= b)),
162            (Value::I64(a), Value::I64(b)) => Ok(Value::Boolean(a >= b)),
163            (Value::U32(a), Value::U32(b)) => Ok(Value::Boolean(a >= b)),
164            (Value::U64(a), Value::U64(b)) => Ok(Value::Boolean(a >= b)),
165            (Value::F32(a), Value::F32(b)) => Ok(Value::Boolean(a >= b)),
166            (Value::F64(a), Value::F64(b)) => Ok(Value::Boolean(a >= b)),
167            _ => Err("Cannot compare these types with >=".to_string()),
168        }
169    }
170}