Skip to content

Commit

Permalink
Merge pull request #33 from roblox-csharp/macros
Browse files Browse the repository at this point in the history
More Dictionary macros
  • Loading branch information
R-unic authored Jan 10, 2025
2 parents 36024d8 + 311036c commit 4d9175f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
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

0 comments on commit 4d9175f

Please sign in to comment.