Skip to content

Commit

Permalink
Support for Bitwise OR operator in translator codegen frontend (#233)
Browse files Browse the repository at this point in the history
* Support for Bitwise OR operator in translator codegen frontend

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

---------

Signed-off-by: Jyotin Goel <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
gjyotin305 and pre-commit-ci[bot] authored Dec 26, 2024
1 parent 17e4ba0 commit 748fded
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions crosstl/translator/codegen/directx_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def map_operator(self, op):
"MULTIPLY": "*",
"DIVIDE": "/",
"BITWISE_XOR": "^",
"BITWISE_OR": "|",
"BITWISE_AND": "&",
"LESS_THAN": "<",
"GREATER_THAN": ">",
Expand Down
1 change: 1 addition & 0 deletions crosstl/translator/codegen/metal_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ def map_operator(self, op):
"MULTIPLY": "*",
"DIVIDE": "/",
"BITWISE_XOR": "^",
"BITWISE_OR": "|",
"BITWISE_AND": "&",
"LESS_THAN": "<",
"GREATER_THAN": ">",
Expand Down
37 changes: 37 additions & 0 deletions tests/test_translator/test_codegen/test_directx_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,5 +683,42 @@ def test_shift_operators():
pytest.fail("Shift right assignment operator codegen not implemented.")


def test_bitwise_or_operator():
code = """
shader main {
struct VSInput {
vec2 texCoord @ TEXCOORD0;
};
struct VSOutput {
vec4 color @ COLOR;
};
sampler2D iChannel0;
vertex {
VSOutput main(VSInput input) {
VSOutput output;
// Use bitwise OR on texture coordinates (for testing purposes)
output.color = vec4(float(int(input.texCoord.x * 100.0) | 15),
float(int(input.texCoord.y * 100.0) | 15),
0.0, 1.0);
return output;
}
}
fragment {
vec4 main(VSOutput input) @ gl_FragColor {
// Simple fragment shader to display the result of the AND operation
return vec4(input.color.rgb, 1.0);
}
}
}
"""
try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
generated_code = generate_code(ast)
print(generated_code)
except SyntaxError:
pytest.fail("Bitwise OR codegen not implemented")


if __name__ == "__main__":
pytest.main()
37 changes: 37 additions & 0 deletions tests/test_translator/test_codegen/test_metal_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,42 @@ def test_bitwise_operators():
pytest.fail("Bitwise Shift codegen not implemented.")


def test_bitwise_or_operator():
code = """
shader main {
struct VSInput {
vec2 texCoord @ TEXCOORD0;
};
struct VSOutput {
vec4 color @ COLOR;
};
sampler2D iChannel0;
vertex {
VSOutput main(VSInput input) {
VSOutput output;
// Use bitwise AND on texture coordinates (for testing purposes)
output.color = vec4(float(int(input.texCoord.x * 100.0) | 15),
float(int(input.texCoord.y * 100.0) | 15),
0.0, 1.0);
return output;
}
}
fragment {
vec4 main(VSOutput input) @ gl_FragColor {
// Simple fragment shader to display the result of the AND operation
return vec4(input.color.rgb, 1.0);
}
}
}
"""
try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
generated_code = generate_code(ast)
print(generated_code)
except SyntaxError:
pytest.fail("Bitwise OR codegen not implemented")


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

0 comments on commit 748fded

Please sign in to comment.