Skip to content

Commit

Permalink
Support for bitwise_and operator in directx backend (#236)
Browse files Browse the repository at this point in the history
* Support for bitwise_and operator in directx backend

Signed-off-by: Jyotin Goel <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Jyotin Goel <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Nripesh Niketan <[email protected]>
Co-authored-by: Nripesh Niketan <[email protected]>
  • Loading branch information
4 people authored Jan 3, 2025
1 parent 5db64c9 commit a80766d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions crosstl/backend/DirectX/DirectxLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
("DEFAULT", r"\bdefault\b"),
("BREAK", r"\bbreak\b"),
("MOD", r"%"),
("BITWISE_AND", r"&"),
]
)

Expand Down
4 changes: 4 additions & 0 deletions crosstl/backend/DirectX/DirectxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def parse_variable_declaration_or_assignment(self):
"SHIFT_LEFT",
"SHIFT_RIGHT",
"BITWISE_OR",
"BITWISE_AND",
"BITWISE_XOR",
]:
# Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
Expand All @@ -279,6 +280,7 @@ def parse_variable_declaration_or_assignment(self):
"SHIFT_LEFT",
"SHIFT_RIGHT",
"BITWISE_OR",
"BITWISE_AND",
"BITWISE_XOR",
]:
# Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
Expand All @@ -303,6 +305,7 @@ def parse_variable_declaration_or_assignment(self):
"SHIFT_LEFT",
"SHIFT_RIGHT",
"BITWISE_OR",
"BITWISE_AND",
"BITWISE_XOR",
]:
op = self.current_token[1]
Expand Down Expand Up @@ -435,6 +438,7 @@ def parse_assignment(self):
"SHIFT_LEFT",
"SHIFT_RIGHT",
"BITWISE_OR",
"BITWISE_AND",
"BITWISE_XOR",
]:
op = self.current_token[1]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_backend/test_directx/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,33 @@ def test_switch_case_codegen():
pytest.fail("Switch-case parsing or code generation not implemented.")


def test_bitwise_and_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 AND
}
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_and_op codegen not implemented.")


def test_double_dtype_codegen():
code = """
PSOutput PSMain(PSInput input) {
Expand Down
11 changes: 11 additions & 0 deletions tests/test_backend/test_directx/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ def test_switch_case_tokenization():
pytest.fail("switch-case tokenization not implemented.")


def test_bitwise_and_tokenization():
code = """
uint val = 0x01;
val = val & 0x02;
"""
try:
tokenize_code(code)
except SyntaxError:
pytest.fail("bitwise_and_op tokenization is not implemented.")


def test_double_dtype_tokenization():
code = """
PSOutput PSMain(PSInput input) {
Expand Down
24 changes: 24 additions & 0 deletions tests/test_backend/test_directx/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ def test_switch_case_parsing():
pytest.fail("switch-case parsing not implemented.")


def test_bitwise_and_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 AND
}
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("bitwise_and_op parsing not implemented.")


def test_double_dtype_parsing():
code = """
PSOutput PSMain(PSInput input) {
Expand Down

0 comments on commit a80766d

Please sign in to comment.