From 28f47d6f7439f164adc55053311a43ac8ec70ad8 Mon Sep 17 00:00:00 2001 From: Jyotin Goel Date: Thu, 26 Dec 2024 19:08:56 +0530 Subject: [PATCH] Support for LOGICAL AND operator in DirectX backend Signed-off-by: Jyotin Goel --- 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 208b51d0..077f39e9 100644 --- a/crosstl/backend/DirectX/DirectxLexer.py +++ b/crosstl/backend/DirectX/DirectxLexer.py @@ -50,7 +50,7 @@ ("ASSIGN_OR", r"\|="), ("ASSIGN_AND", r"\&="), ("BITWISE_XOR", r"\^"), - ("AND", r"&&"), + ("LOGICAL_AND", r"&&"), ("LOGICAL_OR", r"\|\|"), ("BITWISE_OR", r"\|"), ("DOT", r"\."), diff --git a/crosstl/backend/DirectX/DirectxParser.py b/crosstl/backend/DirectX/DirectxParser.py index b1c3b5b7..2e2523ec 100644 --- a/crosstl/backend/DirectX/DirectxParser.py +++ b/crosstl/backend/DirectX/DirectxParser.py @@ -437,9 +437,9 @@ def parse_logical_or(self): def parse_logical_and(self): left = self.parse_equality() - while self.current_token[0] == "AND": + while self.current_token[0] == "LOGICAL_AND": op = self.current_token[1] - self.eat("AND") + self.eat("LOGICAL_AND") right = self.parse_equality() 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 b19a7455..9318f12c 100644 --- a/tests/test_backend/test_directx/test_lexer.py +++ b/tests/test_backend/test_directx/test_lexer.py @@ -193,5 +193,16 @@ def test_logical_or_tokenization(): pytest.fail("logical_or tokenization is not implemented.") +def test_logical_and_tokenization(): + code = """ + bool val_0 = true; + bool val_1 = val_0 && false; + """ + try: + tokenize_code(code) + except SyntaxError: + pytest.fail("logical_and 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 3426e3eb..b85b03d5 100644 --- a/tests/test_backend/test_directx/test_parser.py +++ b/tests/test_backend/test_directx/test_parser.py @@ -282,5 +282,30 @@ def test_logical_or_ops_parsing(): pytest.fail("logical_or_ops not implemented.") +def test_logical_and_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 AND + bool condition1 = true; // First condition + bool condition2 = false; // Second condition + if (condition1 && condition2) { + // both the condition is true + output.out_color = float4(1.0, 0.0, 0.0, 1.0); // Set color to red + } else { + // any one of the condition is 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_and_ops not implemented.") + + if __name__ == "__main__": pytest.main()