Repository: CrossGL/crosstl

Issue:
Add translator support for `Bitwise XOR` token

Discussion:
@Unic-X: Can  i work on this issue @samthakur587 ?

Solution patch:
diff --git a/crosstl/src/translator/codegen/directx_codegen.py b/crosstl/src/translator/codegen/directx_codegen.py
index 479a7462..a9adf5ce 100644
--- a/crosstl/src/translator/codegen/directx_codegen.py
+++ b/crosstl/src/translator/codegen/directx_codegen.py
@@ -354,6 +354,7 @@ def map_operator(self, op):
             "MINUS": "-",
             "MULTIPLY": "*",
             "DIVIDE": "/",
+            "BITWISE_XOR": "^",
             "LESS_THAN": "<",
             "GREATER_THAN": ">",
             "ASSIGN_ADD": "+=",
diff --git a/crosstl/src/translator/codegen/metal_codegen.py b/crosstl/src/translator/codegen/metal_codegen.py
index 1c5d2610..256c0f05 100644
--- a/crosstl/src/translator/codegen/metal_codegen.py
+++ b/crosstl/src/translator/codegen/metal_codegen.py
@@ -414,6 +414,7 @@ def map_operator(self, op):
             "MINUS": "-",
             "MULTIPLY": "*",
             "DIVIDE": "/",
+            "BITWISE_XOR": "^",
             "LESS_THAN": "<",
             "ASSIGN_ADD": "+=",
             "ASSIGN_SUB": "-=",
diff --git a/crosstl/src/translator/codegen/opengl_codegen.py b/crosstl/src/translator/codegen/opengl_codegen.py
index 52644b53..af990621 100644
--- a/crosstl/src/translator/codegen/opengl_codegen.py
+++ b/crosstl/src/translator/codegen/opengl_codegen.py
@@ -271,6 +271,7 @@ def map_operator(self, op):
             "MINUS": "-",
             "MULTIPLY": "*",
             "DIVIDE": "/",
+            "BITWISE_XOR": "^",
             "LESS_THAN": "<",
             "ASSIGN_ADD": "+=",
             "ASSIGN_SUB": "-=",
diff --git a/crosstl/src/translator/lexer.py b/crosstl/src/translator/lexer.py
index 74a4b790..5b842822 100644
--- a/crosstl/src/translator/lexer.py
+++ b/crosstl/src/translator/lexer.py
@@ -54,7 +54,6 @@
     ("ASSIGN_XOR", r"\^="),
     ("LOGICAL_AND", r"&&"),
     ("LOGICAL_OR", r"\|\|"),
-    ("XOR", r"\^"),
     ("NOT", r"!"),
     ("ASSIGN_MOD", r"%="),
     ("MOD", r"%"),
diff --git a/crosstl/src/translator/parser.py b/crosstl/src/translator/parser.py
index 9c14fec9..83f0daa0 100644
--- a/crosstl/src/translator/parser.py
+++ b/crosstl/src/translator/parser.py
@@ -644,6 +644,7 @@ def parse_assignment_or_function_call(self, update_condition=False):
             "ASSIGN_MOD",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "ASSIGN_SHIFT_LEFT" "ASSIGN_SHIFT_RIGHT",
         ]:
             return self.parse_assignment(name)
@@ -705,6 +706,7 @@ def parse_variable_declaration(self, type_name, update_condition=False):
             "ASSIGN_MOD",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "ASSIGN_SHIFT_LEFT" "ASSIGN_SHIFT_RIGHT",
         ]:
             op = self.current_token[1]
@@ -736,6 +738,7 @@ def parse_variable_declaration(self, type_name, update_condition=False):
             "GREATER_EQUAL",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "EQUAL",
             "ASSIGN_AND",
             "ASSIGN_OR",
@@ -796,6 +799,7 @@ def parse_assignment(self, name):
             "ASSIGN_MOD",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "ASSIGN_SHIFT_RIGHT",
             "ASSIGN_SHIFT_LEFT",
         ]:
@@ -962,6 +966,7 @@ def parse_expression(self):
             "ASSIGN_MOD",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "ASSIGN_SHIFT_RIGHT",
             "ASSIGN_SHIFT_LEFT",
         ]:


Test patch:
diff --git a/tests/test_translator/test_parser.py b/tests/test_translator/test_parser.py
index 40b1da2c..c02468d7 100644
--- a/tests/test_translator/test_parser.py
+++ b/tests/test_translator/test_parser.py
@@ -444,5 +444,39 @@ def test_bitwise_operators():
         pytest.fail("Bitwise Shift not working")
 
 
+def test_xor_operator():
+    code = """
+    shader XorTestShader {
+    vertex {
+        input vec3 position;
+        output vec2 vUV;
+
+        void main() {
+            // Perform XOR on the x and y components of the position
+            vUV.x = float(int(position.x) ^ 3);  // XOR with 3
+            vUV.y = float(int(position.y) ^ 5);  // XOR with 5
+            gl_Position = vec4(position, 1.0);
+        }
+    }
+
+    fragment {
+        input vec2 vUV;
+        output vec4 fragColor;
+
+        void main() {
+            // Use XOR in fragment shader
+            float result = float(int(vUV.x) ^ int(vUV.y));
+            fragColor = vec4(result / 10.0, 1.0 - result / 10.0, 0.0, 1.0);
+        }
+    }
+    }
+    """
+    try:
+        tokens = tokenize_code(code)
+        parse_code(tokens)
+    except SyntaxError:
+        pytest.fail("Bitwise XOR not working")
+
+
 if __name__ == "__main__":
     pytest.main()