From c815c0f5ad5b0afdbc82a457508c0619bfcf18d1 Mon Sep 17 00:00:00 2001 From: anshikavashistha Date: Mon, 28 Oct 2024 19:03:31 +0530 Subject: [PATCH 1/4] -s --- crosstl/src/backend/DirectX/DirectxLexer.py | 1 + crosstl/src/backend/DirectX/DirectxParser.py | 4 ++++ tests/test_backend/test_directx/test_codegen.py | 8 ++++++++ tests/test_backend/test_directx/test_lexer.py | 17 +++++++++++++++++ tests/test_backend/test_directx/test_parser.py | 12 ++++++++++++ 5 files changed, 42 insertions(+) diff --git a/crosstl/src/backend/DirectX/DirectxLexer.py b/crosstl/src/backend/DirectX/DirectxLexer.py index c9756df..23c93d7 100644 --- a/crosstl/src/backend/DirectX/DirectxLexer.py +++ b/crosstl/src/backend/DirectX/DirectxLexer.py @@ -50,6 +50,7 @@ ("ASSIGN_AND", r"\&="), ("AND", r"&&"), ("OR", r"\|\|"), + ("BITWISE_OR", r"\|"), ("DOT", r"\."), ("MULTIPLY", r"\*"), ("DIVIDE", r"/"), diff --git a/crosstl/src/backend/DirectX/DirectxParser.py b/crosstl/src/backend/DirectX/DirectxParser.py index 45778a0..75890ed 100644 --- a/crosstl/src/backend/DirectX/DirectxParser.py +++ b/crosstl/src/backend/DirectX/DirectxParser.py @@ -239,6 +239,7 @@ def parse_variable_declaration_or_assignment(self): "ASSIGN_OR", "ASSIGN_AND", "SHIFT_LEFT", + "BITWISE_OR", ]: # Handle assignment operators (e.g., =, +=, -=, ^=, etc.) op = self.current_token[1] @@ -257,6 +258,7 @@ def parse_variable_declaration_or_assignment(self): "ASSIGN_OR", "ASSIGN_AND", "SHIFT_LEFT", + "BITWISE_OR", ]: # Handle assignment operators (e.g., =, +=, -=, ^=, etc.) op = self.current_token[1] @@ -278,6 +280,7 @@ def parse_variable_declaration_or_assignment(self): "ASSIGN_OR", "ASSIGN_AND", "SHIFT_LEFT", + "BITWISE_OR", ]: op = self.current_token[1] self.eat(self.current_token[0]) @@ -407,6 +410,7 @@ def parse_assignment(self): "ASSIGN_OR", "ASSIGN_AND", "SHIFT_LEFT", + "BITWISE_OR", ]: op = self.current_token[1] self.eat(self.current_token[0]) diff --git a/tests/test_backend/test_directx/test_codegen.py b/tests/test_backend/test_directx/test_codegen.py index b261eef..19cd843 100644 --- a/tests/test_backend/test_directx/test_codegen.py +++ b/tests/test_backend/test_directx/test_codegen.py @@ -403,6 +403,14 @@ def test_else_if_codegen(): except SyntaxError: pytest.fail("Else_if statement parsing or code generation not implemented.") +def test_bitwise_or_codegen(): + code = """ + uint val = 0x01; + val = val | 0x02; + """ + generated_code = codegen.generate(code) + assert "| 0x02" in generated_code + def test_assignment_ops_parsing(): code = """ diff --git a/tests/test_backend/test_directx/test_lexer.py b/tests/test_backend/test_directx/test_lexer.py index f9b72c4..fad79b8 100644 --- a/tests/test_backend/test_directx/test_lexer.py +++ b/tests/test_backend/test_directx/test_lexer.py @@ -147,6 +147,15 @@ def test_assignment_ops_tokenization(): redValue &= 0x3; } + // Testing BITWISE_XOR (^) operator on some condition + if (input.in_position.r == 0.5) { + uint redValue = asuint(output.out_color.r); + output.redValue ^= 0x1; + // BITWISE_XOR operation + output.out_color.r = asfloat(redValue); + } + + return output; } @@ -156,6 +165,14 @@ def test_assignment_ops_tokenization(): except SyntaxError: pytest.fail("assign_op tokenization is not implemented.") +def test_bitwise_or_tokenization(): + code = """ + uint val = 0x01; + val = val | 0x02; + """ + tokens = lexer.tokenize(code) + assert ("BITWISE_OR", "|") in tokens + if __name__ == "__main__": pytest.main() diff --git a/tests/test_backend/test_directx/test_parser.py b/tests/test_backend/test_directx/test_parser.py index ba40b85..a66932b 100644 --- a/tests/test_backend/test_directx/test_parser.py +++ b/tests/test_backend/test_directx/test_parser.py @@ -205,7 +205,19 @@ def test_assignment_ops_parsing(): return output; } + + """ + + def test_bitwise_or_parsing(): + code = """ + uint val = 0x01; + if (val | 0x02) { + // Test case for bitwise OR + } """ + parsed_output = parser.parse(code) + assert "BITWISE_OR" in parsed_output + try: tokens = tokenize_code(code) parse_code(tokens) From b62722f01d3e330eeb59005c48e2403bc237dea6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 28 Oct 2024 13:35:34 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_backend/test_directx/test_codegen.py | 1 + tests/test_backend/test_directx/test_lexer.py | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/test_backend/test_directx/test_codegen.py b/tests/test_backend/test_directx/test_codegen.py index 19cd843..3717039 100644 --- a/tests/test_backend/test_directx/test_codegen.py +++ b/tests/test_backend/test_directx/test_codegen.py @@ -403,6 +403,7 @@ def test_else_if_codegen(): except SyntaxError: pytest.fail("Else_if statement parsing or code generation not implemented.") + def test_bitwise_or_codegen(): code = """ uint val = 0x01; diff --git a/tests/test_backend/test_directx/test_lexer.py b/tests/test_backend/test_directx/test_lexer.py index fad79b8..e900037 100644 --- a/tests/test_backend/test_directx/test_lexer.py +++ b/tests/test_backend/test_directx/test_lexer.py @@ -165,6 +165,7 @@ def test_assignment_ops_tokenization(): except SyntaxError: pytest.fail("assign_op tokenization is not implemented.") + def test_bitwise_or_tokenization(): code = """ uint val = 0x01; From bf6bf181b9aec2d5f2c6ce0a86a61d9fe34b93af Mon Sep 17 00:00:00 2001 From: samthakur587 Date: Mon, 28 Oct 2024 21:44:30 +0530 Subject: [PATCH 3/4] fix: added some more test cases --- .../test_backend/test_directx/test_codegen.py | 37 ++++++++++++++----- tests/test_backend/test_directx/test_lexer.py | 19 +++------- .../test_backend/test_directx/test_parser.py | 34 +++++++++++------ 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/tests/test_backend/test_directx/test_codegen.py b/tests/test_backend/test_directx/test_codegen.py index 3717039..ba4e259 100644 --- a/tests/test_backend/test_directx/test_codegen.py +++ b/tests/test_backend/test_directx/test_codegen.py @@ -404,16 +404,7 @@ def test_else_if_codegen(): pytest.fail("Else_if statement parsing or code generation not implemented.") -def test_bitwise_or_codegen(): - code = """ - uint val = 0x01; - val = val | 0x02; - """ - generated_code = codegen.generate(code) - assert "| 0x02" in generated_code - - -def test_assignment_ops_parsing(): +def test_assignment_ops_codegem(): code = """ PSOutput PSMain(PSInput input) { PSOutput output; @@ -460,5 +451,31 @@ def test_assignment_ops_parsing(): pytest.fail("assignment ops parsing or code generation not implemented.") +def test_bitwise_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 OR + } + 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_op parsing or codegen not implemented.") + + if __name__ == "__main__": pytest.main() diff --git a/tests/test_backend/test_directx/test_lexer.py b/tests/test_backend/test_directx/test_lexer.py index e900037..296866f 100644 --- a/tests/test_backend/test_directx/test_lexer.py +++ b/tests/test_backend/test_directx/test_lexer.py @@ -147,15 +147,6 @@ def test_assignment_ops_tokenization(): redValue &= 0x3; } - // Testing BITWISE_XOR (^) operator on some condition - if (input.in_position.r == 0.5) { - uint redValue = asuint(output.out_color.r); - output.redValue ^= 0x1; - // BITWISE_XOR operation - output.out_color.r = asfloat(redValue); - } - - return output; } @@ -168,11 +159,13 @@ def test_assignment_ops_tokenization(): def test_bitwise_or_tokenization(): code = """ - uint val = 0x01; - val = val | 0x02; + uint val = 0x01; + val = val | 0x02; """ - tokens = lexer.tokenize(code) - assert ("BITWISE_OR", "|") in tokens + try: + tokenize_code(code) + except SyntaxError: + pytest.fail("bitwise_op tokenization is not implemented.") if __name__ == "__main__": diff --git a/tests/test_backend/test_directx/test_parser.py b/tests/test_backend/test_directx/test_parser.py index a66932b..0d8de16 100644 --- a/tests/test_backend/test_directx/test_parser.py +++ b/tests/test_backend/test_directx/test_parser.py @@ -205,24 +205,36 @@ def test_assignment_ops_parsing(): return output; } - """ + try: + tokens = tokenize_code(code) + parse_code(tokens) + except SyntaxError: + pytest.fail("assign_op parsing not implemented.") - def test_bitwise_or_parsing(): - code = """ - uint val = 0x01; - if (val | 0x02) { - // Test case for bitwise OR - } - """ - parsed_output = parser.parse(code) - assert "BITWISE_OR" in parsed_output +def test_bitwise_ops_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 OR + } + 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("assign_op parsing not implemented.") + pytest.fail("bitwise_op parsing not implemented.") if __name__ == "__main__": From 27d582d437c97fbd2444d2dba3d3afcf4a0e7f16 Mon Sep 17 00:00:00 2001 From: samunder singh <83540902+samthakur587@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:49:49 +0530 Subject: [PATCH 4/4] small lint fix --- tests/test_backend/test_directx/test_codegen.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_backend/test_directx/test_codegen.py b/tests/test_backend/test_directx/test_codegen.py index 9e93ef4..59294a9 100644 --- a/tests/test_backend/test_directx/test_codegen.py +++ b/tests/test_backend/test_directx/test_codegen.py @@ -404,7 +404,7 @@ def test_else_if_codegen(): pytest.fail("Else_if statement parsing or code generation not implemented.") -def test_assignment_ops_codegem(): +def test_assignment_ops_codegen(): code = """ PSOutput PSMain(PSInput input) { PSOutput output;