Skip to content

Commit

Permalink
feat: include System.Collections & System.Core core libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Jul 30, 2024
1 parent ef5e632 commit 1829542
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 395 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
bin/
obj/
.vs/
include/
include/
TestProject/
39 changes: 19 additions & 20 deletions RobloxCS/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ private void FullyQualifyMemberAccess(INamespaceSymbol? namespaceType, List<Usin
{
if (namespaceType == null) return;

var typeIsImported = usings.Any(usingDirective => usingDirective.Name != null && Utility.GetNamesFromNode(usingDirective).Contains(namespaceType.ContainingNamespace.Name));
var typeIsImported = usings.Any(usingDirective => namespaceType.ContainingNamespace != null && usingDirective.Name != null && Utility.GetNamesFromNode(usingDirective).Contains(namespaceType.ContainingNamespace.Name));
Write($"CS.getAssemblyType(\"{namespaceType.Name}\").");
_flags[CodeGenFlag.ShouldCallGetAssemblyType] = false;
}
Expand Down Expand Up @@ -1576,7 +1576,7 @@ private void WriteName(SyntaxNode node, SyntaxToken identifier)
|| (pluginClassesNamespace != null && IsDescendantOfNamespaceSymbol(symbol, pluginClassesNamespace))
) : false;

List<SyntaxKind> fullyQualifiedParentKinds = [SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.ObjectCreationExpression];
HashSet<SyntaxKind> fullyQualifiedParentKinds = [SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.ObjectCreationExpression];
if (
symbol != null
&& symbol is ITypeSymbol typeSymbol
Expand All @@ -1594,24 +1594,23 @@ private void WriteName(SyntaxNode node, SyntaxToken identifier)
var parentAccessExpression = FindFirstAncestor<MemberAccessExpressionSyntax>(node);
var isLeftSide = parentAccessExpression == null ? true : node == parentAccessExpression.Expression;
var parentBlocks = GetAncestors<SyntaxNode>(node);
var localScopeIncludesIdentifier = parentBlocks
.Any(block =>
{
var descendants = block.DescendantNodes();
var localFunctions = descendants.OfType<LocalFunctionStatementSyntax>();
var variableDesignations = descendants.OfType<VariableDesignationSyntax>();
var variableDeclarators = descendants.OfType<VariableDeclaratorSyntax>();
var forEachStatements = descendants.OfType<ForEachStatementSyntax>();
var forStatements = descendants.OfType<ForStatementSyntax>();
var parameters = descendants.OfType<ParameterSyntax>();
var checkNamePredicate = (SyntaxNode node) => TryGetName(node) == identifierText;
return localFunctions.Any(checkNamePredicate)
|| variableDesignations.Any(checkNamePredicate)
|| variableDeclarators.Any(checkNamePredicate)
|| parameters.Any(checkNamePredicate)
|| forEachStatements.Any(checkNamePredicate)
|| forStatements.Any(forStatement => forStatement.Initializers.Count() > 0);
});
var localScopeIncludesIdentifier = parentBlocks.Any(block =>
{
var descendants = block.DescendantNodes();
var localFunctions = descendants.OfType<LocalFunctionStatementSyntax>();
var variableDesignations = descendants.OfType<VariableDesignationSyntax>();
var variableDeclarators = descendants.OfType<VariableDeclaratorSyntax>();
var forEachStatements = descendants.OfType<ForEachStatementSyntax>();
var forStatements = descendants.OfType<ForStatementSyntax>();
var parameters = descendants.OfType<ParameterSyntax>();
var checkNamePredicate = (SyntaxNode node) => TryGetName(node) == identifierText;
return localFunctions.Any(checkNamePredicate)
|| variableDesignations.Any(checkNamePredicate)
|| variableDeclarators.Any(checkNamePredicate)
|| parameters.Any(checkNamePredicate)
|| forEachStatements.Any(checkNamePredicate)
|| forStatements.Any(forStatement => forStatement.Initializers.Count() > 0);
});

if (isLeftSide && !localScopeIncludesIdentifier && !runtimeNamespaceIncludesIdentifier)
{
Expand Down
7 changes: 5 additions & 2 deletions RobloxCS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ This project includes the compiler and transformers.
- Interfaces to Luau `export type`
- Anonymous types (e.g. `var x = new { };`)
- Type hoisting when outside of namespace
- Macro `Equals` function to `__eq`
- Macro `GetType` function to the name of the type as a string
- Async/await
- Overloaded methods

## Will maybe be supported
- [Class finalizers (destructors)](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers)
Expand All @@ -25,5 +29,4 @@ This project includes the compiler and transformers.

## Will not be supported
- `using name = value` expressions (equivalent to `:=` operator in other languages)
- `volatile`, `fixed`, and `unsafe` keywords
- Pointers
- Any unsafe context (pointers, `volatile`, `fixed`, etc.)
2 changes: 1 addition & 1 deletion RobloxCS/RobloxCS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.10.0" />
<PackageReference Include="YamlDotNet" Version="15.3.0" />
<PackageReference Include="RobloxCS.Types" Version="1.0.6" />
<PackageReference Include="RobloxCS.Types" Version="1.0.10" />
</ItemGroup>

</Project>
15 changes: 6 additions & 9 deletions RobloxCS/Transformers/MainTransformer.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.IO;
using System.Linq.Expressions;
using System.Text.RegularExpressions;

namespace RobloxCS
{
Expand Down Expand Up @@ -47,12 +44,6 @@ public MainTransformer(SyntaxTree tree, ConfigData config)
return base.VisitIdentifierName(node.WithIdentifier(newToken));
}

private static SyntaxToken CreateIdentifierToken(string text, string? valueText = null, SyntaxTriviaList? trivia = null)
{
var triviaList = trivia ??= SyntaxFactory.TriviaList();
return SyntaxFactory.VerbatimIdentifier(triviaList, text, valueText ?? text, triviaList);
}

public override SyntaxNode? VisitArgument(ArgumentSyntax node)
{
if (node.Expression.IsKind(SyntaxKind.IdentifierName))
Expand All @@ -73,6 +64,12 @@ private static SyntaxToken CreateIdentifierToken(string text, string? valueText
return base.VisitConditionalAccessExpression(node);
}

private static SyntaxToken CreateIdentifierToken(string text, string? valueText = null, SyntaxTriviaList? trivia = null)
{
var triviaList = trivia ??= SyntaxFactory.TriviaList();
return SyntaxFactory.VerbatimIdentifier(triviaList, text, valueText ?? text, triviaList);
}

private ExpressionSyntax? ProcessWhenNotNull(ExpressionSyntax expression, ExpressionSyntax whenNotNull)
{
if (whenNotNull == null)
Expand Down
17 changes: 9 additions & 8 deletions RobloxCS/TranspilerUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,18 @@ private static List<PortableExecutableReference> GetCompilationReferences()
return references;
}

private static List<PortableExecutableReference> GetCoreLibReferences()
private static HashSet<PortableExecutableReference> GetCoreLibReferences()
{
var coreLib = typeof(object).GetTypeInfo().Assembly.Location;
var systemRuntime = Path.Combine(Path.GetDirectoryName(coreLib)!, "System.Runtime.dll");
var systemConsole = Path.Combine(Path.GetDirectoryName(coreLib)!, "System.Console.dll");
return new List<PortableExecutableReference>
HashSet<string> coreDlls = ["System.Runtime.dll", "System.Core.dll", "System.Console.dll", "System.Collections.dll"];
HashSet<PortableExecutableReference> references = [MetadataReference.CreateFromFile(coreLib)];

foreach (var coreDll in coreDlls)
{
MetadataReference.CreateFromFile(coreLib),
MetadataReference.CreateFromFile(systemRuntime),
MetadataReference.CreateFromFile(systemConsole)
};
var dllPath = Path.Combine(Path.GetDirectoryName(coreLib)!, coreDll);
references.Add(MetadataReference.CreateFromFile(dllPath));
}
return references;
}
}
}
7 changes: 6 additions & 1 deletion RobloxCS/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public static string FormatLocation(FileLinePositionSpan lineSpan)

