mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Start of generate vectors for C#
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace ScriptCoreGenerator
|
||||
{
|
||||
[Generator]
|
||||
public class HelloSourceGenerator : ISourceGenerator
|
||||
{
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
var receiver = (MainSyntaxReceiver)context.SyntaxReceiver;
|
||||
|
||||
|
||||
|
||||
string output = @"
|
||||
namespace Test{
|
||||
public class Test
|
||||
{
|
||||
public static void P() => GlitchyEngine.Log.Error(""Hello World"");
|
||||
}}
|
||||
";
|
||||
|
||||
// Code generation goes here
|
||||
context.AddSource("Test/Hello.g.cs", output);
|
||||
}
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterForSyntaxNotifications(() => new MainSyntaxReceiver());
|
||||
}
|
||||
}
|
||||
|
||||
public class MainSyntaxReceiver : ISyntaxReceiver
|
||||
{
|
||||
public DefinitionAggregate Definitions { get; } = new();
|
||||
public GivethsAggregate Giveths { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
Definitions.OnVisitSyntaxNode(syntaxNode);
|
||||
Giveths.OnVisitSyntaxNode(syntaxNode);
|
||||
}
|
||||
}
|
||||
|
||||
public class DefinitionAggregate : ISyntaxReceiver
|
||||
{
|
||||
public List<Capture> Captures { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
if (syntaxNode is not AttributeSyntax { Name: IdentifierNameSyntax { Identifier.Text: "Define" } } attr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var method = attr.GetParent<MethodDeclarationSyntax>();
|
||||
var key = method.Identifier.Text;
|
||||
|
||||
Captures.Add(new Capture(key, method));
|
||||
}
|
||||
|
||||
public record Capture(string Key, MethodDeclarationSyntax Method)
|
||||
{
|
||||
public string Key { get; } = Key;
|
||||
public MethodDeclarationSyntax Method { get; } = Method;
|
||||
}
|
||||
}
|
||||
|
||||
public class GivethsAggregate : ISyntaxReceiver
|
||||
{
|
||||
public List<Capture> Captures { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
if (syntaxNode is not AttributeSyntax { Name: IdentifierNameSyntax { Identifier.Text: "Give" } } attr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var target = (attr.ArgumentList.Arguments.Single().Expression as LiteralExpressionSyntax).Token.ValueText;
|
||||
|
||||
var method = attr.GetParent<MethodDeclarationSyntax>();
|
||||
var @class = attr.GetParent<ClassDeclarationSyntax>();
|
||||
|
||||
Captures.Add(new Capture(target, method, @class));
|
||||
}
|
||||
|
||||
public record Capture(string TargetImplementation, MethodDeclarationSyntax Method, ClassDeclarationSyntax Class)
|
||||
{
|
||||
public string TargetImplementation { get; } = TargetImplementation;
|
||||
public MethodDeclarationSyntax Method { get; } = Method;
|
||||
public ClassDeclarationSyntax Class { get; } = Class;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Debug Code Generator": {
|
||||
"commandName": "DebugRoslynComponent",
|
||||
"targetProject": "..\\ScriptCore\\ScriptCore.csproj"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<IsRoslynComponent>true</IsRoslynComponent>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace ScriptCoreGenerator;
|
||||
|
||||
public static class SyntaxNodeExtensions
|
||||
{
|
||||
public static T GetParent<T>(this SyntaxNode node)
|
||||
{
|
||||
var parent = node.Parent;
|
||||
while (true)
|
||||
{
|
||||
if (parent == null)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
if (parent is T t)
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
parent = parent.Parent;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// determine the namespace the class/enum/struct is declared in, if any
|
||||
/// </summary>
|
||||
public static string GetNamespace(this BaseTypeDeclarationSyntax syntax)
|
||||
{
|
||||
// If we don't have a namespace at all we'll return an empty string
|
||||
// This accounts for the "default namespace" case
|
||||
string nameSpace = string.Empty;
|
||||
|
||||
// Get the containing syntax node for the type declaration
|
||||
// (could be a nested type, for example)
|
||||
SyntaxNode? potentialNamespaceParent = syntax.Parent;
|
||||
|
||||
// Keep moving "out" of nested classes etc until we get to a namespace
|
||||
// or until we run out of parents
|
||||
while (potentialNamespaceParent != null &&
|
||||
potentialNamespaceParent is not NamespaceDeclarationSyntax
|
||||
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
|
||||
{
|
||||
potentialNamespaceParent = potentialNamespaceParent.Parent;
|
||||
}
|
||||
|
||||
// Build up the final namespace by looping until we no longer have a namespace declaration
|
||||
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
|
||||
{
|
||||
// We have a namespace. Use that as the type
|
||||
nameSpace = namespaceParent.Name.ToString();
|
||||
|
||||
// Keep moving "out" of the namespace declarations until we
|
||||
// run out of nested namespace declarations
|
||||
while (true)
|
||||
{
|
||||
if (namespaceParent.Parent is not NamespaceDeclarationSyntax parent)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Add the outer namespace as a prefix to the final namespace
|
||||
nameSpace = $"{namespaceParent.Name}.{nameSpace}";
|
||||
namespaceParent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
// return the final namespace
|
||||
return nameSpace;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using static ScriptCoreGenerator.VectorSyntaxReceiver;
|
||||
|
||||
namespace ScriptCoreGenerator;
|
||||
|
||||
[Generator]
|
||||
public class VectorGenerator : ISourceGenerator
|
||||
{
|
||||
public static readonly string[] ComponentNames = {"X", "Y", "Z", "W"};
|
||||
|
||||
public static readonly string[] LowerComponentNames = {"x", "y", "z", "w"};
|
||||
|
||||
public void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
var receiver = (VectorSyntaxReceiver)context.SyntaxReceiver;
|
||||
|
||||
if (receiver == null) return;
|
||||
|
||||
StringBuilder builder = new();
|
||||
|
||||
foreach (var vector in receiver.Vectors)
|
||||
{
|
||||
builder.Clear();
|
||||
|
||||
// Copy all usings from the original file
|
||||
builder.AppendLine(vector.Struct.GetParent<CompilationUnitSyntax>().Usings.ToFullString());
|
||||
|
||||
builder.Append($$"""
|
||||
namespace {{vector.Struct.GetNamespace()}};
|
||||
|
||||
public partial struct {{vector.VectorName}}
|
||||
{
|
||||
|
||||
""");
|
||||
|
||||
GenerateFields(vector, builder);
|
||||
GenerateConstructors(vector, builder);
|
||||
|
||||
//var swizzle = receiver.VectorSwizzle.Swizzles.FirstOrDefault(s => s.VectorName == vector.VectorName);
|
||||
|
||||
//if (swizzle != null)
|
||||
GenerateSwizzle(vector, builder);
|
||||
|
||||
builder.Append('}');
|
||||
|
||||
context.AddSource($"{vector.VectorName}.g.cs", builder.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateFields(VectorDefinition vector, StringBuilder builder)
|
||||
{
|
||||
builder.Append($"\tpublic {vector.ElementTypeName} ");
|
||||
|
||||
for (int i = 0; i < vector.ComponentCount; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
builder.Append(", ");
|
||||
|
||||
builder.Append(ComponentNames[i]);
|
||||
}
|
||||
|
||||
builder.Append(";\n\n");
|
||||
}
|
||||
|
||||
private void GenerateConstructors(VectorDefinition vector, StringBuilder builder)
|
||||
{
|
||||
GenerateSingleConstructor(vector, builder);
|
||||
|
||||
if (vector.ComponentCount == 3)
|
||||
{
|
||||
Generatefloat3Constructors(vector, builder);
|
||||
}
|
||||
else if (vector.ComponentCount == 4)
|
||||
{
|
||||
Generatefloat4Constructors(vector, builder);
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateSingleConstructor(VectorDefinition vector, StringBuilder builder)
|
||||
{
|
||||
StringBuilder parameters = new();
|
||||
StringBuilder body = new();
|
||||
|
||||
for (int i = 0; i < vector.ComponentCount; i++)
|
||||
{
|
||||
if (i != 0)
|
||||
parameters.Append(", ");
|
||||
|
||||
parameters.Append($"{vector.ElementTypeName} {LowerComponentNames[i]}");
|
||||
}
|
||||
|
||||
for (int i = 0; i < vector.ComponentCount; i++)
|
||||
{
|
||||
body.Append($"\n\t\t{ComponentNames[i]} = {LowerComponentNames[i]};");
|
||||
}
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}} ({{parameters}})
|
||||
{{{body}}
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
private void Generatefloat3Constructors(VectorDefinition vector, StringBuilder builder)
|
||||
{
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.BaseName}}2 xy, {{vector.ElementTypeName}} z)
|
||||
{
|
||||
X = xy.X;
|
||||
Y = xy.Y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
""");
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.ElementTypeName}} x, {{vector.BaseName}}2 yz)
|
||||
{
|
||||
X = x;
|
||||
Y = yz.X;
|
||||
Z = yz.Y;
|
||||
}
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
private void Generatefloat4Constructors(VectorDefinition vector, StringBuilder builder)
|
||||
{
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.BaseName}}2 xy, {{vector.BaseName}}2 zw)
|
||||
{
|
||||
//XY = xy;
|
||||
//ZW = zw;
|
||||
X = xy.X;
|
||||
Y = xy.Y;
|
||||
Z = zw.X;
|
||||
W = zw.Y;
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.BaseName}}2 xy, {{vector.ElementTypeName}} z, {{vector.ElementTypeName}} w)
|
||||
{
|
||||
X = xy.X;
|
||||
Y = xy.Y;
|
||||
Z = z;
|
||||
W = w;
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.ElementTypeName}} x, {{vector.BaseName}}2 yz, {{vector.ElementTypeName}} w)
|
||||
{
|
||||
X = x;
|
||||
Y = yz.X;
|
||||
Z = yz.Y;
|
||||
W = w;
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.ElementTypeName}} x, {{vector.ElementTypeName}} y, {{vector.BaseName}}2 zw)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = zw.X;
|
||||
W = zw.Y;
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.BaseName}}3 xyz, {{vector.ElementTypeName}} w)
|
||||
{
|
||||
X = xyz.X;
|
||||
Y = xyz.Y;
|
||||
Z = xyz.Z;
|
||||
W = w;
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.VectorName}}({{vector.ElementTypeName}} x, {{vector.BaseName}}3 yzw)
|
||||
{
|
||||
X = x;
|
||||
Y = yzw.X;
|
||||
Z = yzw.Y;
|
||||
W = yzw.Z;
|
||||
}
|
||||
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the swizzle operator is invalid (same component assigned twice).
|
||||
*/
|
||||
static bool IsSwizzleSetterValid(int[] cmp, int vecSize)
|
||||
{
|
||||
return cmp[0] == cmp[1] || (vecSize >= 3 && cmp[0] == cmp[2]) || (vecSize == 4 && cmp[0] == cmp[3]) ||
|
||||
(vecSize >= 3 && cmp[1] == cmp[2]) || (vecSize == 4 && cmp[1] == cmp[3]) ||
|
||||
(vecSize == 4 && cmp[2] == cmp[3]);
|
||||
}
|
||||
|
||||
private void GenerateSwizzle(VectorDefinition vector, StringBuilder builder)
|
||||
{
|
||||
for(int swizzleCount = 2; swizzleCount <= 4; swizzleCount++)
|
||||
{
|
||||
int[] cmp = new int[4];
|
||||
|
||||
int cmp2max = swizzleCount > 2 ? vector.ComponentCount : 1;
|
||||
int cmp3max = swizzleCount > 3 ? vector.ComponentCount : 1;
|
||||
|
||||
for(cmp[0] = 0; cmp[0] < vector.ComponentCount; cmp[0]++)
|
||||
for(cmp[1] = 0; cmp[1] < vector.ComponentCount; cmp[1]++)
|
||||
for(cmp[2] = 0; cmp[2] < cmp2max; cmp[2]++)
|
||||
for(cmp[3] = 0; cmp[3] < cmp3max; cmp[3]++)
|
||||
{
|
||||
StringBuilder swizzleName = new StringBuilder(4);
|
||||
StringBuilder swizzleConstructor = new StringBuilder(swizzleCount * 3);
|
||||
StringBuilder setter = new StringBuilder();
|
||||
|
||||
bool setterInvalid = IsSwizzleSetterValid(cmp, swizzleCount);
|
||||
|
||||
for(int c = 0; c < swizzleCount; c++)
|
||||
{
|
||||
swizzleName.Append(ComponentNames[cmp[c]]);
|
||||
|
||||
if(c != 0)
|
||||
{
|
||||
swizzleConstructor.Append(", ");
|
||||
}
|
||||
swizzleConstructor.Append(ComponentNames[cmp[c]]);
|
||||
|
||||
if(!setterInvalid && c < vector.ComponentCount)
|
||||
{
|
||||
setter.Append($"\n\t\t\t{ComponentNames[cmp[c]]} = value.{ComponentNames[c]};");
|
||||
}
|
||||
}
|
||||
// if (!setterInvalid)
|
||||
// {
|
||||
// setter.Append(
|
||||
// """
|
||||
// set mut
|
||||
// {
|
||||
//""");
|
||||
// }
|
||||
|
||||
builder.Append($$"""
|
||||
public {{vector.BaseName}}{{swizzleCount}} {{swizzleName}}
|
||||
{
|
||||
get => new({{swizzleConstructor}});
|
||||
|
||||
""");
|
||||
|
||||
if (!setterInvalid)
|
||||
{
|
||||
builder.Append($$"""
|
||||
set
|
||||
{{{setter}}
|
||||
}
|
||||
|
||||
""");
|
||||
}
|
||||
|
||||
builder.Append("\t}\n\n");
|
||||
|
||||
// string swizzleString = $$"""
|
||||
//public {{swizzle.BaseName}}{{swizzleCount}} {{swizzleName}}
|
||||
//{
|
||||
//get => {{swizzleConstructor}};
|
||||
//{{setter}}
|
||||
//}
|
||||
|
||||
//""";
|
||||
|
||||
//builder.Append(swizzleString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Initialize(GeneratorInitializationContext context)
|
||||
{
|
||||
context.RegisterForSyntaxNotifications(() => new VectorSyntaxReceiver());
|
||||
}
|
||||
}
|
||||
|
||||
public class VectorSyntaxReceiver : ISyntaxReceiver
|
||||
{
|
||||
public List<VectorDefinition> Vectors { get; } = new();
|
||||
|
||||
public VectorSwizzleReceiver VectorSwizzle { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
VectorSwizzle.OnVisitSyntaxNode(syntaxNode);
|
||||
|
||||
if (syntaxNode is not AttributeSyntax { Name: IdentifierNameSyntax { Identifier.Text: "Vector" } } attr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var @struct = attr.GetParent<StructDeclarationSyntax>();
|
||||
var name = @struct.Identifier.Text;
|
||||
|
||||
// ToFullString should like probably always work
|
||||
var elementTypeName = (attr.ArgumentList.Arguments[0].Expression as TypeOfExpressionSyntax).Type.ToFullString();
|
||||
|
||||
var componentCount = (attr.ArgumentList.Arguments[1].Expression as LiteralExpressionSyntax).Token.Value as int?;
|
||||
|
||||
if (componentCount == null)
|
||||
return;
|
||||
|
||||
var baseName = (attr.ArgumentList.Arguments[2].Expression as LiteralExpressionSyntax).Token.ValueText;
|
||||
|
||||
Vectors.Add(new VectorDefinition(name, @struct, componentCount.Value, baseName, elementTypeName));
|
||||
}
|
||||
|
||||
public class VectorDefinition
|
||||
{
|
||||
public string VectorName { get; }
|
||||
public StructDeclarationSyntax Struct { get; }
|
||||
|
||||
public int ComponentCount { get; }
|
||||
|
||||
public string BaseName { get; }
|
||||
|
||||
public string ElementTypeName { get; }
|
||||
|
||||
public VectorDefinition(string vectorName, StructDeclarationSyntax @struct, int componentCount, string baseName, string elementTypeName)
|
||||
{
|
||||
VectorName = vectorName;
|
||||
Struct = @struct;
|
||||
ComponentCount = componentCount;
|
||||
BaseName = baseName;
|
||||
ElementTypeName = elementTypeName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class VectorSwizzleReceiver : ISyntaxReceiver
|
||||
{
|
||||
public List<VectorSwizzle> Swizzles { get; } = new();
|
||||
|
||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
||||
{
|
||||
if (syntaxNode is not AttributeSyntax { Name: IdentifierNameSyntax { Identifier.Text: "SwizzleVector" } } attr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var @struct = attr.GetParent<StructDeclarationSyntax>();
|
||||
var name = @struct.Identifier.Text;
|
||||
|
||||
// var baseName = (attr.ArgumentList.Arguments[0].Expression as LiteralExpressionSyntax).Token.ValueText;
|
||||
|
||||
Swizzles.Add(new VectorSwizzle(name));//, baseName));
|
||||
}
|
||||
|
||||
public class VectorSwizzle
|
||||
{
|
||||
public string VectorName { get; }
|
||||
|
||||
//public string BaseName { get; }
|
||||
|
||||
public VectorSwizzle(string vectorName)//, string baseName)
|
||||
{
|
||||
VectorName = vectorName;
|
||||
//BaseName = baseName;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user