1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::value::Value;
use crate::VMError;
use std::ops::Div;

impl<'vm> Div for Value<'vm> {
    type Output = Value<'vm>;

    fn div(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Value::Error(v), _) => Value::Error(v),
            (_, Value::Error(v)) => Value::Error(v),
            (Value::None, _) => Value::None,
            (lhs, Value::None) => Value::Error(VMError::RuntimeError(format!(
                "Cannot divide {} by 0/none",
                lhs
            ))),
            (Value::Bool(a), Value::Bool(b)) => Value::Bool(a | b),
            (Value::Bool(a), b) => Value::Bool(a | b.to_bool()),
            (b, Value::Bool(a)) => Value::Bool(a | b.to_bool()),
            (Value::Number(a), Value::Number(b)) => {
                if b.is_zero() {
                    return Value::Error(VMError::RuntimeError(format!(
                        "Cannot divide {} by 0/none",
                        a
                    )));
                }

                match a / b {
                    Ok(n) => Value::Number(n),
                    Err(e) => Value::Error(e),
                }
            }
            (Value::String(a), Value::String(b)) => {
                let result = a.split(b.as_str());
                Value::List(result.map(|s| Value::String(s.to_string())).collect())
            }
            (Value::String(a), b) => {
                let b = b.to_string();
                let result = a.split(b.as_str());
                Value::List(result.map(|s| Value::String(s.to_string())).collect())
            }
            // (Value::List(a), Value::List(b)) => {
            //     let mut result = a.clone();
            //     result.extend(b);
            //     Value::List(result)
            // }
            // (Value::List(a), b) => {
            //     let mut result = a.clone();
            //     result.push(b);
            //     Value::List(result)
            // }
            // (Value::Map(a), Value::Map(b)) => {
            //     let mut result = a.clone();
            //     result.extend(b);
            //     Value::Map(result)
            // }
            // (Value::Map(a), b) => {
            //     let mut result = a.clone();
            //     result.insert(b.clone(), b);
            //     Value::Map(result)
            // }
            _ => todo!(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::define_value_tests;
    use crate::number::Number;
    use crate::value::Value;
    use crate::VMError::RuntimeError;

    define_value_tests! {
        / {
            test_none_div_none => (Value::None, Value::None, Value::None);
            test_none_bool_false_div_none => (Value::Bool(false), Value::None, Value::Error(RuntimeError("Cannot divide false by 0/none".to_string())));
            test_bool_true_div_none => (Value::Bool(true), Value::None, Value::Error(RuntimeError("Cannot divide true by 0/none".to_string())));
            test_none_bool_true_div_true => (Value::None, Value::Bool(true), Value::None);
            test_false_bool_true_div_true => (Value::Bool(false), Value::Bool(true), Value::Bool(true));
            test_false_0_div_true => (Value::Bool(false), Value::Number(Number::UInt(0)), Value::Bool(false));
            test_true_0_div_true => (Value::Bool(true), Value::Number(Number::UInt(0)), Value::Number(Number::UInt(1)));
            // div more test cases here as needed
        }
    }
}