From 18466a14b0746969d74ce990da60ef3e32e98efa Mon Sep 17 00:00:00 2001 From: Yogesh <126279793+themaverick@users.noreply.github.com> Date: Thu, 26 Dec 2024 13:54:34 +0530 Subject: [PATCH] added logical_or to diretx backend (#231) --- crosstl/backend/DirectX/DirectxLexer.py | 2 +- crosstl/backend/DirectX/DirectxParser.py | 4 +-- tests/test_backend/test_directx/test_lexer.py | 11 ++++++++ .../test_backend/test_directx/test_parser.py | 25 +++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/crosstl/backend/DirectX/DirectxLexer.py b/crosstl/backend/DirectX/DirectxLexer.py index a2c6084f..208b51d0 100644 --- a/crosstl/backend/DirectX/DirectxLexer.py +++ b/crosstl/backend/DirectX/DirectxLexer.py @@ -51,7 +51,7 @@ ("ASSIGN_AND", r"\&="), ("BITWISE_XOR", r"\^"), ("AND", r"&&"), - ("OR", r"\|\|"), + ("LOGICAL_OR", r"\|\|"), ("BITWISE_OR", r"\|"), ("DOT", r"\."), ("MULTIPLY", r"\*"), diff --git a/crosstl/backend/DirectX/DirectxParser.py b/crosstl/backend/DirectX/DirectxParser.py index c3cd409d..b1c3b5b7 100644 --- a/crosstl/backend/DirectX/DirectxParser.py +++ b/crosstl/backend/DirectX/DirectxParser.py @@ -428,9 +428,9 @@ def parse_assignment(self): def parse_logical_or(self): left = self.parse_logical_and() - while self.current_token[0] == "OR": + while self.current_token[0] == "LOGICAL_OR": op = self.current_token[1] - self.eat("OR") + self.eat("LOGICAL_OR") right = self.parse_logical_and() left = BinaryOpNode(left, op, right) return left diff --git a/tests/test_backend/test_directx/test_lexer.py b/tests/test_backend/test_directx/test_lexer.py index 0b12c360..b19a7455 100644 --- a/tests/test_backend/test_directx/test_lexer.py +++ b/tests/test_backend/test_directx/test_lexer.py @@ -182,5 +182,16 @@ def test_bitwise_or_tokenization(): pytest.fail("bitwise_op tokenization is not implemented.") +def test_logical_or_tokenization(): + code = """ + bool val_0 = true; + bool val_1 = val_0 || false; + """ + try: + tokenize_code(code) + except SyntaxError: + pytest.fail("logical_or tokenization is not implemented.") + + 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 dd73e9df..3426e3eb 100644 --- a/tests/test_backend/test_directx/test_parser.py +++ b/tests/test_backend/test_directx/test_parser.py @@ -257,5 +257,30 @@ def test_bitwise_ops_parsing(): pytest.fail("bitwise_op parsing not implemented.") +def test_logical_or_ops_parsing(): + code = """ + PSOutput PSMain(PSInput input) { + PSOutput output; + output.out_color = float4(0.0, 0.0, 0.0, 1.0); + // Test case for logical OR + bool condition1 = true; // First condition + bool condition2 = false; // Second condition + if (condition1 || condition2) { + // If one of the condition is true + output.out_color = float4(1.0, 0.0, 0.0, 1.0); // Set color to red + } else { + // If both of the conditions are false + output.out_color = float4(0.0, 1.0, 0.0, 1.0); // Set color to green + } + return output; + } + """ + try: + tokens = tokenize_code(code) + parse_code(tokens) + except SyntaxError: + pytest.fail("logical_or_ops not implemented.") + + if __name__ == "__main__": pytest.main()