Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BITWISE_OR operator support in lexer and parser #209

Merged
merged 5 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crosstl/src/backend/DirectX/DirectxLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
("BITWISE_XOR", r"\^"),
("AND", r"&&"),
("OR", r"\|\|"),
("BITWISE_OR", r"\|"),
("DOT", r"\."),
("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
Expand Down
4 changes: 4 additions & 0 deletions crosstl/src/backend/DirectX/DirectxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def parse_variable_declaration_or_assignment(self):
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
"BITWISE_OR",
"BITWISE_XOR",
]:
# Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
Expand All @@ -258,6 +259,7 @@ def parse_variable_declaration_or_assignment(self):
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
"BITWISE_OR",
"BITWISE_XOR",
]:
# Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
Expand All @@ -280,6 +282,7 @@ def parse_variable_declaration_or_assignment(self):
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
"BITWISE_OR",
"BITWISE_XOR",
]:
op = self.current_token[1]
Expand Down Expand Up @@ -410,6 +413,7 @@ def parse_assignment(self):
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
"BITWISE_OR",
"BITWISE_XOR",
]:
op = self.current_token[1]
Expand Down
28 changes: 27 additions & 1 deletion tests/test_backend/test_directx/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def test_else_if_codegen():
pytest.fail("Else_if statement parsing or code generation not implemented.")


def test_assignment_ops_parsing():
def test_assignment_ops_codegen():
code = """
PSOutput PSMain(PSInput input) {
PSOutput output;
Expand Down Expand Up @@ -460,5 +460,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()
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 @@ -157,5 +157,16 @@ def test_assignment_ops_tokenization():
pytest.fail("assign_op tokenization is not implemented.")


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


if __name__ == "__main__":
pytest.main()
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 @@ -221,5 +221,29 @@ def test_assignment_ops_parsing():
pytest.fail("assign_op parsing not implemented.")


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("bitwise_op parsing not implemented.")


if __name__ == "__main__":
pytest.main()
Loading