Comments (3)
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 LNode
s 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.
P.S. Thanks for your interest in LLLPG/Loyc, and let me know if I forgot to respond to anything!
from ecsharp.
@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)
- #r "path" works only with absolute path in compileTime directive HOT 4
- Base compileTime on dotnet-script CLI tool instead of Roslyn scripting HOT 11
- Support F#ish object expression or Java#ish anonymous clases HOT 2
- Add `define` macro with first argument treated as `this` to enable chaining HOT 29
- Comments before multi-using statements are erased HOT 4
- await fluent operator HOT 1
- Error when transforming the `typeof(X<>)` construct HOT 2
- The error Semicolon': expected Colon for the specific code HOT 2
- Support C# 9 pattern matching HOT 3
- Ref locals cannot have parentheses
- EC#: Syntax error in `Foo<T?>`
- Qualified names of identifier macros not working?
- 'with' or quick binding operator bug HOT 1
- EC#: [return: ...] attribute sometimes causes IndexOutOfRangeException in InternalList
- EC#: `#pragma warning` is not propagated to output file
- EC#: Add support for suffix `!` operator
- EC#: New `?>` operator causes parser errors in code like `X<T?>` HOT 1
- Lemp integration - custom macro is not been called
- Lemp - Match only on attribute name
- Add Deconstruct Method To All LNode types to use c# pattern matching
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ecsharp.