Skip to content

Commit

Permalink
Add Support for Bitwise shift opeators (#166)
Browse files Browse the repository at this point in the history
* added test for bitwise shift tokens

* added translator support for bitwise shift tokens

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

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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
DeVcB13d and pre-commit-ci[bot] authored Sep 20, 2024
1 parent 6b1526e commit 1e7b255
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crosstl/src/translator/codegen/directx_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,5 +372,7 @@ def map_operator(self, op):
"OR": "||",
"EQUALS": "=",
"ASSIGN_SHIFT_LEFT": "<<=",
"BITWISE_SHIFT_RIGHT": ">>",
"BITWISE_SHIFT_LEFT": "<<",
}
return op_map.get(op, op)
2 changes: 2 additions & 0 deletions crosstl/src/translator/codegen/metal_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,7 @@ def map_operator(self, op):
"OR": "||",
"EQUALS": "=",
"ASSIGN_SHIFT_LEFT": "<<=",
"BITWISE_SHIFT_RIGHT": ">>",
"BITWISE_SHIFT_LEFT": "<<",
}
return op_map.get(op, op)
2 changes: 2 additions & 0 deletions crosstl/src/translator/codegen/opengl_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,7 @@ def map_operator(self, op):
"OR": "||",
"EQUALS": "=",
"ASSIGN_SHIFT_LEFT": "<<=",
"BITWISE_SHIFT_RIGHT": ">>",
"BITWISE_SHIFT_LEFT": "<<",
}
return op_map.get(op, op)
31 changes: 30 additions & 1 deletion tests/test_translator/test_codegen/test_directx_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,36 @@ def test_assignment_shift_operators():
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Struct parsing not implemented.")
pytest.fail("Assignment shift parsing not implemented.")


def test_bitwise_operators():
code = """
shader LightControl {
vertex {
input vec3 position;
output int isLightOn;
void main() {
isLightOn = 2 >> 1;
}
}
fragment {
input int isLightOn;
output vec4 fragColor;
void main() {
isLightOn = isLightOn << 1;
}
}
}
"""
try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Bitwise Shift parsing not implemented.")


if __name__ == "__main__":
Expand Down
29 changes: 29 additions & 0 deletions tests/test_translator/test_codegen/test_metal_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,34 @@ def test_assignment_shift_operators():
pytest.fail("Struct parsing not implemented.")


def test_bitwise_operators():
code = """
shader LightControl {
vertex {
input vec3 position;
output int isLightOn;
void main() {
isLightOn = 2 >> 1;
}
}
fragment {
input int isLightOn;
output vec4 fragColor;
void main() {
isLightOn = isLightOn << 1;
}
}
}
"""
try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Bitwise Shift parsing not implemented.")


if __name__ == "__main__":
pytest.main()
29 changes: 29 additions & 0 deletions tests/test_translator/test_codegen/test_opengl_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,34 @@ def test_assignment_shift_operators():
pytest.fail("Struct parsing not implemented.")


def test_bitwise_operators():
code = """
shader LightControl {
vertex {
input vec3 position;
output int isLightOn;
void main() {
isLightOn = 2 >> 1;
}
}
fragment {
input int isLightOn;
output vec4 fragColor;
void main() {
isLightOn = isLightOn << 1;
}
}
}
"""
try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Bitwise Shift parsing not implemented.")


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

0 comments on commit 1e7b255

Please sign in to comment.