Skip to content

Commit

Permalink
Merge pull request #34 from roblox-csharp/test/more-generation
Browse files Browse the repository at this point in the history
Fix for statement rendering
  • Loading branch information
R-unic authored Jan 12, 2025
2 parents 4d9175f + f5174e0 commit 7aeabaa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 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
4 changes: 3 additions & 1 deletion RobloxCS.Luau/AstUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace RobloxCS.Luau;

public static class AstUtility
{
public static readonly IdentifierName DiscardName = new("_");

// TODO: make per-scope
/// <summary>file path -> dictionary(identifier name, amount of times identifier is used)</summary>
private static readonly Dictionary<string, Dictionary<string, uint>> _identifierDeclarations = [];
Expand Down Expand Up @@ -322,7 +324,7 @@ valueParent is ExpressionStatementSyntax ?

/// <code>local _ = discardedValue</code>
public static Variable DiscardVariable(SyntaxNode node, Expression value) =>
new(CreateSimpleName<IdentifierName>(node, "_"), true, value);
new(DiscardName, true, value);

public static GenericName? GetGenericName(Name name) =>
name switch
Expand Down
6 changes: 3 additions & 3 deletions RobloxCS.Tests/RenderingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7aeabaa

Please sign in to comment.