All Projects → zspitz → Expressiontreetostring

zspitz / Expressiontreetostring

Licence: mit
String representations of expression trees + library of expression tree objects

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Expressiontreetostring

Graphqlgen
⚙️ Generate type-safe resolvers based upon your GraphQL Schema
Stars: ✭ 796 (+1183.87%)
Mutual labels:  code-generation
Drupal Console
The Drupal CLI. A tool to generate boilerplate code, interact with and debug Drupal.
Stars: ✭ 913 (+1372.58%)
Mutual labels:  code-generation
Spoon
Spoon is a metaprogramming library to analyze and transform Java source code (up to Java 15). 🥄 is made with ❤️, 🍻 and ✨. It parses source files to build a well-designed AST with powerful analysis and transformation API.
Stars: ✭ 1,078 (+1638.71%)
Mutual labels:  code-generation
Voucher Code Generator Java
Stars: ✭ 18 (-70.97%)
Mutual labels:  code-generation
Gnostic
A compiler for APIs described by the OpenAPI Specification with plugins for code generation and other API support tasks.
Stars: ✭ 870 (+1303.23%)
Mutual labels:  code-generation
Devflowcharter
Flowchart builder and code generator.
Stars: ✭ 41 (-33.87%)
Mutual labels:  code-generation
Casadi
CasADi is a symbolic framework for numeric optimization implementing automatic differentiation in forward and reverse modes on sparse matrix-valued computational graphs. It supports self-contained C-code generation and interfaces state-of-the-art codes such as SUNDIALS, IPOPT etc. It can be used from C++, Python or Matlab/Octave.
Stars: ✭ 714 (+1051.61%)
Mutual labels:  code-generation
Pymbolic
A simple package to do symbolic math (focus on code gen and DSLs)
Stars: ✭ 57 (-8.06%)
Mutual labels:  code-generation
Maple
Elixir GraphQL Client | Compile time client code generator for GraphQL APIs based on introspection queries and schema files
Stars: ✭ 20 (-67.74%)
Mutual labels:  code-generation
Cognicrypt
CogniCrypt is an Eclipse plugin that supports Java developers in using Java Cryptographic APIs.
Stars: ✭ 50 (-19.35%)
Mutual labels:  code-generation
Polygen
PolyGen is a code generator that produces database schema, ORM layer, REST API and a (coming soon — stay tuned!) single-page web UI for your business model.
Stars: ✭ 19 (-69.35%)
Mutual labels:  code-generation
Go Zero
go-zero is a web and rpc framework written in Go. It's born to ensure the stability of the busy sites with resilient design. Builtin goctl greatly improves the development productivity.
Stars: ✭ 13,156 (+21119.35%)
Mutual labels:  code-generation
Dogen
Reference implementation of the MASD Code Generator.
Stars: ✭ 44 (-29.03%)
Mutual labels:  code-generation
Mappinggenerator
🔄 "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
Stars: ✭ 831 (+1240.32%)
Mutual labels:  code-generation
Ffcx
Next generation FEniCS Form Compiler
Stars: ✭ 55 (-11.29%)
Mutual labels:  code-generation
Scrooge
A Thrift parser/generator
Stars: ✭ 724 (+1067.74%)
Mutual labels:  code-generation
Gradle Xjc Plugin
A Gradle plugin to run the XJC binding compiler during a build
Stars: ✭ 38 (-38.71%)
Mutual labels:  code-generation
Genco
A whitespace-aware quasiquoter for beautiful code generation.
Stars: ✭ 59 (-4.84%)
Mutual labels:  code-generation
Idris Elixir
A code-generator for Idris that targets Elixir
Stars: ✭ 56 (-9.68%)
Mutual labels:  code-generation
Scala Db Codegen
Scala code/boilerplate generator from a db schema
Stars: ✭ 49 (-20.97%)
Mutual labels:  code-generation

Expression Tree To String

AppVeyor build status Tests NuGet Status       Test objects: NuGet TestObjects Status

Targets .NET Standard 2.0 and above / Framework 4.5.2 and above

Provides a ToString extension method which returns a string representation of an expression tree (an object inheriting from System.Linq.Expressions.Expression).

Expression<Func<bool>> expr = () => true;

Console.WriteLine(expr.ToString("C#"));
// prints: () => true

Console.WriteLine(expr.ToString("Visual Basic"));
// prints: Function() True

Console.WriteLine(expr.ToString("Factory methods", "C#"));
// prints:
/*
    // using static System.Linq.Expressions.Expression

    Lambda(
        Constant(true)
    )
*/

Console.WriteLine(expr.ToString("Object notation", "C#"));
// prints:
/*
    new Expression<Func<bool>> {
        NodeType = ExpressionType.Lambda,
        Type = typeof(Func<bool>),
        Body = new ConstantExpression {
            Type = typeof(bool),
            Value = true
        },
        ReturnType = typeof(bool)
    }
*/

