From 1e7b2557b5ae88bc4e38a2dd46974071baedaeff Mon Sep 17 00:00:00 2001 From: C B Dev Narayan Date: Fri, 20 Sep 2024 15:40:39 +0530 Subject: [PATCH] Add Support for Bitwise shift opeators (#166) * added test for bitwise shift tokens * added translator support for bitwise shift tokens * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../src/translator/codegen/directx_codegen.py | 2 ++ .../src/translator/codegen/metal_codegen.py | 2 ++ .../src/translator/codegen/opengl_codegen.py | 2 ++ .../test_codegen/test_directx_codegen.py | 31 ++++++++++++++++++- .../test_codegen/test_metal_codegen.py | 29 +++++++++++++++++ .../test_codegen/test_opengl_codegen.py | 29 +++++++++++++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/crosstl/src/translator/codegen/directx_codegen.py b/crosstl/src/translator/codegen/directx_codegen.py index 62581235..cfd41bda 100644 --- a/crosstl/src/translator/codegen/directx_codegen.py +++ b/crosstl/src/translator/codegen/directx_codegen.py @@ -372,5 +372,7 @@ def map_operator(self, op): "OR": "||", "EQUALS": "=", "ASSIGN_SHIFT_LEFT": "<<=", + "BITWISE_SHIFT_RIGHT": ">>", + "BITWISE_SHIFT_LEFT": "<<", } return op_map.get(op, op) diff --git a/crosstl/src/translator/codegen/metal_codegen.py b/crosstl/src/translator/codegen/metal_codegen.py index 0cb808b2..fe958af7 100644 --- a/crosstl/src/translator/codegen/metal_codegen.py +++ b/crosstl/src/translator/codegen/metal_codegen.py @@ -432,5 +432,7 @@ def map_operator(self, op): "OR": "||", "EQUALS": "=", "ASSIGN_SHIFT_LEFT": "<<=", + "BITWISE_SHIFT_RIGHT": ">>", + "BITWISE_SHIFT_LEFT": "<<", } return op_map.get(op, op) diff --git a/crosstl/src/translator/codegen/opengl_codegen.py b/crosstl/src/translator/codegen/opengl_codegen.py index 08aa9a15..317b7f65 100644 --- a/crosstl/src/translator/codegen/opengl_codegen.py +++ b/crosstl/src/translator/codegen/opengl_codegen.py @@ -289,5 +289,7 @@ def map_operator(self, op): "OR": "||", "EQUALS": "=", "ASSIGN_SHIFT_LEFT": "<<=", + "BITWISE_SHIFT_RIGHT": ">>", + "BITWISE_SHIFT_LEFT": "<<", } return op_map.get(op, op) diff --git a/tests/test_translator/test_codegen/test_directx_codegen.py b/tests/test_translator/test_codegen/test_directx_codegen.py index b94ca3ef..0e4160a4 100644 --- a/tests/test_translator/test_codegen/test_directx_codegen.py +++ b/tests/test_translator/test_codegen/test_directx_codegen.py @@ -418,7 +418,36 @@ def test_assignment_shift_operators(): code = generate_code(ast) print(code) except SyntaxError: - pytest.fail("Struct parsing not implemented.") + pytest.fail("Assignment shift parsing not implemented.") + + +def test_bitwise_operators(): + code = """ + shader LightControl { + vertex { + input vec3 position; + output int isLightOn; + void main() { + isLightOn = 2 >> 1; + } + } + fragment { + input int isLightOn; + output vec4 fragColor; + void main() { + isLightOn = isLightOn << 1; + } + } + } + + """ + try: + tokens = tokenize_code(code) + ast = parse_code(tokens) + code = generate_code(ast) + print(code) + except SyntaxError: + pytest.fail("Bitwise Shift parsing not implemented.") if __name__ == "__main__": diff --git a/tests/test_translator/test_codegen/test_metal_codegen.py b/tests/test_translator/test_codegen/test_metal_codegen.py index f16c1df8..9585b2db 100644 --- a/tests/test_translator/test_codegen/test_metal_codegen.py +++ b/tests/test_translator/test_codegen/test_metal_codegen.py @@ -323,5 +323,34 @@ def test_assignment_shift_operators(): pytest.fail("Struct parsing not implemented.") +def test_bitwise_operators(): + code = """ + shader LightControl { + vertex { + input vec3 position; + output int isLightOn; + void main() { + isLightOn = 2 >> 1; + } + } + fragment { + input int isLightOn; + output vec4 fragColor; + void main() { + isLightOn = isLightOn << 1; + } + } + } + + """ + try: + tokens = tokenize_code(code) + ast = parse_code(tokens) + code = generate_code(ast) + print(code) + except SyntaxError: + pytest.fail("Bitwise Shift parsing not implemented.") + + if __name__ == "__main__": pytest.main() diff --git a/tests/test_translator/test_codegen/test_opengl_codegen.py b/tests/test_translator/test_codegen/test_opengl_codegen.py index 40f55026..ef56b95f 100644 --- a/tests/test_translator/test_codegen/test_opengl_codegen.py +++ b/tests/test_translator/test_codegen/test_opengl_codegen.py @@ -323,5 +323,34 @@ def test_assignment_shift_operators(): pytest.fail("Struct parsing not implemented.") +def test_bitwise_operators(): + code = """ + shader LightControl { + vertex { + input vec3 position; + output int isLightOn; + void main() { + isLightOn = 2 >> 1; + } + } + fragment { + input int isLightOn; + output vec4 fragColor; + void main() { + isLightOn = isLightOn << 1; + } + } + } + + """ + try: + tokens = tokenize_code(code) + ast = parse_code(tokens) + code = generate_code(ast) + print(code) + except SyntaxError: + pytest.fail("Bitwise Shift parsing not implemented.") + + if __name__ == "__main__": pytest.main()