expression-trees

Parse Math Expression in PHP

[亡魂溺海] 提交于 2019-12-04 22:55:06
I'm currently trying to parse math expression into expression tree. But I'm stuck on the stage where I need to implement functions and negates. I don't understand logic to do it using Shunting-Yard algorithm. What I currently want to do is to support Negates, like -(x+5) Function calls, like min(x,y) Power just after function name, like cos^2(x) Implicit multiplication, like 2x is same as 2*x Scientific notation Constants e and pi Can somebody tell me hints how to implement this? An working , PSR-0 compatible implementation of the shunting yard algorithm can be found here: https://github.com

Can I generate an async method dynamically using System.Linq.Expressions?

那年仲夏 提交于 2019-12-04 22:39:51
I know the compiler can't convert an async lambda expression to an expression tree, but is it possible to generate the expression tree manually ? var expr = Expression.Lambda<Func<Task>>( // how do I use 'await' in the body here? ); var func = expr.Compile(); I can't find any method related to async or await in the Expression class, but perhaps there's another way? await involves significant compiler re-writing; the generated IL is quite dissimilar to the original C#, with variable hoisting (onto a class) and branching, tasks, continuations, etc. It certainly isn't something that can be

Are LINQ expression trees proper trees?

≡放荡痞女 提交于 2019-12-04 22:18:26
Are LINQ expression trees proper trees, as in, graphs (directed or not, wikipedia does not seem too agree) without cycles? What is the root of an expression tree from the following C# expression? (string s) => s.Length The expression tree looks like this, with "->" denoting the name of the property of the node the other node is accessible through. ->Parameters[0] Lambda---------Parameter(string s) \ / \->Body /->Expression \ / Member(Length) When using ExpressionVisitor to visit the LambdaExpression, the ParameterExpression is visited twice. Is there a way to use the ExpressionVisitor to visit

Expression tree for a member access of depth > 1

巧了我就是萌 提交于 2019-12-04 21:48:24
public class Job { public string Name { get; set; } public int Salary { get; set; } } public class Employee { public string Name { get; set; } public Job Job { get; set; } } If I want to create an expression tree of a member access to Employee.Name this is what I do: var param = Expression.Parameter(type, "x"); var memberAccess = Expression.PropertyOrField(param, memberName); return Expression.Lambda<Func<TModel, TMember>>(memberAccess, param); What is the equivalent to this for a member access to Employee.Job.Salary ? You need: var jobProperty = Expression.PropertyOrField(param, "Job"); var

Compiled Expression Trees misunderstanding?

做~自己de王妃 提交于 2019-12-04 19:55:42
问题 I have this expression : Expression<Func<string, bool>> f = s => s.Length < 5; ParameterExpression p = Expression.Parameter (typeof (string), "s"); MemberExpression stringLength = Expression.Property (p, "Length"); ConstantExpression five = Expression.Constant (5); BinaryExpression comparison = Expression.LessThan (stringLength, five); Expression<Func<string, bool>> lambda= Expression.Lambda<Func<string, bool>> (comparison, p); //lets : test Func<string, bool> runnable = lambda.Compile();

How do I infer the usage of parentheses when translating an expression tree?

喜欢而已 提交于 2019-12-04 19:46:51
问题 I am working on translating an expression tree to a format that resembles infix notation; I am not evaluating the tree or executing its operations. The tree contains both logical and relational operations, and I would like to emit parentheses in an intelligent manner during the translation. To illustrate, consider the following contrived expression: a < x & (a < y | a == c) & a != d If I walk the expression tree produced by this expression in-order, then I will print out the following

Performing part of a IQueryable query and deferring the rest to Linq for Objects

我的梦境 提交于 2019-12-04 19:44:41
问题 I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects to process the rest of the Expression tree (for things like Joins, projection etc) My thought was that I could just replace the expression constant that contains my IQueryProvider with the result-sets IEnumerable via an ExpressionVisitor and then return that new expression. Also return the IEnumerable's provider from

How do I dynamically construct a predicate method from an expression tree?

∥☆過路亽.° 提交于 2019-12-04 19:29:39
Here's the scenario: Silverlight 4.0, DataGrid, PagedCollectionView itemssource. The objective is to apply a Filter to the PCV. The filter needs to be a Predicate<object>(Method) - where Method implements some logic against the object and returns true/false for inclusion. What I have is a need to optionally include 3 different criteria in the filter logic and explicit code quickly gets ugly. We don't want that, do we? So I see that there is a way to build an expression tree using PredicateBuilder and pass that into Linq.Where, a la: IQueryable<Product> SearchProducts (params string[] keywords)

NotSupportedException when using compiled lambda expression for Average

我的未来我决定 提交于 2019-12-04 19:03:41
I tried to answer this question but failed: So let's take the original query: var result = db.Employees.GroupBy(x => x.Region) .Select(g => new { Region = g.Key, Avg = g.Average(x => x.BaseSalary)}); Works fine. Now we want to dynamically decide what to average. I try to create the lambda for Average dynamically: string property = "BaseSalary"; var parameter = Expression.Parameter(typeof(Employee)); var propAccess = Expression.PropertyOrField(parameter, property); var expression = (Expression<Func<Employee,int?>>)Expression.Lambda(propAccess, parameter); var lambda = expression.Compile(); and

Expression/Statement trees

为君一笑 提交于 2019-12-04 18:49:17
问题 Updated Question Further Down I've been experimenting with expression trees in .NET 4 to generate code at runtime and I've been trying to implement the foreach statement by building an expression tree. In the end, the expression should be able to generate a delegate that does this: Action<IEnumerable<int>> action = source => { var enumerator = source.GetEnumerator(); while(enumerator.MoveNext()) { var i = enumerator.Current; // the body of the foreach that I don't currently have yet } } I've