Skip to main content

linkrs_core/value/
value_arithmetic.rs

1//! Value Calculation Module
2//!
3//! This module provides methods for arithmetic, logical, and bitwise operations on values.
4use crate::value::Value;
5
6impl Value {
7    /// Addition operation
8    pub fn add(&self, other: &Value) -> Result<Value, String> {
9        use Value::*;
10        match (self, other) {
11            // Same type operations
12            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a.wrapping_add(*b))),
13            (Int(a), Int(b)) => Ok(Int(a.wrapping_add(*b))),
14            (BigInt(a), BigInt(b)) => Ok(BigInt(a.wrapping_add(*b))),
15            (Float(a), Float(b)) => Ok(Float(a + b)),
16            (Double(a), Double(b)) => Ok(Double(a + b)),
17
18            // Cross-type operations: promote to larger type
19            (SmallInt(a), Int(b)) => Ok(Int(*a as i32 + b)),
20            (Int(a), SmallInt(b)) => Ok(Int(a + *b as i32)),
21            (SmallInt(a), BigInt(b)) => Ok(BigInt(*a as i64 + b)),
22            (BigInt(a), SmallInt(b)) => Ok(BigInt(a + *b as i64)),
23            (Int(a), BigInt(b)) => Ok(BigInt(*a as i64 + b)),
24            (BigInt(a), Int(b)) => Ok(BigInt(a + *b as i64)),
25
26            // Integer to float promotion
27            (SmallInt(a), Float(b)) => Ok(Float(*a as f32 + b)),
28            (Float(a), SmallInt(b)) => Ok(Float(a + *b as f32)),
29            (Int(a), Float(b)) => Ok(Float(*a as f32 + b)),
30            (Float(a), Int(b)) => Ok(Float(a + *b as f32)),
31            (BigInt(a), Float(b)) => Ok(Float(*a as f32 + b)),
32            (Float(a), BigInt(b)) => Ok(Float(a + *b as f32)),
33
34            (SmallInt(a), Double(b)) => Ok(Double(*a as f64 + b)),
35            (Double(a), SmallInt(b)) => Ok(Double(a + *b as f64)),
36            (Int(a), Double(b)) => Ok(Double(*a as f64 + b)),
37            (Double(a), Int(b)) => Ok(Double(a + *b as f64)),
38            (BigInt(a), Double(b)) => Ok(Double(*a as f64 + b)),
39            (Double(a), BigInt(b)) => Ok(Double(a + *b as f64)),
40
41            (Float(a), Double(b)) => Ok(Double(*a as f64 + b)),
42            (Double(a), Float(b)) => Ok(Double(a + *b as f64)),
43
44            // String concatenation
45            (String(a), String(b)) => Ok(String(format!("{}{}", a, b).into())),
46            (String(a), FixedString(b)) => Ok(String(format!("{}{}", a, b).into())),
47            (FixedString(a), String(b)) => Ok(String(format!("{}{}", a, b).into())),
48            (FixedString(a), FixedString(b)) => Ok(String(format!("{}{}", a, b).into())),
49            // High-precision decimal addition
50            (Decimal128(a), Decimal128(b)) => a.add(b).map(Decimal128),
51            _ => Err("Cannot perform addition on these value types".to_string()),
52        }
53    }
54
55    /// Subtraction operation
56    pub fn sub(&self, other: &Value) -> Result<Value, String> {
57        use Value::*;
58        match (self, other) {
59            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a.wrapping_sub(*b))),
60            (Int(a), Int(b)) => Ok(Int(a.wrapping_sub(*b))),
61            (BigInt(a), BigInt(b)) => Ok(BigInt(a.wrapping_sub(*b))),
62            (Float(a), Float(b)) => Ok(Float(a - b)),
63            (Double(a), Double(b)) => Ok(Double(a - b)),
64
65            // Cross-type operations
66            (SmallInt(a), Int(b)) => Ok(Int(*a as i32 - b)),
67            (Int(a), SmallInt(b)) => Ok(Int(a - *b as i32)),
68            (SmallInt(a), BigInt(b)) => Ok(BigInt(*a as i64 - b)),
69            (BigInt(a), SmallInt(b)) => Ok(BigInt(a - *b as i64)),
70            (Int(a), BigInt(b)) => Ok(BigInt(*a as i64 - b)),
71            (BigInt(a), Int(b)) => Ok(BigInt(a - *b as i64)),
72
73            // Integer to float
74            (SmallInt(a), Float(b)) => Ok(Float(*a as f32 - b)),
75            (Float(a), SmallInt(b)) => Ok(Float(a - *b as f32)),
76            (Int(a), Float(b)) => Ok(Float(*a as f32 - b)),
77            (Float(a), Int(b)) => Ok(Float(a - *b as f32)),
78            (BigInt(a), Float(b)) => Ok(Float(*a as f32 - b)),
79            (Float(a), BigInt(b)) => Ok(Float(a - *b as f32)),
80
81            (SmallInt(a), Double(b)) => Ok(Double(*a as f64 - b)),
82            (Double(a), SmallInt(b)) => Ok(Double(a - *b as f64)),
83            (Int(a), Double(b)) => Ok(Double(*a as f64 - b)),
84            (Double(a), Int(b)) => Ok(Double(a - *b as f64)),
85            (BigInt(a), Double(b)) => Ok(Double(*a as f64 - b)),
86            (Double(a), BigInt(b)) => Ok(Double(a - *b as f64)),
87
88            (Float(a), Double(b)) => Ok(Double(*a as f64 - b)),
89            (Double(a), Float(b)) => Ok(Double(a - *b as f64)),
90
91            _ => Err("Cannot perform subtraction on these value types".to_string()),
92        }
93    }
94
95    /// Multiplication operation
96    pub fn mul(&self, other: &Value) -> Result<Value, String> {
97        use Value::*;
98        match (self, other) {
99            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a.wrapping_mul(*b))),
100            (Int(a), Int(b)) => Ok(Int(a.wrapping_mul(*b))),
101            (BigInt(a), BigInt(b)) => Ok(BigInt(a.wrapping_mul(*b))),
102            (Float(a), Float(b)) => Ok(Float(a * b)),
103            (Double(a), Double(b)) => Ok(Double(a * b)),
104
105            // Cross-type operations
106            (SmallInt(a), Int(b)) => Ok(Int(*a as i32 * b)),
107            (Int(a), SmallInt(b)) => Ok(Int(a * *b as i32)),
108            (SmallInt(a), BigInt(b)) => Ok(BigInt(*a as i64 * b)),
109            (BigInt(a), SmallInt(b)) => Ok(BigInt(a * *b as i64)),
110            (Int(a), BigInt(b)) => Ok(BigInt(*a as i64 * b)),
111            (BigInt(a), Int(b)) => Ok(BigInt(a * *b as i64)),
112
113            // Integer to float
114            (SmallInt(a), Float(b)) => Ok(Float(*a as f32 * b)),
115            (Float(a), SmallInt(b)) => Ok(Float(a * *b as f32)),
116            (Int(a), Float(b)) => Ok(Float(*a as f32 * b)),
117            (Float(a), Int(b)) => Ok(Float(a * *b as f32)),
118            (BigInt(a), Float(b)) => Ok(Float(*a as f32 * b)),
119            (Float(a), BigInt(b)) => Ok(Float(a * *b as f32)),
120
121            (SmallInt(a), Double(b)) => Ok(Double(*a as f64 * b)),
122            (Double(a), SmallInt(b)) => Ok(Double(a * *b as f64)),
123            (Int(a), Double(b)) => Ok(Double(*a as f64 * b)),
124            (Double(a), Int(b)) => Ok(Double(a * *b as f64)),
125            (BigInt(a), Double(b)) => Ok(Double(*a as f64 * b)),
126            (Double(a), BigInt(b)) => Ok(Double(a * *b as f64)),
127
128            (Float(a), Double(b)) => Ok(Double(*a as f64 * b)),
129            (Double(a), Float(b)) => Ok(Double(a * *b as f64)),
130
131            _ => Err("Cannot perform multiplication on these value types".to_string()),
132        }
133    }
134
135    /// Division operation
136    pub fn div(&self, other: &Value) -> Result<Value, String> {
137        use Value::*;
138        match (self, other) {
139            (SmallInt(a), SmallInt(b)) => {
140                if *b == 0 {
141                    Err("Division by zero".to_string())
142                } else {
143                    Ok(SmallInt(a / b))
144                }
145            }
146            (Int(a), Int(b)) => {
147                if *b == 0 {
148                    Err("Division by zero".to_string())
149                } else {
150                    Ok(Int(a / b))
151                }
152            }
153            (BigInt(a), BigInt(b)) => {
154                if *b == 0 {
155                    Err("Division by zero".to_string())
156                } else {
157                    Ok(BigInt(a / b))
158                }
159            }
160            (Float(a), Float(b)) => {
161                if *b == 0.0 {
162                    Err("Division by zero".to_string())
163                } else {
164                    Ok(Float(a / b))
165                }
166            }
167            (Double(a), Double(b)) => {
168                if *b == 0.0 {
169                    Err("Division by zero".to_string())
170                } else {
171                    Ok(Double(a / b))
172                }
173            }
174
175            // Cross-type: promote to larger type
176            (SmallInt(a), Int(b)) => {
177                if *b == 0 {
178                    Err("Division by zero".to_string())
179                } else {
180                    Ok(Int(*a as i32 / b))
181                }
182            }
183            (Int(a), SmallInt(b)) => {
184                if *b == 0 {
185                    Err("Division by zero".to_string())
186                } else {
187                    Ok(Int(a / *b as i32))
188                }
189            }
190            (Int(a), BigInt(b)) => {
191                if *b == 0 {
192                    Err("Division by zero".to_string())
193                } else {
194                    Ok(BigInt(*a as i64 / b))
195                }
196            }
197            (BigInt(a), Int(b)) => {
198                if *b == 0 {
199                    Err("Division by zero".to_string())
200                } else {
201                    Ok(BigInt(a / *b as i64))
202                }
203            }
204
205            // Integer to double for division
206            (SmallInt(a), Double(b)) => {
207                if *b == 0.0 {
208                    Err("Division by zero".to_string())
209                } else {
210                    Ok(Double(*a as f64 / b))
211                }
212            }
213            (Double(a), SmallInt(b)) => {
214                if *b == 0 {
215                    Err("Division by zero".to_string())
216                } else {
217                    Ok(Double(a / *b as f64))
218                }
219            }
220            (Int(a), Double(b)) => {
221                if *b == 0.0 {
222                    Err("Division by zero".to_string())
223                } else {
224                    Ok(Double(*a as f64 / b))
225                }
226            }
227            (Double(a), Int(b)) => {
228                if *b == 0 {
229                    Err("Division by zero".to_string())
230                } else {
231                    Ok(Double(a / *b as f64))
232                }
233            }
234            (BigInt(a), Double(b)) => {
235                if *b == 0.0 {
236                    Err("Division by zero".to_string())
237                } else {
238                    Ok(Double(*a as f64 / b))
239                }
240            }
241            (Double(a), BigInt(b)) => {
242                if *b == 0 {
243                    Err("Division by zero".to_string())
244                } else {
245                    Ok(Double(a / *b as f64))
246                }
247            }
248
249            _ => Err("Cannot perform division on these value types".to_string()),
250        }
251    }
252
253    /// Modular operation
254    pub fn rem(&self, other: &Value) -> Result<Value, String> {
255        use Value::*;
256        match (self, other) {
257            (SmallInt(a), SmallInt(b)) => {
258                if *b == 0 {
259                    Err("Division by zero".to_string())
260                } else {
261                    Ok(SmallInt(a % b))
262                }
263            }
264            (Int(a), Int(b)) => {
265                if *b == 0 {
266                    Err("Division by zero".to_string())
267                } else {
268                    Ok(Int(a % b))
269                }
270            }
271            (BigInt(a), BigInt(b)) => {
272                if *b == 0 {
273                    Err("Division by zero".to_string())
274                } else {
275                    Ok(BigInt(a % b))
276                }
277            }
278            _ => Err("Modulo operation is only supported for integer types".to_string()),
279        }
280    }
281
282    /// Power operation
283    pub fn pow(&self, other: &Value) -> Result<Value, String> {
284        use Value::*;
285        match (self, other) {
286            (SmallInt(a), SmallInt(b)) => {
287                if *b < 0 {
288                    Err("Negative exponent not supported for integer power operation".to_string())
289                } else {
290                    Ok(SmallInt(a.pow(*b as u32)))
291                }
292            }
293            (Int(a), Int(b)) => {
294                if *b < 0 {
295                    Err("Negative exponent not supported for integer power operation".to_string())
296                } else {
297                    Ok(Int(a.pow(*b as u32)))
298                }
299            }
300            (BigInt(a), BigInt(b)) => {
301                if *b < 0 {
302                    Err("Negative exponent not supported for integer power operation".to_string())
303                } else {
304                    Ok(BigInt(a.pow(*b as u32)))
305                }
306            }
307            (Float(a), Float(b)) => Ok(Float(a.powf(*b))),
308            (Double(a), Double(b)) => Ok(Double(a.powf(*b))),
309            (SmallInt(a), Double(b)) => Ok(Double((*a as f64).powf(*b))),
310            (Double(a), SmallInt(b)) => Ok(Double(a.powi(*b as i32))),
311            (Int(a), Double(b)) => Ok(Double((*a as f64).powf(*b))),
312            (Double(a), Int(b)) => Ok(Double(a.powi(*b))),
313            (BigInt(a), Double(b)) => Ok(Double((*a as f64).powf(*b))),
314            (Double(a), BigInt(b)) => Ok(Double(a.powi(*b as i32))),
315            _ => Err("Cannot perform power operation on these value types".to_string()),
316        }
317    }
318
319    /// Negation operation
320    pub fn neg(&self) -> Result<Value, String> {
321        use Value::*;
322        match self {
323            SmallInt(a) => Ok(SmallInt(-a)),
324            Int(a) => Ok(Int(-a)),
325            BigInt(a) => Ok(BigInt(-a)),
326            Float(a) => Ok(Float(-a)),
327            Double(a) => Ok(Double(-a)),
328            _ => Err("Negation is only supported for numeric types".to_string()),
329        }
330    }
331
332    /// Logic and Operations
333    pub fn and(&self, other: &Value) -> Result<Value, String> {
334        use Value::*;
335        match (self, other) {
336            (Bool(a), Bool(b)) => Ok(Bool(*a && *b)),
337            _ => Err("Logical AND is only supported for boolean types".to_string()),
338        }
339    }
340
341    /// Logical OR operation
342    pub fn or(&self, other: &Value) -> Result<Value, String> {
343        use Value::*;
344        match (self, other) {
345            (Bool(a), Bool(b)) => Ok(Bool(*a || *b)),
346            _ => Err("Logical OR is only supported for boolean types".to_string()),
347        }
348    }
349
350    /// Logical NOT operation
351    pub fn not(&self) -> Result<Value, String> {
352        use Value::*;
353        match self {
354            Bool(a) => Ok(Bool(!a)),
355            _ => Err("Logical NOT is only supported for boolean types".to_string()),
356        }
357    }
358
359    /// Bitwise AND operation
360    pub fn bit_and(&self, other: &Value) -> Result<Value, String> {
361        use Value::*;
362        match (self, other) {
363            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a & b)),
364            (Int(a), Int(b)) => Ok(Int(a & b)),
365            (BigInt(a), BigInt(b)) => Ok(BigInt(a & b)),
366            _ => Err("Bitwise AND is only supported for integer types".to_string()),
367        }
368    }
369
370    /// Bitwise OR operation
371    pub fn bit_or(&self, other: &Value) -> Result<Value, String> {
372        use Value::*;
373        match (self, other) {
374            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a | b)),
375            (Int(a), Int(b)) => Ok(Int(a | b)),
376            (BigInt(a), BigInt(b)) => Ok(BigInt(a | b)),
377            _ => Err("Bitwise OR is only supported for integer types".to_string()),
378        }
379    }
380
381    /// Bitwise XOR operation
382    pub fn bit_xor(&self, other: &Value) -> Result<Value, String> {
383        use Value::*;
384        match (self, other) {
385            (SmallInt(a), SmallInt(b)) => Ok(SmallInt(a ^ b)),
386            (Int(a), Int(b)) => Ok(Int(a ^ b)),
387            (BigInt(a), BigInt(b)) => Ok(BigInt(a ^ b)),
388            _ => Err("Bitwise XOR is only supported for integer types".to_string()),
389        }
390    }
391
392    /// Left shift operation
393    pub fn bit_shl(&self, other: &Value) -> Result<Value, String> {
394        use Value::*;
395        match (self, other) {
396            (SmallInt(a), SmallInt(b)) => {
397                if *b < 0 || *b >= 16 {
398                    Err("Shift count out of range".to_string())
399                } else {
400                    Ok(SmallInt(a << *b as u32))
401                }
402            }
403            (Int(a), Int(b)) => {
404                if *b < 0 || *b >= 32 {
405                    Err("Shift count out of range".to_string())
406                } else {
407                    Ok(Int(a << *b as u32))
408                }
409            }
410            (BigInt(a), BigInt(b)) => {
411                if *b < 0 || *b >= 64 {
412                    Err("Shift count out of range".to_string())
413                } else {
414                    Ok(BigInt(a << *b as u32))
415                }
416            }
417            _ => Err("Bitwise left shift is only supported for integer types".to_string()),
418        }
419    }
420
421    /// Right-shift operation
422    pub fn bit_shr(&self, other: &Value) -> Result<Value, String> {
423        use Value::*;
424        match (self, other) {
425            (SmallInt(a), SmallInt(b)) => {
426                if *b < 0 || *b >= 16 {
427                    Err("Shift count out of range".to_string())
428                } else {
429                    Ok(SmallInt(a >> *b as u32))
430                }
431            }
432            (Int(a), Int(b)) => {
433                if *b < 0 || *b >= 32 {
434                    Err("Shift count out of range".to_string())
435                } else {
436                    Ok(Int(a >> *b as u32))
437                }
438            }
439            (BigInt(a), BigInt(b)) => {
440                if *b < 0 || *b >= 64 {
441                    Err("Shift count out of range".to_string())
442                } else {
443                    Ok(BigInt(a >> *b as u32))
444                }
445            }
446            _ => Err("Bitwise right shift is only supported for integer types".to_string()),
447        }
448    }
449
450    /// Bitwise NOT operation
451    pub fn bit_not(&self) -> Result<Value, String> {
452        use Value::*;
453        match self {
454            SmallInt(a) => Ok(SmallInt(!a)),
455            Int(a) => Ok(Int(!a)),
456            BigInt(a) => Ok(BigInt(!a)),
457            _ => Err("Bitwise NOT is only supported for integer types".to_string()),
458        }
459    }
460
461    /// Absolute value operation
462    pub fn abs(&self) -> Result<Value, String> {
463        use Value::*;
464        match self {
465            SmallInt(a) => Ok(SmallInt(a.abs())),
466            Int(a) => Ok(Int(a.abs())),
467            BigInt(a) => Ok(BigInt(a.abs())),
468            Float(a) => Ok(Float(a.abs())),
469            Double(a) => Ok(Double(a.abs())),
470            _ => Err("Absolute value is only supported for numeric types".to_string()),
471        }
472    }
473
474    /// Get the length of a value (for strings, lists, maps, sets)
475    pub fn len(&self) -> Result<Value, String> {
476        use Value::*;
477        match self {
478            String(s) => Ok(Int(s.len() as i32)),
479            FixedString(data) => Ok(Int(data.len() as i32)),
480            List(l) => Ok(Int(l.values.len() as i32)),
481            Map(m) => Ok(Int(m.len() as i32)),
482            Set(s) => Ok(Int(s.len() as i32)),
483            Blob(b) => Ok(Int(b.len() as i32)),
484            _ => Err(
485                "Length operation is only supported for string, blob, list, map, or set types"
486                    .to_string(),
487            ),
488        }
489    }
490}