-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from CrosRoad95/feature/luau
Add luau transpiler
- Loading branch information
Showing
12 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Text; | ||
using Loretta.CodeAnalysis; | ||
using Loretta.CodeAnalysis.Lua; | ||
using Loretta.CodeAnalysis.Lua.Syntax; | ||
using Loretta.CodeAnalysis.Text; | ||
|
||
namespace SlipeServer.Scripting.Luau; | ||
|
||
internal sealed class LuauToLuaTransform : IScriptTransform | ||
{ | ||
public Stream Transform(Stream data, string lang) | ||
{ | ||
if (lang != "lua") | ||
return data; | ||
|
||
SourceText sourceText = SourceText.From(data, Encoding.UTF8); | ||
|
||
var syntaxTree = LuaSyntaxTree.ParseText(sourceText, options: new LuaParseOptions(LuaSyntaxOptions.Luau), path: "script.lua"); | ||
|
||
var root = syntaxTree.GetRoot(); | ||
|
||
var rewriter = new TypeAnnotationRemover(); | ||
var strippedRoot = rewriter.Visit(root); | ||
|
||
var outData = new MemoryStream(); | ||
var writer = new StreamWriter(outData); | ||
strippedRoot.WriteTo(writer); | ||
writer.Flush(); | ||
return outData; | ||
} | ||
|
||
class TypeAnnotationRemover : LuaSyntaxRewriter | ||
{ | ||
public override SyntaxNode? Visit(SyntaxNode? node) | ||
{ | ||
if (node is TypeDeclarationStatementSyntax or TypeBindingSyntax) | ||
return null; | ||
|
||
return base.Visit(node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using SlipeServer.Server.ServerBuilders; | ||
|
||
namespace SlipeServer.Scripting.Luau; | ||
|
||
public static class ServerBuilderExtensions | ||
{ | ||
public static ServerBuilder AddLuauTranspiler(this ServerBuilder builder) | ||
{ | ||
builder.ConfigureServices(services => | ||
{ | ||
services.AddLuauTranspiler(); | ||
}); | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace SlipeServer.Scripting.Luau; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddLuauTranspiler(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<IScriptTransform, LuauToLuaTransform>(); | ||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SlipeServer.Scripting\SlipeServer.Scripting.csproj" /> | ||
<ProjectReference Include="..\SlipeServer.Server\SlipeServer.Server.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" /> | ||
<PackageReference Include="Loretta.CodeAnalysis.Lua" Version="0.2.13-nightly.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace SlipeServer.Scripting; | ||
|
||
public interface IScriptTransform | ||
{ | ||
Stream Transform(Stream data, string lang); | ||
} | ||
|
||
public sealed class ScriptTransformationPipeline | ||
{ | ||
private readonly List<IScriptTransform> scriptTransforms; | ||
|
||
public ScriptTransformationPipeline(IEnumerable<IScriptTransform> scriptTransforms) | ||
{ | ||
this.scriptTransforms = scriptTransforms.ToList(); | ||
} | ||
|
||
public void Add(IScriptTransform scriptTransform) | ||
{ | ||
this.scriptTransforms.Add(scriptTransform); | ||
} | ||
|
||
public Stream Transform(Stream data, string lang) | ||
{ | ||
var transformedData = data; | ||
|
||
foreach (var transformation in this.scriptTransforms) | ||
{ | ||
transformedData = transformation.Transform(transformedData, lang); | ||
transformedData.Position = 0; | ||
} | ||
|
||
return transformedData; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace SlipeServer.Scripting; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static void AddScripting(this IServiceCollection services) | ||
{ | ||
services.TryAddSingleton<ScriptTransformationPipeline>(); | ||
services.AddSingleton<IScriptEventRuntime, ScriptEventRuntime>(); | ||
services.AddSingleton<IScriptInputRuntime, ScriptInputRuntime>(); | ||
} | ||
|
||
public static void AddScripting<T>(this IServiceCollection services) where T : class, IScriptEventRuntime | ||
{ | ||
services.TryAddSingleton<ScriptTransformationPipeline>(); | ||
services.AddSingleton<IScriptEventRuntime, T>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters