Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Dictionary macros #33

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions RobloxCS.Luau/Macros.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ private static bool ListMethod(Func<SyntaxNode, Node?> visit, MemberAccessExpres
InvocationExpressionSyntax invocation, out Expression? expanded)
{
expanded = null;
Console.WriteLine(memberAccess.Expression is IdentifierNameSyntax);
switch (memberAccess.Name.Identifier.Text) {
case "Add": {
var arguments = (ArgumentList)visit(invocation.ArgumentList)!;
Expand All @@ -279,6 +280,20 @@ private static bool ListMethod(Func<SyntaxNode, Node?> visit, MemberAccessExpres
expanded = new Call(new QualifiedName(new IdentifierName("table"), new IdentifierName("insert")), arguments);
break;
}
case "Contains": {
var arguments = (ArgumentList)visit(invocation.ArgumentList)!;
var self = (Expression)visit(memberAccess.Expression)!;
arguments.Arguments.Insert(0, new Argument(self));

expanded = new BinaryOperator(new Call(new QualifiedName(new IdentifierName("table"), new IdentifierName("find")), arguments), "~=", AstUtility.Nil());
break;
}
case "Clear": {
var self = (Expression)visit(memberAccess.Expression)!;

expanded = new Call(new QualifiedName(new IdentifierName("table"), new IdentifierName("clear")), new ArgumentList([new Argument(self)]));
break;
}
}

expanded?.MarkExpanded(MacroKind.ListMethod);
Expand All @@ -300,6 +315,20 @@ private static bool DictionaryMethod(Func<SyntaxNode, Node?> visit, MemberAccess
expanded = new Assignment(new ElementAccess(self, key), value);
break;
}
case "ContainsKey": {
var arguments = (ArgumentList)visit(invocation.ArgumentList)!;
var self = (Expression)visit(memberAccess.Expression)!;
var key = arguments.Arguments.First().Expression;

expanded = new BinaryOperator(new ElementAccess(self, key), "~=", AstUtility.Nil());
break;
}
case "Clear": {
var self = (Expression)visit(memberAccess.Expression)!;

expanded = new Call(new QualifiedName(new IdentifierName("table"), new IdentifierName("clear")), new ArgumentList([new Argument(self)]));
break;
}
}

expanded?.MarkExpanded(MacroKind.DictionaryMethod);
Expand Down
30 changes: 30 additions & 0 deletions RobloxCS/LuauGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,36 @@ public override Luau.Node VisitPostfixUnaryExpression(PostfixUnaryExpressionSynt
return new Luau.UnaryOperator(mappedOperator, operand);
}

public override Luau.Call VisitSwitchExpression(SwitchExpressionSyntax node) {
var statements = new List<Luau.Statement>();
var createTempVariable = node.GoverningExpression is not IdentifierNameSyntax && node.GoverningExpression is not LiteralExpressionSyntax;
var condition = Visit<Luau.Expression>(node.GoverningExpression);
var comparand = createTempVariable ?
Luau.AstUtility.CreateSimpleName(node.GoverningExpression, "_exp", registerIdentifier: true)
: condition;

SwitchExpressionArmSyntax? discardPattern = null;
foreach (var section in node.Arms) {
if (section.Pattern is DiscardPatternSyntax) {
discardPattern = section;
continue;
}

var binaryOp = HandlePattern(section.Pattern, comparand);

statements.Add(new Luau.If(binaryOp, new Luau.Return(Visit<Luau.Expression>(section.Expression))));
}

if (createTempVariable)
statements.Insert(0, new Luau.Variable((Luau.IdentifierName)comparand, true, condition));

if (discardPattern != null)
statements.Add(new Luau.Return(Visit<Luau.Expression>(discardPattern.Expression)));

return new Luau.Call(new Luau.Parenthesized(new Luau.AnonymousFunction(new([]), body: new Luau.Block(statements)
)), new([]));
}

// TODO: create VisitCaseSwitchLabel, VisitCasePatternSwitchLabel, VisitDefaultSwitchLabel methods
public override Luau.Block VisitSwitchStatement(SwitchStatementSyntax node)
{
Expand Down
Loading