GetBinaryBinder() static private method

static private GetBinaryBinder ( TokenType tokenType ) : System.Runtime.CompilerServices.CallSiteBinder
tokenType TokenType
return System.Runtime.CompilerServices.CallSiteBinder
Beispiel #1
0
        private static Expression GetCompareAST(IList <ParseNode> nodes, int start, Expression context, Expression chain = null)
        {
            //Rewrite chained compare expressions to chained AND expressions, e.g. 5>4>3> --> 5>4 AND 4>3
            Debug.Assert(nodes.Count >= 3 && nodes.Count % 2 == 1);
            Expression result;

            if (start == 0)
            {
                result = chain;
            }
            else
            {
                var link = Expression.Dynamic(DLRUtil.GetBinaryBinder(nodes[start - 1].Token.Type),
                                              typeof(object),
                                              nodes[start - 2].GetAST(context),
                                              nodes[start].GetAST(context));
                chain = chain != null
                            ? Expression.Dynamic(DLRUtil.GetBinaryBinder(TokenType.AND),
                                                 typeof(object),
                                                 link,
                                                 chain)
                            : link;

                result = GetCompareAST(nodes, start - 2, context, chain);
            }
            return(result);
        }
Beispiel #2
0
 private static Expression GetBinaryAST(IList <ParseNode> nodes, int start, Expression context)
 {
     //chain from left to right
     //2 + 3 + 4 is calculated as (2+3)+4, 15%12%5 as (15%12)%5
     Debug.Assert(nodes.Count >= 3 && nodes.Count % 2 == 1);
     return(start == 0
                ? nodes[start].GetAST(context)
                : Expression.Dynamic(DLRUtil.GetBinaryBinder(nodes[start - 1].Token.Type),
                                     typeof(object),
                                     GetBinaryAST(nodes, start - 2, context),
                                     nodes[start].GetAST(context)));
 }
        private static Expression GetBinaryAST(IList <ParseNode> nodes, int start, Expression context)
        {
            //chain from left to right
            //2 + 3 + 4 is calculated as (2+3)+4, 15%12%5 as (15%12)%5
            Debug.Assert(nodes.Count >= 3 && nodes.Count % 2 == 1);

            Expression result;

            if (start == 0)
            {
                result = nodes[start].GetAST(context);
            }
            else
            {
                var tokenType = nodes[start - 1].Token.Type;
                var left      = GetBinaryAST(nodes, start - 2, context);
                var right     = nodes[start].GetAST(context);

                if (tokenType == TokenType.AND)
                {
                    result = Expression.AndAlso(
                        Expression.Convert(left, typeof(bool)),
                        Expression.Convert(right, typeof(bool)));
                }
                else if (tokenType == TokenType.OR)
                {
                    result = Expression.OrElse(
                        Expression.Convert(left, typeof(bool)),
                        Expression.Convert(right, typeof(bool)));
                }
                else
                {
                    result = Expression.Dynamic(
                        DLRUtil.GetBinaryBinder(tokenType),
                        typeof(object),
                        left,
                        right);
                }
            }

            return(result);
        }