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
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn test_assign_op_zig_str() {
104        assert_eq!(AssignOp::Assign.to_zig_str(), "=");
105        assert_eq!(AssignOp::LogicAnd.to_zig_str(), "and=");
106        assert_eq!(AssignOp::Nullish.to_zig_str(), "orelse=");
107    }
108
109    #[test]
110    fn test_bin_op_coverage() {
111        // Smoke test: all variants are constructible
112        let ops = [
113            BinOp::Add,
114            BinOp::Sub,
115            BinOp::Mul,
116            BinOp::Div,
117            BinOp::Mod,
118            BinOp::Lt,
119            BinOp::Gt,
120            BinOp::Le,
121            BinOp::Ge,
122            BinOp::Eq,
123            BinOp::Ne,
124            BinOp::StrictEq,
125            BinOp::StrictNe,
126            BinOp::BitAnd,
127            BinOp::BitOr,
128            BinOp::BitXor,
129            BinOp::Shl,
130            BinOp::Shr,
131            BinOp::UrShr,
132            BinOp::In,
133            BinOp::InstanceOf,
134            BinOp::Pow,
135        ];
136        assert_eq!(ops.len(), 22);
137    }
138}