Skip to content

Commit

Permalink
fix: add one to numerical index values
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Jul 13, 2024
1 parent f081f0c commit 8323470
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Roblox CSharp to Lua compiler
- String concatenation
- Default values (via `default` keyword)
- Classes outside of namespaces
- Minify `if condition then return end` generation
- Macro `IEnumerable<T>` (or just `List<T>`) methods
- Classes/structs/interfaces nested in classes/structs/interfaces
- Restrict `volatile` and `partial` usage (`partial` maybe supported in the future)
Expand Down
30 changes: 25 additions & 5 deletions RobloxCS/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public override void VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpre
{
Write("return ");
Visit(node.ExpressionBody);
WriteLine();
}

_indent--;
Expand All @@ -205,6 +206,7 @@ public override void VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax no
{
Write("return ");
Visit(node.ExpressionBody);
WriteLine();
}

_indent--;
Expand All @@ -216,7 +218,24 @@ public override void VisitBracketedArgumentList(BracketedArgumentListSyntax node
Write('[');
foreach (var argument in node.Arguments)
{
Visit(argument);
if (argument.Expression is LiteralExpressionSyntax numericalLiteral)
{
int.TryParse(numericalLiteral.Token.ValueText, out var indexValue);
Write((indexValue + 1).ToString());
}
else
{
var argumentSymbol = _semanticModel.GetSymbolInfo(argument.Expression).Symbol;
var definitionSymbol = argumentSymbol?.OriginalDefinition;
var indexSymbol = definitionSymbol ?? argumentSymbol;
var isNumericalIndex = indexSymbol is ITypeSymbol typeSymbol && Constants.INTEGER_TYPES.Contains(typeSymbol.Name);
Visit(argument);
if (isNumericalIndex)
{
Write(" + 1");
}
}

if (argument != node.Arguments.Last())
{
Write(", ");
Expand Down Expand Up @@ -634,23 +653,24 @@ public override void VisitIdentifierName(IdentifierNameSyntax node)

if (isLeftSide && !localScopeIncludesIdentifier && !runtimeNamespaceIncludesIdentifier)
{
// TODO: check for inherited members
var parentNamespace = FindFirstAncestor<NamespaceDeclarationSyntax>(node);
var namespaceIncludesIdentifier = parentNamespace != null && parentNamespace.Members
.Where(member => GetNames(member).Contains(identifierName))
.Count() > 0;

var parentClass = FindFirstAncestor<ClassDeclarationSyntax>(node);
var classIncludesIdentifier = parentClass != null && parentClass.Members
var classMember= parentClass?.Members
.Where(member => GetName(member) == identifierName)
.Count() > 0;
.FirstOrDefault();

if (namespaceIncludesIdentifier)
{
Write($"namespace[\"$getMember\"](namespace, \"{identifierName}\")");
}
else if (classIncludesIdentifier)
else if (classMember != null)
{
Write($"class.{identifierName}");
Write($"{(HasSyntax(classMember.Modifiers, SyntaxKind.StaticKeyword) ? "class" : "self")}.{identifierName}");
}
else
{
Expand Down
12 changes: 12 additions & 0 deletions RobloxCS/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ internal static class Constants
{
"as"
};

public static readonly HashSet<string> INTEGER_TYPES = new HashSet<string>
{
"sbyte",
"byte",
"short",
"ushort",
"int",
"uint",
"long",
"ulong"
};
}
}
18 changes: 13 additions & 5 deletions TestProject/dist/client/Main.client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ CS.namespace("TestGame", function(namespace)

function class.Main()
namespace["$getMember"](namespace, "ComponentRunner").AttachTag("Lava", function(instance)
return namespace["$getMember"](namespace, "LavaComponent").new(instance)end)
return namespace["$getMember"](namespace, "LavaComponent").new(instance)
end)
end

if namespace == nil then
Expand All @@ -25,11 +26,12 @@ CS.namespace("TestGame", function(namespace)
function class.new(instance)
local self = setmetatable({}, class)

print("lava component created")
self.Instance = instance
print(`lava component created with {instance}`)

function self.Start()
print("lava component started")
CS.getAssemblyType("Instance").Touched:Connect(function(hit)
self.Instance.Touched:Connect(function(hit)
local model = hit:FindFirstAncestorOfClass("Model")
local humanoid = if model == nil then nil else model:FindFirstChildOfClass("Humanoid")
if humanoid == nil then
Expand All @@ -56,11 +58,17 @@ CS.namespace("TestGame", function(namespace)
local attached = false
local instances = game:GetService("CollectionService"):GetTagged(tag)
game:GetService("CollectionService").TagAdded:Connect(function(tag)
local instance = game:GetService("CollectionService"):GetTagged(tag)[0]
if attached then
return
end
local instance = game:GetService("CollectionService"):GetTagged(tag)[1]
print(instance)
class.Run(attachComponent(instance))
attached = true
end)
for _, instance in instances do
if attached then
end
class.Run(attachComponent(instance))
attached = true
end
Expand Down Expand Up @@ -103,7 +111,7 @@ CS.namespace("TestGame", function(namespace)
function class.new(instance)
local self = setmetatable({}, class)

class.Instance = instance
self.Instance = instance


return self
Expand Down
5 changes: 4 additions & 1 deletion TestProject/src/client/Main.client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public static void Main()

public class LavaComponent : GameComponent<Part>
{
public new Part Instance { get; set; }

public LavaComponent(Part instance)
: base(instance)
{
Instance = instance;
Console.WriteLine("lava component created");
Console.WriteLine($"lava component created with {instance}");
}

public override void Start()
Expand Down Expand Up @@ -54,6 +56,7 @@ public static void AttachTag<TComponent>(string tag, Func<Instance, TComponent>
{
if (attached) return;
var instance = Services.CollectionService.GetTagged(tag)[0];
Console.WriteLine(instance);
Run(attachComponent(instance));
attached = true;
});
Expand Down

0 comments on commit 8323470

Please sign in to comment.