From 83013986188e96dc88f026c6a6663184bccfa088 Mon Sep 17 00:00:00 2001 From: R-unic Date: Sun, 12 Jan 2025 12:04:18 -0500 Subject: [PATCH 1/2] fix: For statement rendering --- RobloxCS.Luau/AST/For.cs | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/RobloxCS.Luau/AST/For.cs b/RobloxCS.Luau/AST/For.cs index 2c88638..017716c 100644 --- a/RobloxCS.Luau/AST/For.cs +++ b/RobloxCS.Luau/AST/For.cs @@ -6,9 +6,9 @@ public class For : Statement public Expression Iterable { get; } public Statement Body { get; } - public For(List initializers, Expression iterable, Statement body) + public For(List names, Expression iterable, Statement body) { - Names = initializers; + Names = names; Iterable = iterable; Body = body; AddChildren(Names); @@ -18,27 +18,13 @@ public For(List 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(); From a789f18ddea9937c2f4d947f1a4300638a2452e9 Mon Sep 17 00:00:00 2001 From: R-unic Date: Sun, 12 Jan 2025 12:08:47 -0500 Subject: [PATCH 2/2] test: fix iterative for loop test case --- RobloxCS.Luau/AstUtility.cs | 4 +++- RobloxCS.Tests/RenderingTest.cs | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/RobloxCS.Luau/AstUtility.cs b/RobloxCS.Luau/AstUtility.cs index 697b60d..ef3e412 100644 --- a/RobloxCS.Luau/AstUtility.cs +++ b/RobloxCS.Luau/AstUtility.cs @@ -9,6 +9,8 @@ namespace RobloxCS.Luau; public static class AstUtility { + public static readonly IdentifierName DiscardName = new("_"); + // TODO: make per-scope /// file path -> dictionary(identifier name, amount of times identifier is used) private static readonly Dictionary> _identifierDeclarations = []; @@ -322,7 +324,7 @@ valueParent is ExpressionStatementSyntax ? /// local _ = discardedValue public static Variable DiscardVariable(SyntaxNode node, Expression value) => - new(CreateSimpleName(node, "_"), true, value); + new(DiscardName, true, value); public static GenericName? GetGenericName(Name name) => name switch diff --git a/RobloxCS.Tests/RenderingTest.cs b/RobloxCS.Tests/RenderingTest.cs index 2be8a69..0db3efe 100644 --- a/RobloxCS.Tests/RenderingTest.cs +++ b/RobloxCS.Tests/RenderingTest.cs @@ -89,10 +89,10 @@ public void Renders_SingleLineComment() [Fact] public void Renders_IterativeFor() { - var name = new IdentifierName("value"); + var valueName = new IdentifierName("value"); var iterable = new IdentifierName("abc"); - var body = new ExpressionStatement(AstUtility.PrintCall(name)); - var forStatement = new For([name], iterable, body); + var body = new ExpressionStatement(AstUtility.PrintCall(valueName)); + var forStatement = new For([AstUtility.DiscardName, valueName], iterable, body); var output = Render(forStatement); const string expectedOutput = """ for _, value in abc do