Closed
Conversation
Also fixed scoping issue with for loops which transpile to lua while statements
Prevented use of numerical for loops when the loop variable is modified in the loops body. This is done by tracking symbols that are assigned in scopes and checking that when deciding how to transpile the for statement.
also: - added checking for object destructuring in for...of - fixed lua for...in in source maps
…-of-operation issues in numerical for limit
…ypeScriptToLua into smart-for-loop
…tToLua into smart-for-loop
Collaborator
Author
|
I'm taking this out of draft as I'm not seeing any ways to simplify/clarify the code any better. |
Contributor
|
Offset limit when using < or > is always 1 now, but it should be equal to increment: for (let i = 0; i < 2; i += 0.5) {
console.log(i); // 0 0.5 1 1.5
}for i = 0, 1, 0.5 do
print(i) -- 0 0.5 1
end |
Collaborator
Author
|
After looking at this more closely, setting the offset to the step value only works in certain situations. for (let i = 0; i < 1; i += 0.3) {
console.log(i); // 0, 0.3, 0.6, 0.9
}for i = 0, 1 - 0.3, 0.3 do
print(i) -- 0, 0.3, 0.6
end |
…whole number literal as a step value
Collaborator
Author
|
For now, I'm dis-allowing optimization on loops using |
Contributor
|
True, what about using epsilon? for i = 0, 1 - 2.220446049250313e-16, 0.3 do
print(i) -- 0, 0.3, 0.6, 0.9
end
for i = 0, 2 - 2.220446049250313e-16, 0.5 do
print(i) -- 0, 0.5, 1, 1.5
end |
Collaborator
Author
|
I considered that, but any value we use could have precision issues in some lua implementations. It's too bad lua doesn't have a built in const for that like math.huge |
tomblind
added a commit
that referenced
this pull request
May 25, 2019
Extracted from #585 Also includes: - Throwing error on attempts to use object destructuring in for...of loops - Fixed issue with sourcemaps in lua for...in loops
Collaborator
Author
|
Closing as implicit optimization is out of scope of the main TSTL project. This is better done as a plugin. |
tomblind
added a commit
that referenced
this pull request
May 26, 2019
Extracted from #585 Also includes: - Throwing error on attempts to use object destructuring in for...of loops - Fixed issue with sourcemaps in lua for...in loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Allows certain for loops to be transpiled as lua numerical for loops.
=>
imust be declared withletand not modified inside the loop bodyi < limitori <= limitfor forward (addition) loopsi > limitori >= limitfor reverse (subtraction) loopslimitmust be either:<=or>=<or>(to avoid issues with offsetting)++i,--i, etc...)i += xori -= x)i = i + xori = i - x)There is also support for a special case:
=>
Since this is a common paradigm in (oldschool) javscript, it seemed like a good thing to support. Note that it will only be transpiled as a numerical
forif thelimitvariable is not referenced in the the loop body at all.Also included in this PR:
for...ofloops over arrays now useipairsinstead of a numerical lua for loopfor...ofloops (this silently produced bad code before)for...instatements