GithubHelp home page GithubHelp logo

Comments (3)

qwertie avatar qwertie commented on June 9, 2024

Well, me, I haven't ever had a direct use for expression trees. I don't even remember how they deal with typing information (is it... optionally typed?). I probably used them in Linq-to-SQL, but that's it, since I find parsing of "any executable code block" or "any code block" to be a much more useful concept. So I am curious, if you had a expression-only parser, what would you use it for?

It would be really appreciated if you could figure out how to parse LINQ expressions into LNodes first by modifying the existing Ecs/Parser/EcsParserGrammar.les in this repo... but if you prefer to go the other route and directly produce expression trees from all expessions (including LINQ), I'd be curious to see the results. Back when I looked into parsing LINQ expressions, I had difficulty finding complete information about the various kinds of LINQ expressions and how they are translated into C#-without-lambda-expressions. Technically, that information can be found in the C# language reference, but it is not easy reading.

If you want to rip out the EC# expression parser and turn it into a plain-C# expression parser, you'd want to remove the EC#-specific things. The following comes to mind:

  • EC# allows braced blocks in expressions, like 2 + {bool c = x*y; if (c > 0) x else y}
  • EC# allows tuples like (x, y)
  • EC# allows variable declarations in expressions, like (var Foo2 = Foo*Foo) * Foo2

Making it more complicated to remove the latter two features is the fact that there is no special rule for lambda expressions; lambda is treated like any other infix operator! Thus 5 => 3+2 is valid, and (x, y) => z has a tuple on the LHS. So... it would be easier to write a converter from LNode to expression trees, but then your code will have more dependencies than you probably wanted.

Just a reminder: if you add Ecs.exe and its dependencies like Loyc.Syntax.dll as references to your project, you can parse a specified expression by calling

LNode expr = Ecs.Parser.EcsLanguageService.Value.ParseSingle(
    "your_expression(here)", null, ParsingService.Exprs);

To avoid creating intermediate LNodes, I think you could make new derived classes of System.Linq.Expressions.Expression to represent tuples and variable declarations, and then postprocess the output of the parser from (A a, B b) => expr (where the left-hand side is a tuple) into an Expression<A, B>. (Btw I'm a little confused: when I look at the documentation of Expression it doesn't seem to have type parameters, and I don't see any links to the version of Expression that does have type parameters.)

from ecsharp.

qwertie avatar qwertie commented on June 9, 2024

P.S. Thanks for your interest in LLLPG/Loyc, and let me know if I forgot to respond to anything!

from ecsharp.

qwertie avatar qwertie commented on June 9, 2024

@glopesdev it appears based on this question that you can already parse a string into an expression tree at run-time.

However, it would be straightforward to do it in Enhanced C# using matchCode - have a look at this example and imagine replacing the "Eval" function with a "CreateExpressionTree" function.

from ecsharp.

Related Issues (20)

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.