Ghost4Man.VisitorPatternGenerator 0.1.2

dotnet add package Ghost4Man.VisitorPatternGenerator --version 0.1.2
NuGet\Install-Package Ghost4Man.VisitorPatternGenerator -Version 0.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ghost4Man.VisitorPatternGenerator" Version="0.1.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ghost4Man.VisitorPatternGenerator --version 0.1.2
#r "nuget: Ghost4Man.VisitorPatternGenerator, 0.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ghost4Man.VisitorPatternGenerator as a Cake Addin
#addin nuget:?package=Ghost4Man.VisitorPatternGenerator&version=0.1.2

// Install Ghost4Man.VisitorPatternGenerator as a Cake Tool
#tool nuget:?package=Ghost4Man.VisitorPatternGenerator&version=0.1.2

VisitorPatternGenerator

GitHub Nuget Nuget

C# Roslyn (incremental) source generator to help implement the visitor design pattern.

Fork

This fork (of Rekkonnect's fork of hikarin522/VisitorPatternGenerator) adds support for record types.

Usage

Example of simple integer expressions:

partial interface IExpr;
[Acceptor<IExpr>] partial record NumberLiteral(int Value) : IExpr;
[Acceptor<IExpr>] partial record BinaryOperation(IExpr Left, char Operator, IExpr Right) : IExpr;
[Acceptor<IExpr>] partial record FunctionCall(string Function, IExpr[] Args) : IExpr;

[Visitor<IExpr>] partial interface IExprVisitor<out TResult>;
[Visitor<IExpr>] partial interface IExprVisitor<in TArg, out TResult>;
[Visitor<IExpr>(voidReturn: true)] partial interface IExprVoidVisitor;
[Visitor<IExpr>(voidReturn: true)] partial interface IExprVoidVisitor<in TArg>;

and a visitor that evaluates them:

class ExpressionEvaluator : IExprVisitor<int>
{
    public int Visit(NumberLiteral expr) => expr.Value;

    public int Visit(BinaryOperation expr) => expr.Operator switch {
        '+' => expr.Left.Accept(this) + expr.Right.Accept(this),
        '*' => expr.Left.Accept(this) * expr.Right.Accept(this),
    };

    public int Visit(FunctionCall expr) => expr switch {
        ("max", [IExpr a, IExpr b]) => Math.Max(a.Accept(this), b.Accept(this)),
    };
}

used like this:

IExpr expr = new FunctionCall("max", [
    new NumberLiteral(0),
    new BinaryOperation(new NumberLiteral(40), '+', new NumberLiteral(2))
]);
var visitor = new ExpressionEvaluator();
Console.WriteLine($"Result: {expr.Accept(visitor)}");

For more samples, see https://github.com/hikarin522/VisitorPatternGenerator/tree/master/Sample/

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.2 257 12/3/2023