Console.WriteLine(expr.ToString("Object notation", "Visual Basic"));
// prints:
/*
    New Expression(Of Func(Of Boolean)) With {
        .NodeType = ExpressionType.Lambda,
        .Type = GetType(Func(Of Boolean)),
        .Body = New ConstantExpression With {
            .Type = GetType(Boolean),
            .Value = True
        },
        .ReturnType = GetType(Boolean)
    }
*/

Console.WriteLine(expr.ToString("Textual tree"));
// prints:
/*
    Lambda (Func<bool>)
        Body - Constant (bool) = True
*/

var b = true;
Expression<Func<int>> expr1 = () => b ? 1 : 0;
Console.WriteLine(expr1.ToString("ToString"));
// prints:
/*
    () => IIF(value(_tests.Program+<>c__DisplayClass0_0).b, 1, 0)
*/

Console.WriteLine(expr1.ToString("DebugView"));
// prints:
/*
    .Lambda #Lambda1<System.Func`1[System.Int32]>() {
        .If (
            .Constant<_tests.Program+<>c__DisplayClass0_0>(_tests.Program+<>c__DisplayClass0_0).b
        ) {
            1
        } .Else {
            0
        }
    }
*/

Expression<Func<Person, bool>> filter = p => p => p.LastName == "A" || p.FirstName == "B" || p.DOB == DateTime.MinValue || p.LastName == "C" || p.FirstName == "D";
Console.WriteLine(filter.ToString("Dynamic LINQ"));
// prints:
/*
    LastName in ("A", "C") || DOB == DateTime.MinValue || FirstName in ("B", "D")
*/

Features:

  • Multiple writers:

    • Pseudo-code in C# or VB.NET
    • Factory method calls which generate this expression
    • Object notation, using object initializer and collection initializer syntax to describe objects
    • Textual tree, focusing on the properties related to the structure of the tree
    • ToString and DebugView reimplementation
    • Dynamic LINQ equivalent to the expression
  • For C# and VB pseudo-code representations:

    • Extension methods are rendered as instance methods

      Expression<Func<int, int>> expr = x => Enumerable.Range(1, x).Select(y => x * y).Count();
      Console.WriteLine(expr.ToString("C#"));
      // prints: (int x) => Enumerable.Range(1, x).Select((int y) => x * y).Count()
      
    • Closed-over variables are rendered as simple identifiers (instead of member access on the hidden compiler-generated class)

      var i = 7;
      var j = 8;
      Expression<Func<int>> expr = () => i + j;
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => i + j
      
    • Calls to String.Concat and String.Format are mapped to the + operator and string interpolation, respectively (where possible):

      var name = "World";
      Expression<Func<string>> expr = () => string.Format("Hello, {0}!", name);
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => $"Hello, {name}!"
      
    • Unnecessary conversions are not rendered:

      Expression<Func<IEnumerable<char>>> expr = () => (IEnumerable<char>)"abcd";
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => "abcd"
      
    • Comparisons against an enum are rendered properly, not as comparison to int-converted value:

      var dow = DayOfWeek.Sunday;
      Expression<Func<bool>> expr = () => DateTime.Today.DayOfWeek == dow;
      
      Console.WriteLine(expr.ToString("Textual tree", "C#"));
      // prints:
      /*
        Lambda (Func<bool>)
            · Body - Equal (bool) = false
                · Left - Convert (int) = 3
                    · Operand - MemberAccess (DayOfWeek) DayOfWeek = DayOfWeek.Wednesday
                        · Expression - MemberAccess (DateTime) DateTime.Today = 30/09/2020 12:00:00 am
                · Right - Convert (int) = 0
                    · Operand - MemberAccess (DayOfWeek) dow = DayOfWeek.Sunday
                        · Expression - Constant (<closure>) = #<closure>      
      */
      
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => DateTime.Today.DayOfWeek == dow
      
  • Each representation (including the ToString and DebugView renderers) can return the start and length of the substring corresponding to any of the paths of the tree's nodes, which can be used to find the substring corresponding to a given node in the tree:

    var s = expr.ToString("C#", out var pathSpans);
    Console.WriteLine(s);
    // prints: (Person p) => p.DOB.DayOfWeek == DayOfWeek.Tuesday
    
    (int start, int length) = pathSpans["Body.Left.Operand"];
    Console.WriteLine(s.Substring(start, length));
    // prints: p.DOB.DayOfWeek
    
  • Type names are rendered using language syntax and keywords, instead of the Type.Name property; e.g. List<string> or List(Of Date) instead of List`1

  • Supports the full range of types in System.Linq.Expressions, including .NET 4 expression types, and DynamicExpression

  • Extensibility -- allows creating custom renderers, or inheriting from existing renderers, to handle your own Expression-derived types

For more information, see the wiki.

Feedback

  • Star the project
  • File an issue
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].