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

Added else if Conditional Statements to the Metal backend #34 #39

Merged
merged 32 commits into from
Aug 29, 2024

Conversation

adityanarayanan343
Copy link
Contributor

@adityanarayanan343 adityanarayanan343 commented Aug 20, 2024

Added Support for else if in MetalParser

  1. Grammar Update: Enhanced the parse_if_statement method to handle else if constructs in Metal syntax.

  2. AST Update: Modified the Abstract Syntax Tree (AST) structure to support else if nodes, ensuring correct hierarchical representation of nested conditional logic.

  3. Code Refactor: Refactored the parse_if_statement method to improve readability and maintainability while integrating else if logic.

Closes #34

@adityanarayanan343
Copy link
Contributor Author

Can u please check what else is there to change in this code?

Copy link
Contributor

@samthakur587 samthakur587 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @adityanarayanan343 for contributing here i'll happy to solve any query if you in understanding the code.

if self.code[pos : pos + 7] == "else if":
self.tokens.append(("ELSE_IF", "else if"))
pos += 7
continue
for token_type, pattern in TOKENS:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hii @adityanarayanan343 here you have added a condition for check else if token. you can directly add Token in TOKENS list and in this MetalLexer class the function tokenize will compile the string and find the if this token is in the code.

f"IfNode(condition={self.condition}, if_body={self.if_body}, "
f"elif_conditions={self.elif_conditions}, "
f"elif_bodies={self.elif_bodies}, else_body={self.else_body})"
)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the changes in IfNode class is lgtm in ast.

crosstl/src/backend/Metal/MetalParser.py Show resolved Hide resolved
@adityanarayanan343
Copy link
Contributor Author

Can you check whether all the fixes you mentioned were addressed and please do tell if there were any other issues.

@NripeshN NripeshN requested a review from samthakur587 August 20, 2024 11:57
@samthakur587
Copy link
Contributor

Can you check whether all the fixes you mentioned were addressed and please do tell if there were any other issues.

Hii @adityanarayanan343 sorry for late reply your changes are looking great 👍

One last thing can you please add a test for your else_if condition at metal backend you can find test files in tests folder. Before that pull the latest changes. 😄

Thanks
Sam

Copy link
Contributor

@samthakur587 samthakur587 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 waiting for tests to be added

@adityanarayanan343
Copy link
Contributor Author

Sure thing @samthakur587 I will do it tomorrow morning and create a pull request.

@samthakur587
Copy link
Contributor

Sure thing @samthakur587 I will do it tomorrow morning and create a pull request.

You can write all tests in this pr also just pull latest changes add test and push on this branch.

@adityanarayanan343
Copy link
Contributor Author

Hi @samthakur587 I have done testing and added if-
Uploading Screenshot 2024-08-22 at 9.08.16 AM.png…
else test case to the test codes please check and comment on any changes.
P.S: Attached Screenshot of getting all test cases passed

@samthakur587
Copy link
Contributor

hii @adityanarayanan343 the tests are passing at metal codegen can you please have a look on this


self = <crosstl.src.backend.Metal.MetalCrossGLCodeGen.MetalToCrossGLConverter object at 0x7f10543909a0>
node = IfNode(condition=BinaryOpNode(left=MemberAccessNode(object=MemberAccessNode(object=input, member=position), member=x),...right=VectorConstructorNode(type_name=float2, args=['0.5', '0.5']))], else_body=None)], elif_bodies=[], else_body=None)
indent = 3, is_main = True

def generate_if_statement(self, node, indent, is_main):
    condition = self.generate_expression(node.condition, is_main)

    code = f"if ({condition}) {{\n"
    code += self.generate_function_body(node.if_body, indent + 1, is_main)
    code += "    " * indent + "}"
    for elif_node in node.elif_conditions:
        elif_condition = self.generate_expression(elif_node.condition, is_main)
        code += f" else if ({elif_condition}) {{\n"
      code += self.generate_function_body(elif_node.body, indent + 1, is_main)

E AttributeError: 'IfNode' object has no attribute 'body'

crosstl/src/backend/Metal/MetalCrossGLCodeGen.py:178: AttributeError
=========================== short test summary info ============================
FAILED tests/test_backend/test_metal/test_codegen.py::test_else_if - AttributeError: 'IfNode' object has no attribute 'body'
========================= 1 failed, 16 passed in 0.19s =========================

@adityanarayanan343
Copy link
Contributor Author

Hi @samthakur587 ,

I think I have fixed the issue so can you please confirm the same thing and do comment on it.

Kudos,
Aditya

@samthakur587
Copy link
Contributor

hii @adityanarayanan343 your tests are passing but not translating the correctly

shader main {
 
    // Vertex Shader
    vertex {
        input vec3 position;
        output vec2 vUV;
        void main() {
                        gl_Position = vec4(position, 1.0);
            if (position.x == position.y) {
                vUV = vec2(0.0, 0.0);
            } else if ([]) {
                vUV = vec2(1.0, 1.0);
            }
                    }
    }

    // Fragment Shader
    fragment {
        input vec2 vUV;
        output vec4 fragColor;
        void main() {
                        if (vUV.x == vUV.y) {
                fragColor = vec4(0.0, 1.0, 0.0, 1.0);
            } else if ([]) {
                fragColor = vec4(1.0, 0.0, 0.0, 1.0);
            }
                    }
    }
}

in else if condition the only [ ] is translated . can you please have a look on this . and can you please write a separate test for else_if not in if condition.

@adityanarayanan343
Copy link
Contributor Author

adityanarayanan343 commented Aug 22, 2024

Hi @samthakur587 I have added a separate case for if and if-else conditions please check this issue and check for any other issues. Also, can you show me where the else if issue [ ] is showing because I can't find it

Aditya

@adityanarayanan343
Copy link
Contributor Author

Hi @vaatsalya123 can you check whether this issue has been fixed or not and can you please comment on it.

Thanks,
Aditya

@samthakur587
Copy link
Contributor

hii @adityanarayanan343 if you run this command .

pytest -v tests/test_backend/test_metal/test_codegen.py -s

then you can see the generated code for your test and if you see for else_if test then in this the condition is generated empty .

@samthakur587 samthakur587 self-requested a review August 24, 2024 12:40
Copy link
Contributor

@samthakur587 samthakur587 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hii @adityanarayanan343 we are very close to merging the PR . just make the last changes i requested .

if_body = self.parse_block()

# Handle `else if`
elif_conditions = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hii @adityanarayanan343 can you have check on this bcz you are combining the elif_body and elif_condition in single list elif_condition which is not a good way to storing it in ast node . even when you have created the 2 different parameters in IFNode is elif_condition and elif_body.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @samthakur587, I think I have added the required changes please do check on it and comment on it.

Copy link
Contributor

@samthakur587 samthakur587 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hii @adityanarayanan343 great work i have fixed some small issues . now everything LGTM 🚀

@samthakur587 samthakur587 merged commit c5bc4c5 into CrossGL:main Aug 29, 2024
36 checks passed
@samthakur587 samthakur587 mentioned this pull request Aug 29, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add else if Conditional Statements to the Metal backend
2 participants