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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::runtime::Variant;
use crate::runtime::types::MetaObject;
use crate::runtime::errors::{ExecResult, RuntimeError};


macro_rules! meta_eval_unary {
    ( $operand:expr, $unary_method:tt ) => {
        $operand.as_meta().$unary_method()
            .unwrap_or_else(|| Err(RuntimeError::invalid_unary_operand($operand)))
    };
}

macro_rules! meta_eval_binary {
    ( $lhs:expr, $rhs:expr, $binary_method:tt, $reflected_method:tt) => {
        {
            if let Some(result) = $lhs.as_meta().$binary_method($rhs) {
                return result;
            }
            
            if $lhs.type_tag() != $rhs.type_tag() {
                if let Some(result) = $rhs.as_meta().$reflected_method($lhs) {
                    return result;
                }
            }
            
            Err(RuntimeError::invalid_binary_operands($lhs, $rhs))
        }
    };
}

macro_rules! meta_eval_inequality {
    ( $lhs:expr, $rhs:expr, $compare_method:tt, $reflected_method:tt) => {
        {
            if let Some(result) = $lhs.as_meta().$compare_method($rhs) {
                return result;
            }
            
            if $lhs.type_tag() != $rhs.type_tag() {
                if let Some(result) = $rhs.as_meta().$reflected_method($lhs) {
                    return result.map(|cmp| !cmp);
                }
            }
            
            Err(RuntimeError::invalid_binary_operands($lhs, $rhs))
        }
    };
}

impl Variant {
    
    // Unary
    
    #[inline(always)]
    pub fn apply_neg(&self) -> ExecResult<Variant> {
        meta_eval_unary!(self, op_neg)
    }
    
    #[inline(always)]
    pub fn apply_pos(&self) -> ExecResult<Variant> {
        meta_eval_unary!(self, op_pos)
    }
    
    #[inline(always)]
    pub fn apply_inv(&self) -> ExecResult<Variant> {
        meta_eval_unary!(self, op_inv)
    }
    
    #[inline(always)]
    pub fn apply_not(&self) -> ExecResult<Variant> {
        Ok(Variant::from(!self.as_bool()?))
    }
    
    // Arithmetic
    
    #[inline(always)]
    pub fn apply_mul(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_mul, op_rmul)
    }
    
    #[inline(always)]
    pub fn apply_div(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_div, op_rdiv)
    }
    
    #[inline(always)]
    pub fn apply_mod(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_mod, op_rmod)
    }
    
    #[inline(always)]
    pub fn apply_add(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_add, op_radd)
    }
    
    #[inline(always)]
    pub fn apply_sub(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_sub, op_rsub)
    }
    
    // Bitwise
    
    pub fn apply_and(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_and, op_rand)
    }
    
    pub fn apply_xor(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_xor, op_rxor)
    }
    
    pub fn apply_or(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_or, op_ror)
    }
    
    // Shifts
    
    pub fn apply_shl(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_shl, op_rshl)
    }
    
    pub fn apply_shr(&self, rhs: &Variant) -> ExecResult<Variant> {
        meta_eval_binary!(self, rhs, op_shr, op_rshr)
    }

    // Comparison
    
    pub fn cmp_eq(&self, other: &Variant) -> ExecResult<bool> {
        if let Some(result) = self.as_meta().cmp_eq(other) {
            return result;
        }
        
        if self.type_tag() != other.type_tag() {
            if let Some(result) = other.as_meta().cmp_eq(self) {
                return result;
            }
        }
        
        Ok(false)
    }
    
    pub fn cmp_ne(&self, other: &Variant) -> ExecResult<bool> {
        self.cmp_eq(other).map(|cmp| !cmp)
    }
    
    pub fn cmp_lt(&self, other: &Variant) -> ExecResult<bool> {
        meta_eval_inequality!(self, other, cmp_lt, cmp_le)
    }
    
    pub fn cmp_le(&self, other: &Variant) -> ExecResult<bool> {
        meta_eval_inequality!(self, other, cmp_le, cmp_lt)
    }
    
    pub fn cmp_gt(&self, other: &Variant) -> ExecResult<bool> {
        self.cmp_le(other).map(|cmp| !cmp)
    }

    pub fn cmp_ge(&self, other: &Variant) -> ExecResult<bool> {
        self.cmp_lt(other).map(|cmp| !cmp)
    }
}