Skip to content

Commit 88f8402

Browse files
committed
Added current state of code generator from: otac0n/GenerateMatrixMath@3a8e59d
1 parent 8627d6a commit 88f8402

16 files changed

Lines changed: 1927 additions & 0 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project Sdk="Microsoft.NET.Sdk">
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9+
<LangVersion>preview</LangVersion>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
13+
<PackageReference Include="Weave" Version="2.1.0">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
</ItemGroup>
18+
<ItemGroup>
19+
<WeaveTemplate Include="Templates\Matrix.weave" />
20+
<WeaveTemplate Include="Templates\MatrixExt.weave" />
21+
<WeaveTemplate Include="Templates\Name.weave" />
22+
<WeaveTemplate Include="Templates\Vector.weave" />
23+
<WeaveTemplate Include="Templates\VectorExt.weave" />
24+
<None Remove="Matrix.weave" />
25+
</ItemGroup>
26+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace GenerateMatrixMath.Model
2+
{
3+
public enum ArgumentMultiplicity
4+
{
5+
One,
6+
Memberwise,
7+
}
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace GenerateMatrixMath.Model
2+
{
3+
public record class Dimension(int Rows, int Columns) : IComparable<Dimension>
4+
{
5+
public int CompareTo(Dimension? other)
6+
{
7+
if (other is null)
8+
{
9+
return 1;
10+
}
11+
12+
int comp;
13+
var aMax = Math.Max(this.Rows, this.Columns);
14+
var bMax = Math.Max(other.Rows, other.Columns);
15+
16+
comp = aMax.CompareTo(bMax);
17+
if (comp != 0)
18+
{
19+
return comp;
20+
}
21+
22+
var aMin = Math.Min(this.Rows, this.Columns);
23+
var bMin = Math.Min(other.Rows, other.Columns);
24+
25+
comp = aMin.CompareTo(bMin);
26+
return comp;
27+
}
28+
29+
public static implicit operator Dimension((int rows, int columns) value) => new(value.rows, value.columns);
30+
31+
public override string ToString() => $"{this.Rows}x{this.Columns}";
32+
}
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GenerateMatrixMath.Model
2+
{
3+
public record class Extension(Type Type, string Name, ArgumentMultiplicity[] Arguments)
4+
{
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GenerateMatrixMath.Model
2+
{
3+
public record class ExtensionProperty(Type Type, string Name)
4+
{
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GenerateMatrixMath.Model
2+
{
3+
public record class Matrix(Dimension Size, HashSet<Dimension> AllSizes, Type[] Casts, Type[] NumericsTypes, IEnumerable<(Dimension Left, Dimension Right)> MultiplyFunctions, IEnumerable<(Dimension Left, Dimension Right)> MultiplyOperators)
4+
{
5+
}
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace GenerateMatrixMath.Model
2+
{
3+
using System.Collections.Immutable;
4+
5+
public record class Vector(int Size, IEnumerable<int> Sizes, HashSet<Dimension> AllSizes, Type[] Casts, Type[] NumericsTypes, Type[] Intrinsics, IEnumerable<Extension> Extensions, IEnumerable<ExtensionProperty> ExtensionProperties)
6+
{
7+
public static readonly ImmutableList<string> VectorFieldNames = ["X", "Y", "Z", "W"];
8+
}
9+
}

sources/GenerateGenericMath/GenerateMatrixMath/Program.cs

Lines changed: 261 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.Reflection;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
6+
namespace GenerateMatrixMath
7+
{
8+
internal static class RoslynExtensions
9+
{
10+
public static string ToCSharpString(this Type type, string[] usingNamespaces = null, Assembly[] usingAssemblies = null, SymbolDisplayFormat symbolDisplayFormat = null)
11+
{
12+
var compilationUnit = SyntaxFactory.CompilationUnit();
13+
if (usingNamespaces != null)
14+
{
15+
compilationUnit = compilationUnit.AddUsings(
16+
Array.ConvertAll(usingNamespaces, n => SyntaxFactory.UsingDirective(SyntaxFactory.ParseName(n))));
17+
}
18+
else
19+
{
20+
compilationUnit = compilationUnit.AddUsings(
21+
SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System")));
22+
}
23+
24+
MetadataReference[] metadataReferences;
25+
if (usingAssemblies != null)
26+
{
27+
metadataReferences = Array.ConvertAll(usingAssemblies, u => MetadataReference.CreateFromFile(u.Location));
28+
}
29+
else
30+
{
31+
metadataReferences = new[]
32+
{
33+
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
34+
MetadataReference.CreateFromFile(type.Assembly.Location)
35+
};
36+
}
37+
38+
var typeName = SyntaxFactory.ParseTypeName(type.ToFullyQualifiedName());
39+
40+
var field = SyntaxFactory.FieldDeclaration(
41+
SyntaxFactory.VariableDeclaration(typeName).WithVariables(
42+
SyntaxFactory.SingletonSeparatedList(
43+
SyntaxFactory.VariableDeclarator(
44+
SyntaxFactory.Identifier("field")))));
45+
compilationUnit = compilationUnit.AddMembers(
46+
SyntaxFactory.ClassDeclaration("MyClass").AddMembers(
47+
field))
48+
.NormalizeWhitespace();
49+
50+
var tree = compilationUnit.SyntaxTree;
51+
var compilation = CSharpCompilation.Create("MyAssembly", [tree], metadataReferences);
52+
var semanticModel = compilation.GetSemanticModel(tree);
53+
var root = tree.GetRoot();
54+
55+
var typeSymbol = semanticModel.GetTypeInfo(compilationUnit
56+
.DescendantNodes().OfType<ClassDeclarationSyntax>().Single()
57+
.Members.OfType<FieldDeclarationSyntax>().Single()
58+
.Declaration.Type);
59+
60+
return typeSymbol.Type.ToDisplayString(symbolDisplayFormat ?? new SymbolDisplayFormat(
61+
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypes,
62+
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
63+
miscellaneousOptions: SymbolDisplayMiscellaneousOptions.UseSpecialTypes));
64+
}
65+
66+
public static string ToFullyQualifiedName(this Type type)
67+
{
68+
switch (type)
69+
{
70+
case { IsGenericParameter: true }: return type.Name;
71+
case { IsArray: true }: return type.GetElementType().ToFullyQualifiedName() + "[]";
72+
case { IsPointer: true }: return type.GetElementType().ToFullyQualifiedName() + "*";
73+
case { IsByRef: true }: return type.GetElementType().ToFullyQualifiedName() + "&";
74+
case { IsGenericType: false }: return string.IsNullOrEmpty(type.FullName) ? type.Name : type.FullName.Replace('+', '.');
75+
default:
76+
var fullName = type.GetGenericTypeDefinition().FullName;
77+
var backTickIndex = fullName.IndexOf('`');
78+
if (backTickIndex > 0)
79+
{
80+
fullName = fullName.Substring(0, backTickIndex);
81+
}
82+
83+
return fullName.Replace('+', '.') + "<" + string.Join(", ", type.GetGenericArguments().Select(ToFullyQualifiedName)) + ">";
84+
}
85+
;
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)