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

Support for Bitwise OR operator in translator codegen frontend #233

Merged
merged 2 commits into from
Dec 26, 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/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()
Loading