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

Add else if to translator #32 #104

Conversation

AxelB1011
Copy link
Contributor

@AxelB1011 AxelB1011 commented Aug 29, 2024

PR Description

Modified the following files in the crosstl/src/translator directory:

parser.py: Updated the the parse_if_statement() function.
directx_codegen.py: Implemented else if for HLSL output.
metal_codegen.py: Implemented else if for Metal output.
opengl_codegen.py: Implemented else if for GLSL output.
test_lexer.py, test_parser.py, test_directx_codegen.py, test_metal_codegen.py, test_opengl_codegen.py: Added tests for multiple else if conditionals in test_translator and test_translator/test_codegen.

Related Issue

Closed #32

shader Sample

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;
            }
            if (vUV.x < 0.25) {
                vUV.x = 0.0;
            } 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);
            }
        }
    }

Checklist

  • Have you added the necessary tests?
  • Only modified the files mentioned in the related issue(s)?
  • Are all tests passing?

AxelB1011 and others added 20 commits August 20, 2024 14:07
* feat: Implement tokens for unsigned int and double data types.

* chore: Add test for data type tokenization

* feat: Refactor unsigned int to uint after review and edit tests for same
@AxelB1011 AxelB1011 requested a review from NripeshN as a code owner August 29, 2024 10:00
@AxelB1011
Copy link
Contributor Author

AxelB1011 commented Aug 29, 2024

@samthakur587 I have created a new PR as per your advice. Please let me know if this is alright. It might be better to squash all the commits that I've made into one or just cherry pick the latest one but I'm not sure if that is okay

@samthakur587
Copy link
Contributor

hii @AxelB1011 great work so far just one last thing to fix . let's assume you have two continues if statements how do you handle this . the parser not parse this second if state.

can you try this example code for test your 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;
            }
            if (vUV.x < 0.25) {
                vUV.x = 0.0;
            }
            
            } else if (vUV.x < 0.75) {
                vUV.x = 0.5;
            } 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 {
                fragColor = vec4(0.0, 0.0, 0.0, 1.0);
            }
            fragColor = vec4(color, 1.0);
            }
        }
    }

@samthakur587
Copy link
Contributor

you can take reference from this PR . i fixed this issue here . #39

@AxelB1011
Copy link
Contributor Author

AxelB1011 commented Aug 29, 2024

Thank you for the feedback @samthakur587! I have used your code for reference and updated the tests. The parser seems to be working fine, unless I am missing something..?

hii @AxelB1011 great work so far just one last thing to fix . let's assume you have two continues if statements how do you handle this . the parser not parse this second if state.

can you try this example code for test your 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;
            }
            if (vUV.x < 0.25) {
                vUV.x = 0.0;
            } else if (vUV.x < 0.75) {
                vUV.x = 0.5;
            } 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 {
                fragColor = vec4(0.0, 0.0, 0.0, 1.0);
            }
            fragColor = vec4(color, 1.0);
            }
        }
    }

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 @AxelB1011 really a great work everything LGTM 🚀

Thanks for your contribution

@samthakur587 samthakur587 merged commit d4a35e6 into CrossGL:main Sep 5, 2024
36 checks passed
@samthakur587 samthakur587 mentioned this pull request Sep 6, 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 Assignment AND Token at translator frontend Add else if Conditional Statements to the translator
5 participants