Code Monkey home page Code Monkey logo

arrizer's Introduction

Arrizer
(based on .NET Roslyn SDK)

This is just a little experiment in using Roslyn for code generation. I was looking for something that could easily turn structures into structure of arrays (SoA). Couldn't find it.

If you're interested in writing something similar I recommend skimming through Interesting parts, reading Roslyn SDK Guide and most importantly checking out the Roslyn SDK Code Generation repository.

Interesting parts

Excerpt from Arrizer.Generators/Generators.cs which defines the specific code generation.

...
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
{
    var ogStruct = (StructDeclarationSyntax)context.ProcessingNode;
    var ogMembers = ogStruct.Members;

    var newStruct = SyntaxFactory.StructDeclaration(ogStruct.Identifier.ValueText + suffix)
        .WithAttributeLists(ogStruct.AttributeLists)
        .WithTriviaFrom(ogStruct);
    List<MemberDeclarationSyntax> newMembers = new List<MemberDeclarationSyntax>();

    foreach(var ogMember in ogMembers)
    {
        if(ogMember.RawKind == (int)SyntaxKind.FieldDeclaration)
        {
            FieldDeclarationSyntax ogFieldSyntax = (FieldDeclarationSyntax)ogMember;
            VariableDeclarationSyntax ogVariableSyntax = ogFieldSyntax.Declaration;
            TypeSyntax ogType = ogVariableSyntax.Type;
            VariableDeclarationSyntax newVariableSyntax = ogVariableSyntax.WithType(ArrayType(ogType, SingletonList(ArrayRankSpecifier())));
            FieldDeclarationSyntax newFieldSyntax = ogFieldSyntax.ReplaceNode(ogVariableSyntax, newVariableSyntax);
            newMembers.Add(newFieldSyntax);
        }
        else
        {
            newMembers.Add(ogMember);
        }
    }

    newStruct = newStruct.AddMembers(newMembers.ToArray());

    var result = SyntaxFactory.SingletonList<MemberDeclarationSyntax>(newStruct);

    return Task.FromResult(result);
}
...

Excerpt from Arrizer.Attributes/Attributes.cs which contains attribute that you can slap on your structs.

...
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = true)]
[CodeGenerationAttribute("Arrizer.Generators.SOAGenerator, Arrizer.Generators")]
[Conditional("CodeGeneration")]
public class SOAAttribute : Attribute
{
    public SOAAttribute(string suffix = "SOA")
    {
        Suffix = suffix;
    }

    public string Suffix { get; }
}
...

Example usage

using System;
using System.Diagnostics;
using System.Reflection;
using CodeGeneration.Roslyn;
using Arrizer.Attributes;

namespace Arrizer
{

    [SOA] // default suffix for new type - "SOA"
    [SOA("DifferentSuffix")]
    struct Particle
    {
        public float PositionX;
        public float PositionY;
        public float Rotation;

        public int Status;
    }

    class Program
    {

        static void Main(string[] args)
        {
            foreach (var type in typeof(Program).Assembly.GetTypes())
            {
                Console.WriteLine(type.FullName);
                var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                foreach(var field in fields)
                {
                    Console.WriteLine("  - " + (field.IsPublic?"public":"private") + " " + field.FieldType.Name + " " + field.Name);
                }
            }

            Particle particle = new Particle();
            ParticleSOA particleSOA = new ParticleSOA();
            ParticleDifferentSuffix particleDifferrentSuffix = new ParticleDifferentSuffix();
        }
    }
}

arrizer's People

Contributors

aksyr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.