Repository: CrossGL/crosstl
Pull request #153 (2024-09-12 14:55:52)

Add Support for Bitwise XOR opeator(^) Token in Translator Codegen Frontend #108
### PR Description
This PR implements support for the Bitwise Operator (^) token in the CrossGL project, addressing issue #108  . The token is now properly translated across the DirectX, Metal, and OpenGL backends.

### Related Issue
#108 

### shader Sample

```crossgl
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);
        }
    }
    }
```


### Checklist
- [X] Have you added the necessary tests?
- [ ] Only modified the files mentioned in the related issue(s)?
- [X] Are all tests passing?

For supporting bit-wise operator support maybe `crosstl/src/translator/parser.py ` needs to be modified


Unic-X: @samthakur587 @vaatsalya123 can you review and this ! 

Patch:
diff --git a/crosstl/src/translator/codegen/directx_codegen.py b/crosstl/src/translator/codegen/directx_codegen.py
index 479a746..a9adf5c 100644
--- a/crosstl/src/translator/codegen/directx_codegen.py
+++ b/crosstl/src/translator/codegen/directx_codegen.py
@@ -354,6 +354,7 @@ class HLSLCodeGen:
             "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 1c5d261..256c0f0 100644
--- a/crosstl/src/translator/codegen/metal_codegen.py
+++ b/crosstl/src/translator/codegen/metal_codegen.py
@@ -414,6 +414,7 @@ class MetalCodeGen:
             "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 52644b5..af99062 100644
--- a/crosstl/src/translator/codegen/opengl_codegen.py
+++ b/crosstl/src/translator/codegen/opengl_codegen.py
@@ -271,6 +271,7 @@ class GLSLCodeGen:
             "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 74a4b79..5b84282 100644
--- a/crosstl/src/translator/lexer.py
+++ b/crosstl/src/translator/lexer.py
@@ -54,7 +54,6 @@ TOKENS = [
     ("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 9c14fec..83f0daa 100644
--- a/crosstl/src/translator/parser.py
+++ b/crosstl/src/translator/parser.py
@@ -644,6 +644,7 @@ class Parser:
             "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 @@ class Parser:
             "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 @@ class Parser:
             "GREATER_EQUAL",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "EQUAL",
             "ASSIGN_AND",
             "ASSIGN_OR",
@@ -796,6 +799,7 @@ class Parser:
             "ASSIGN_MOD",
             "BITWISE_SHIFT_RIGHT",
             "BITWISE_SHIFT_LEFT",
+            "BITWISE_XOR",
             "ASSIGN_SHIFT_RIGHT",
             "ASSIGN_SHIFT_LEFT",
         ]:
@@ -962,6 +966,7 @@ class Parser:
             "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 40b1da2..c02468d 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()