Skip to main content

js2zig_core/zigir/
ops.rs

1// zigir/ops.rs
2// Operator enums for IR expressions.
3
4/// Binary arithmetic/comparison operator.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum BinOp {
7    Add,
8    Sub,
9    Mul,
10    Div,
11    Mod,
12    Pow,
13    Lt,
14    Gt,
15    Le,
16    Ge,
17    Eq,
18    Ne,
19    StrictEq,
20    StrictNe,
21    BitAnd,
22    BitOr,
23    BitXor,
24    Shl,
25    Shr,
26    UrShr,
27    In,
28    InstanceOf,
29}
30
31/// Unary operator (prefix).
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
33pub enum UnaOp {
34    Neg,    // -x
35    Not,    // !x
36    BitNot, // ~x
37    TypeOf, // typeof x
38    Void,   // void x
39    Delete, // delete x (only valid in specific contexts)
40}
41
42/// Logical short-circuit operator.
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub enum LogicalOp {
45    And,     // &&
46    Or,      // ||
47    Nullish, // ??
48}
49
50/// Update operator (++ / --).
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
52pub enum UpdateOp {
53    Increment,
54    Decrement,
55}
56
57/// Compound assignment operator.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59pub enum AssignOp {
60    Assign,   // =
61    Add,      // +=
62    Sub,      // -=
63    Mul,      // *=
64    Div,      // /=
65    Mod,      // %=
66    Shl,      // <<=
67    Shr,      // >>=
68    BitAnd,   // &=
69    BitOr,    // |=
70    BitXor,   // ^=
71    LogicAnd, // &&=
72    LogicOr,  // ||=
73    Nullish,  // ??=
74}
75
76impl AssignOp {
77    /// Zig assignment operator string.
78    pub fn to_zig_str(self) -> &'static str {
79        match self {
80            AssignOp::Assign => "=",
81            AssignOp::Add => "+=",
82            AssignOp::Sub => "-=",
83            AssignOp::Mul => "*=",
84            AssignOp::Div => "/=",
85            AssignOp::Mod => "%=",
86            AssignOp::Shl => "<<=",
87            AssignOp::Shr => ">>=",
88            AssignOp::BitAnd => "&=",
89            AssignOp::BitOr => "|=",
90            AssignOp::BitXor => "^=",
91            AssignOp::LogicAnd => "and=",
92            AssignOp::LogicOr => "or=",
93            AssignOp::Nullish => "orelse=",
94        }
95    }
96
97    /// For compound assignments on BigInt, convert to the equivalent BinOp.
98    /// Returns None for non-arithmetic ops (Assign, LogicAnd, LogicOr, Nullish).
99    pub fn to_bin_op(&self) -> Option<BinOp> {
100        match self {
101            Self::Add => Some(BinOp::Add),
102            Self::Sub => Some(BinOp::Sub),
103            Self::Mul => Some(BinOp::Mul),
104            Self::Div => Some(BinOp::Div),
105            Self::Mod => Some(BinOp::Mod),
106            Self::Shl => Some(BinOp::Shl),
107            Self::Shr => Some(BinOp::Shr),
108            Self::BitAnd => Some(BinOp::BitAnd),
109            Self::BitOr => Some(BinOp::BitOr),
110            Self::BitXor => Some(BinOp::BitXor),
111            _ => None,
112        }
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[test]
121    fn test_assign_op_zig_str() {
122        assert_eq!(AssignOp::Assign.to_zig_str(), "=");
123        assert_eq!(AssignOp::LogicAnd.to_zig_str(), "and=");
124        assert_eq!(AssignOp::Nullish.to_zig_str(), "orelse=");
125    }
126
127    #[test]
128    fn test_bin_op_coverage() {
129        // Smoke test: all variants are constructible
130        let ops = [
131            BinOp::Add,
132            BinOp::Sub,
133            BinOp::Mul,
134            BinOp::Div,
135            BinOp::Mod,
136            BinOp::Lt,
137            BinOp::Gt,
138            BinOp::Le,
139            BinOp::Ge,
140            BinOp::Eq,
141            BinOp::Ne,
142            BinOp::StrictEq,
143            BinOp::StrictNe,
144            BinOp::BitAnd,
145            BinOp::BitOr,
146            BinOp::BitXor,
147            BinOp::Shl,
148            BinOp::Shr,
149            BinOp::UrShr,
150            BinOp::In,
151            BinOp::InstanceOf,
152            BinOp::Pow,
153        ];
154        assert_eq!(ops.len(), 22);
155    }
156}