Repository: CrossGL/crosstl

Issue:
BITWISE_AND : &
<!-- BOT_STATE: {} -->

Discussion:

Patch:
diff --git a/crosstl/backend/DirectX/DirectxLexer.py b/crosstl/backend/DirectX/DirectxLexer.py
index a85cc9d..e6c4c28 100644
--- a/crosstl/backend/DirectX/DirectxLexer.py
+++ b/crosstl/backend/DirectX/DirectxLexer.py
@@ -103,6 +103,7 @@ TOKENS = tuple(
         ("DEFAULT", r"\bdefault\b"),
         ("BREAK", r"\bbreak\b"),
         ("MOD", r"%"),
+        ("BITWISE_AND", r"&"),
     ]
 )
 
diff --git a/crosstl/backend/DirectX/DirectxParser.py b/crosstl/backend/DirectX/DirectxParser.py
index 5c5ff80..1c46952 100644
--- a/crosstl/backend/DirectX/DirectxParser.py
+++ b/crosstl/backend/DirectX/DirectxParser.py
@@ -258,6 +258,7 @@ class HLSLParser:
                     "SHIFT_LEFT",
                     "SHIFT_RIGHT",
                     "BITWISE_OR",
+                    "BITWISE_AND",
                     "BITWISE_XOR",
                 ]:
                     # Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
@@ -279,6 +280,7 @@ class HLSLParser:
                 "SHIFT_LEFT",
                 "SHIFT_RIGHT",
                 "BITWISE_OR",
+                "BITWISE_AND",
                 "BITWISE_XOR",
             ]:
                 # Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
@@ -303,6 +305,7 @@ class HLSLParser:
                     "SHIFT_LEFT",
                     "SHIFT_RIGHT",
                     "BITWISE_OR",
+                    "BITWISE_AND",
                     "BITWISE_XOR",
                 ]:
                     op = self.current_token[1]
@@ -435,6 +438,7 @@ class HLSLParser:
             "SHIFT_LEFT",
             "SHIFT_RIGHT",
             "BITWISE_OR",
+            "BITWISE_AND",
             "BITWISE_XOR",
         ]:
             op = self.current_token[1]


Test patch:
diff --git a/tests/test_backend/test_directx/test_codegen.py b/tests/test_backend/test_directx/test_codegen.py
index 505a08f..e82bc25 100644
--- a/tests/test_backend/test_directx/test_codegen.py
+++ b/tests/test_backend/test_directx/test_codegen.py
@@ -575,6 +575,33 @@ def test_switch_case_codegen():
         pytest.fail("Switch-case parsing or code generation not implemented.")
 
 
+def test_bitwise_and_ops_codgen():
+    code = """
+        PSOutput PSMain(PSInput input) {
+            PSOutput output;
+            output.out_color = float4(0.0, 0.0, 0.0, 1.0);
+            uint val = 0x01;
+            if (val & 0x02) {
+                // Test case for bitwise AND
+            }
+            uint filterA = 0b0001; // First filter
+            uint filterB = 0b1000; // Second filter
+
+            // Merge both filters
+            uint combinedFilter = filterA & filterB; // combinedFilter becomes 0b1001
+            return output;
+        }
+        
+        """
+    try:
+        tokens = tokenize_code(code)
+        ast = parse_code(tokens)
+        generated_code = generate_code(ast)
+        print(generated_code)
+    except SyntaxError:
+        pytest.fail("bitwise_and_op codegen not implemented.")
+
+
 def test_double_dtype_codegen():
     code = """
             PSOutput PSMain(PSInput input) {
diff --git a/tests/test_backend/test_directx/test_lexer.py b/tests/test_backend/test_directx/test_lexer.py
index 8542492..661ec9d 100644
--- a/tests/test_backend/test_directx/test_lexer.py
+++ b/tests/test_backend/test_directx/test_lexer.py
@@ -228,6 +228,17 @@ def test_switch_case_tokenization():
         pytest.fail("switch-case tokenization not implemented.")
 
 
+def test_bitwise_and_tokenization():
+    code = """
+        uint val = 0x01;
+        val = val & 0x02;
+    """
+    try:
+        tokenize_code(code)
+    except SyntaxError:
+        pytest.fail("bitwise_and_op tokenization is not implemented.")
+
+
 def test_double_dtype_tokenization():
     code = """
             PSOutput PSMain(PSInput input) {
diff --git a/tests/test_backend/test_directx/test_parser.py b/tests/test_backend/test_directx/test_parser.py
index 1128734..abad0b4 100644
--- a/tests/test_backend/test_directx/test_parser.py
+++ b/tests/test_backend/test_directx/test_parser.py
@@ -332,6 +332,30 @@ def test_switch_case_parsing():
         pytest.fail("switch-case parsing not implemented.")
 
 
+def test_bitwise_and_parsing():
+    code = """
+        PSOutput PSMain(PSInput input) {
+            PSOutput output;
+            output.out_color = float4(0.0, 0.0, 0.0, 1.0);
+            uint val = 0x01;
+            if (val & 0x02) {
+                // Test case for bitwise AND
+            }
+            uint filterA = 0b0001; // First filter
+            uint filterB = 0b1000; // Second filter
+
+            // Merge both filters
+            uint combinedFilter = filterA & filterB; // combinedFilter becomes 0b1001
+            return output;
+        }
+    """
+    try:
+        tokens = tokenize_code(code)
+        parse_code(tokens)
+    except SyntaxError:
+        pytest.fail("bitwise_and_op parsing not implemented.")
+
+
 def test_double_dtype_parsing():
     code = """
             PSOutput PSMain(PSInput input) {