public static ISymbol? FindMember(INamespaceSymbol namespaceSymbol, string memberName)
{
return namespaceSymbol.GetMembers().FirstOrDefault(member => member.Name == memberName);
var member = namespaceSymbol.GetMembers().FirstOrDefault<ISymbol?>(member => member?.Name == memberName, null);
if (member == null && namespaceSymbol.ContainingNamespace != null)
{
member = FindMember(namespaceSymbol.ContainingNamespace, memberName);
}
return member;
}

public static ISymbol? FindMemberDeep(INamedTypeSymbol namedTypeSymbol, string memberName)
Expand Down
14 changes: 0 additions & 14 deletions TestProject/TestProject.csproj

This file was deleted.

7 changes: 0 additions & 7 deletions TestProject/aftman.toml

This file was deleted.

48 changes: 0 additions & 48 deletions TestProject/default.project.json

This file was deleted.

40 changes: 0 additions & 40 deletions TestProject/dist/Client/Components/Lava.lua

This file was deleted.

21 changes: 0 additions & 21 deletions TestProject/dist/Client/Main.client.lua

This file was deleted.

76 changes: 0 additions & 76 deletions TestProject/dist/Shared/Components.lua

This file was deleted.

Loading

0 comments on commit 1829542

Please sign in to comment.