Skip to content

Commit

Permalink
fix: For statement rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Jan 12, 2025
1 parent da70747 commit 8301398
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions RobloxCS.Luau/AST/For.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ public class For : Statement
public Expression Iterable { get; }
public Statement Body { get; }

public For(List<IdentifierName> initializers, Expression iterable, Statement body)
public For(List<IdentifierName> names, Expression iterable, Statement body)
{
Names = initializers;
Names = names;
Iterable = iterable;
Body = body;
AddChildren(Names);
Expand All @@ -18,27 +18,13 @@ public For(List<IdentifierName> initializers, Expression iterable, Statement bod

public override void Render(LuauWriter luau)
{
var singleValueIteration = Names.Count == 1;
luau.Write("for _, ");
if (singleValueIteration)
Names.First().Render(luau);
else
luau.Write("_binding");
luau.Write("for ");
luau.WriteNodesCommaSeparated(Names);

luau.Write(" in ");
Iterable.Render(luau);
luau.WriteLine(" do");
luau.PushIndent();

if (!singleValueIteration)
{
var index = 0;
foreach (var name in Names)
{
var indexLiteral = new Literal((++index).ToString());
luau.WriteVariable(name, true, new ElementAccess(new IdentifierName("_binding"), indexLiteral));
}
}

Body.Render(luau);
luau.PopIndent();
Expand Down

0 comments on commit 8301398

Please sign in to comment.