Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelB1011 committed Aug 22, 2024
1 parent 28748e9 commit d3eb7c2
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 24 deletions.
10 changes: 5 additions & 5 deletions crosstl/src/translator/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ def __init__(
self,
if_condition,
if_body,
else_if_condition=None,
else_if_body=None,
else_if_conditions=None,
else_if_bodies=None,
else_body=None,
):
self.if_condition = if_condition
self.if_body = if_body
self.else_if_condition = else_if_condition
self.else_if_body = else_if_body
self.else_if_conditions = else_if_conditions or []
self.else_if_bodies = else_if_bodies or []
self.else_body = else_body

def __repr__(self):
return f"IfNode(if_condition={self.if_condition}, if_body={self.if_body}, else_if_condition={self.else_if_condition}, else_if_body={self.else_if_body}, else_body={self.else_body})"
return f"IfNode(if_condition={self.if_condition}, if_body={self.if_body}, else_if_conditions={self.else_if_conditions}, else_if_bodies={self.else_if_bodies}, else_body={self.else_body})"


class ForNode(ASTNode):
Expand Down
6 changes: 3 additions & 3 deletions crosstl/src/translator/codegen/directx_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@ def generate_if(self, node, indent, shader_type=None):
code += self.generate_statement(stmt, indent + 1, shader_type)
code += f"{indent_str}}}"

while node.else_if_body:
code += f" else if ({self.generate_expression(node.else_if_condition, shader_type)}) {{\n"
for stmt in node.else_if_body:
for else_if_condition, else_if_body in zip(node.else_if_conditions, node.else_if_bodies):
code += f" else if ({self.generate_expression(else_if_condition, shader_type)}) {{\n"
for stmt in else_if_body:
code += self.generate_statement(stmt, indent + 1, shader_type)
code += f"{indent_str}}}"

Expand Down
6 changes: 3 additions & 3 deletions crosstl/src/translator/codegen/metal_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,9 @@ def generate_if(self, node, indent, shader_type=None):
code += self.generate_statement(stmt, indent + 1, shader_type)
code += f"{indent_str}}}"

while node.else_if_body:
code += f" else if ({self.generate_expression(node.else_if_condition, shader_type)}) {{\n"
for stmt in node.else_if_body:
for else_if_condition, else_if_body in zip(node.else_if_conditions, node.else_if_bodies):
code += f" else if ({self.generate_expression(else_if_condition, shader_type)}) {{\n"
for stmt in else_if_body:
code += self.generate_statement(stmt, indent + 1, shader_type)
code += f"{indent_str}}}"

Expand Down
6 changes: 3 additions & 3 deletions crosstl/src/translator/codegen/opengl_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ def generate_if(self, node, indent, shader_type=None):
code += self.generate_statement(stmt, indent + 1, shader_type)
code += f"{indent_str}}}"

while node.else_if_body:
code += f" else if ({self.generate_expression(node.else_if_condition, shader_type)}) {{\n"
for stmt in node.else_if_body:
for else_if_condition, else_if_body in zip(node.else_if_conditions, node.else_if_bodies):
code += f" else if ({self.generate_expression(else_if_condition, shader_type)}) {{\n"
for stmt in else_if_body:
code += self.generate_statement(stmt, indent + 1, shader_type)
code += f"{indent_str}}}"

Expand Down
2 changes: 0 additions & 2 deletions crosstl/src/translator/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
("ASSIGN_DIV", r"/="),
("WHITESPACE", r"\s+"),
("IF", r"\bif\b"),
("ELSE_IF", r"\belse\sif\b"),
("ELSE", r"\belse\b"),
("FOR", r"\bfor\b"),
("RETURN", r"\breturn\b"),
Expand Down Expand Up @@ -76,7 +75,6 @@
"vertex": "VERTEX",
"fragment": "FRAGMENT",
"if": "IF",
"else if": "ELSE_IF",
"else": "ELSE",
"for": "FOR",
"return": "RETURN",
Expand Down
15 changes: 9 additions & 6 deletions crosstl/src/translator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,20 @@ def parse_if_statement(self):
self.eat("LBRACE")
if_body = self.parse_body()
self.eat("RBRACE")
else_if_condition = None
else_if_body = None
else_if_condition = []
else_if_body = []
else_body = None
while self.current_token[0] == "ELSE_IF":
self.eat("ELSE_IF")

while self.current_token[0] == "ELSE" and self.peak(1)[0] == "IF":
self.eat("ELSE")
self.eat("IF")
self.eat("LPAREN")
else_if_condition = self.parse_expression()
else_if_condition.append(self.parse_expression())
self.eat("RPAREN")
self.eat("LBRACE")
else_if_body = self.parse_body()
else_if_body.append(self.parse_body())
self.eat("RBRACE")

if self.current_token[0] == "ELSE":
self.eat("ELSE")
self.eat("LBRACE")
Expand Down
19 changes: 18 additions & 1 deletion tests/test_translator/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def test_else_statement_tokenization():
except SyntaxError:
pytest.fail("Struct parsing not implemented.")

def test_else_if_statement_tokenization():
code = """
if (a > b) {
return a;
} else if (a < b) {
return b;
} else if (a == b) {
return 1;
} else {
return 0;
}
"""
try:
tokenize_code(code)
except SyntaxError:
pytest.fail("Struct parsing not implemented.")


def test_function_call_tokenization():
code = """
Expand Down Expand Up @@ -112,4 +129,4 @@ def test_data_types_tokenization():
try:
tokenize_code(code)
except SyntaxError:
pytest.fail("Data types tokenization not implemented.")
pytest.fail("Data types tokenization not implemented.")
49 changes: 48 additions & 1 deletion tests/test_translator/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,53 @@ def test_else_statement():
except SyntaxError:
pytest.fail("Struct parsing not implemented.")

def test_else_if_statement():
code = """
shader PerlinNoise {
vertex {
input vec3 position;
output vec2 vUV;
void main() {
vUV = position.xy * 10.0;
if (vUV.x < 0.5) {
vUV.x = 0.25;
} else if (vUV.x < 0.75) {
vUV.x = 0.5;
} else if (vUV.x < 1.0) {
vUV.x = 0.75;
} else {
vUV.x = 0.0;
}
gl_Position = vec4(position, 1.0);
}
}
// Fragment Shader
fragment {
input vec2 vUV;
output vec4 fragColor;
void main() {
if (vUV.x > 0.75) {
fragColor = vec4(1.0, 1.0, 1.0, 1.0);
} else if (vUV.x > 0.5) {
fragColor = vec4(0.5, 0.5, 0.5, 1.0);
} else if (vUV.x > 0.25) {
fragColor = vec4(0.25, 0.25, 0.25, 1.0);
} else {
fragColor = vec4(0.0, 0.0, 0.0, 1.0);
}
fragColor = vec4(color, 1.0);
}
}
}
"""
try:
tokens = tokenize_code(code)
parse_code(tokens)
except SyntaxError:
pytest.fail("Struct parsing not implemented.")

def test_function_call():
code = """
Expand Down Expand Up @@ -206,4 +253,4 @@ def test_function_call():
tokens = tokenize_code(code)
parse_code(tokens)
except SyntaxError:
pytest.fail("Struct parsing not implemented.")
pytest.fail("Struct parsing not implemented.")

0 comments on commit d3eb7c2

Please sign in to comment.