add support for continue statement#112
Merged
lolleko merged 6 commits intoTypeScriptToLua:masterfrom Jun 7, 2018
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This implements the support for TS's
continuestatement. Normally, tstl would throw an exception whenever thecontinuestatement is used, saying that Lua does not support the statement.However, while Lua indeed does not support the
continuestatement, it is still perfectly possible to implement the functionality of a continue statement by changing how loops get transpiled.Here's a simple example:
The following TS
Gets transpiled to
As can be seen from the example above, the functionality of a continue statement gets achieved via a clever usage of the
repeat until trueblock, which acts a simpledo endblock, but allows for a possibility of stopping the execution of the block via abreakstatement. The____continuelocal allows for differentiation between abreakstatement for therepeat until trueand the parent loop. If abreakstatement was used without setting the____continuelocal to true, then it will have an effect of a normal break (break fromrepeat until trueand break from the parent loop). However, if abreakstatement was used after setting the____continuelocal to true, then it will act with accordance to how the effect of thecontinuestatement should be, which is: stop executing the current cycle, and jump to the next one (breakrepeat until true, but do not break the parent loop). The____continue = truegets automatically added at the end of the body of the parent loop.So, the
continuestatement simply gets transpiled to:With this implementation, you can nest multiple loops inside of each other, and the
____continuelocals of the nested loops will not interfere with each other, thanks to how scoping works in Lua.The following loops were used for testing:
The transpiled code:
The outcomes match.
I don't really know if there's a certain way that the pull requests should be made, or if there's some kind of standard for the workflow. If I've done something wrong with the presentation, then please don't hesitate to correct me. :)