Skip to content

Commit

Permalink
Small Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwith2427 committed Sep 9, 2024
1 parent 7e62aab commit 90d0640
Show file tree
Hide file tree
Showing 5 changed files with 631 additions and 48 deletions.
36 changes: 1 addition & 35 deletions crosstl/src/backend/Opengl/OpenglParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,41 +333,7 @@ def parse_variable(self, type_name):
value = self.parse_expression()
if self.current_token[0] == "SEMICOLON":
self.eat("SEMICOLON")
if op == "ASSIGN_ADD":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode("", name), "PLUS", value)
)
elif op == "ASSIGN_MUL":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode("", name), "MULTIPLY", value)
)
elif op == "ASSIGN_DIV":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode("", name), "DIVIDE", value)
)
elif op == "ASSIGN_SUB":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode("", name), "MINUS", value)
)
elif op == "ASSIGN_XOR":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode("", name), "BITWISE_XOR", value)
)
elif op == "ASSIGN_OR":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode("", name), "BITWISE_OR", value)
)
elif op == "ASSIGN_AND":
return AssignmentNode(
VariableNode(type_name, name),
BinaryOpNode(VariableNode(type_name, name), "BITWISE_AND", value)
)
return AssignmentNode(VariableNode(type_name, name),BinaryOpNode(VariableNode("", name), op, value))
else:
raise SyntaxError(
f"Expected ';' after compound assignment, found: {self.current_token[0]}"
Expand Down
9 changes: 8 additions & 1 deletion crosstl/src/backend/Opengl/openglCrossglCodegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ def map_operator(self, op):
"OR": "||",
"BITWISE_OR": "|",
"BITWISE_XOR": "^",
"BITWISE_AND": "&"
"BITWISE_AND": "&",
"ASSIGN_ADD": "+",
"ASSIGN_MUL": "*",
"ASSIGN_DIV": "/",
"ASSIGN_SUB": "-",
"ASSIGN_OR": "|",
"ASSIGN_XOR": "^",
"ASSIGN_AND": "&",
}
return op_map.get(op, op)
291 changes: 291 additions & 0 deletions tests/test_backend/test_opengl/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,294 @@ def test_function_call():
print(code)
except SyntaxError:
pytest.fail("Struct parsing not implemented.")


def test_bitwise_and():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
int bit = 7&4;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Bitwise and operation failed")


def test_bitwise_or():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
int bit = 4|6;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Bitwise or operation failed")


def test_bitwise_xor():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
int bit = 9^4;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Bitwise xor operation failed")


def test_uint():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
unsigned int u = 4;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("uint operation failed")


def test_double():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
double r = 7.87;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("double operation failed")


def test_assign_and():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
int bit = 9&4;
bit&=7;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Assign and operation failed")


def test_assign_or():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
int bit = 9|4;
bit|=7;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Assign or operation failed")


def test_assign_xor():
code = """
#version 450
// Vertex shader
float perlinNoise(vec2 p) {
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
layout(location = 0) in vec3 position;
out vec2 vUV;
void main() {
vUV = position.xy * 10.0;
float noise = perlinNoise(vUV);
}
// Fragment shader
in vec2 vUV;
layout(location = 0) out vec4 fragColor;
void main() {
float noise = perlinNoise(vUV);
float height = noise * 10.0;
int bit = 9^4;
bit^=7;
vec3 color = vec3(height / 10.0, 1.0 - height / 10.0, 0.0);
}
"""

try:
tokens = tokenize_code(code)
ast = parse_code(tokens)
code = generate_code(ast)
print(code)
except SyntaxError:
pytest.fail("Assign xor operation failed")
Loading

0 comments on commit 90d0640

Please sign in to comment.