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

[Feature] Implement control-flow statements continue and break for Kipper loop statements #269

Closed
6 of 7 tasks
Tracked by #417 ...
Luna-Klatzer opened this issue Aug 3, 2022 · 7 comments · Fixed by #417 or #419
Closed
6 of 7 tasks
Tracked by #417 ...
Assignees
Labels
feature New feature or enhancement hacktoberfest Hacktoberfest-related issue or pull request
Milestone

Comments

@Luna-Klatzer
Copy link
Member

Luna-Klatzer commented Aug 3, 2022

Is there an existing proposal for this?

  • I have searched the existing issues

This feature does not exist in the latest version

  • I am using the latest version

Proposal

Implement control-flow statements and keywords continue and break, which should be usable to either jump to the next iteration of a loop or stop a loop prematurely. The syntax and behaviour should be exactly the same as in the JS implementation and follow the guidelines/docs of MDN continue and MDN break.

Exact behaviour / changes you want

  • Implement the new reserved keyword break.
  • Implement the new reserved keyword continue.
  • Implement semantic analysis for the statements and ensure they are only used inside of loops.
  • Implement warnings for unreachable code, if a line of code is directly after a break statement.
  • Add translation support for TypeScript and JavaScript ([Feature] Implement new translation target for JavaScript #208).
@Luna-Klatzer Luna-Klatzer added feature New feature or enhancement question Further information is requested labels Aug 3, 2022
@Luna-Klatzer Luna-Klatzer self-assigned this Aug 3, 2022
@Luna-Klatzer Luna-Klatzer added hacktoberfest Hacktoberfest-related issue or pull request and removed question Further information is requested labels Oct 1, 2022
@GeekGuy-29
Copy link

Hey there can you assign this issue to me?
PS:- Can you briefly explain as in where do you want these statements to be implemented.

@Luna-Klatzer
Copy link
Member Author

Luna-Klatzer commented Oct 1, 2022

Sure! It's relatively simple since the syntax for break and continue has been already implemented, as well as the class JumpStatement, so you won't have to deal with the Antlr4 Parser. So the major thing that still needs implementation is to properly ensure that they are used in the right context cases, e.g. inside a loop and not anywhere else.

This can be done during semantic analysis, which is handled here:
https://github.com/Luna-Klatzer/Kipper/blob/e9e357a0a7f6fe1f24515b8be0a501c6cc6f75cf/kipper/core/src/compiler/semantics/language/statements.ts#L529-L545

PS: If you do start working on it, please create a new branch extending 183-feature-implement-functions-definitions-declarations-and-call-expressions-for-user-defined-functions, since that's the branch where currently return statements have been implemented and so there have been some changes to the handling of return, continue and break. This will make it easier for me to merge them and avoid conflicts! For further questions, you can always ask me and thanks in advance :)

@GeekGuy-29
Copy link

Alright got your point.
Just a small issue, since I am a newbie, can you tell me how should I run the semantic analysis so that, I can get the points where I have to implement the correction.

Very lastly,
post semantic analysis and fixing the errors, IG my PR can be merged right?

@GeekGuy-29
Copy link

First off !
Hats off to you ma'am, you made this much on your own and, that's splendid.

I just wanted to say one thing.
All the issues related to continue and break statements are in the 183 branch right? So, if I am right then, rather than checking via semantic analysis, I can manually check for those..and implement the corrections.
Lemme know if I am right.
I would really love to contribute ma'am, thank you for the opportunity.

@Luna-Klatzer
Copy link
Member Author

Luna-Klatzer commented Oct 2, 2022

Alright got your point. Just a small issue, since I am a newbie, can you tell me how should I run the semantic analysis so that, I can get the points where I have to implement the correction.

Very lastly, post semantic analysis and fixing the errors, IG my PR can be merged right?

The semantic analysis function of JumpStatement is automatically called whenever the function KipperCompiler.compile() is called for a compilation. So you won't have to worry about calling it since it's automatically done by the compiler, since it already has an algorithm to go from top to bottom and check every item.

So for testing, you can either build the project and run the CLI file run, or create a new Mocha test testing for your case here.

Also considering that you will have to throw an error for the user, I would recommend creating a new function in KipperSemanticChecker, where the function is called from the primarySemanticAnalysis and throws an error if something is invalid like here for an example:

https://github.com/Luna-Klatzer/Kipper/blob/7f8c6b9070138902406793b7c03587faeb24916e/kipper/core/src/compiler/semantics/language/expressions.ts#L1530-L1532

Which calls this function here:

https://github.com/Luna-Klatzer/Kipper/blob/7f8c6b9070138902406793b7c03587faeb24916e/kipper/core/src/compiler/semantics/processor/semantic-checker.ts#L147-L154

To that, you likely will need to create a new error in kipper/core/src/errors.ts, where you specify an error message such as Jump statements used outside loop with an error class extending KipperSyntaxError for example. For how to do that just look at the existing errors and copy the same scheme.

And sure, if you finish the check the PR is good to go! :)

@Luna-Klatzer
Copy link
Member Author

First off ! Hats off to you ma'am, you made this much on your own and, that's splendid.

I just wanted to say one thing. All the issues related to continue and break statements are in the 183 branch right? So, if I am right then, rather than checking via semantic analysis, I can manually check for those..and implement the corrections. Lemme know if I am right. I would really love to contribute ma'am, thank you for the opportunity.

Oh the feature is not implemented in any branch yet, just the 183-feature-implement-functions-definitions-declarations-and-call-expressions-for-user-defined-functions branch is where I have made changes to the class structure, so it's better if you fork and then make a new branch from there, so I can later merge it more easily :)

@Luna-Klatzer
Copy link
Member Author

Luna-Klatzer commented Feb 18, 2023

Implemented by #417 and will be released in next release.

@Luna-Klatzer Luna-Klatzer unpinned this issue Feb 18, 2023
@Luna-Klatzer Luna-Klatzer mentioned this issue Feb 19, 2023
13 tasks
@Luna-Klatzer Luna-Klatzer linked a pull request Feb 19, 2023 that will close this issue
13 tasks
@Luna-Klatzer Luna-Klatzer mentioned this issue Feb 19, 2023
34 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment