예제 #1
0
        public void InlineLambdas_Single()
        {
            var p = Expression.Parameter(typeof(int), "p");
            var a = Expression.Parameter(typeof(int), "a");

            var input = Expression.Lambda(
                Expression.Invoke(
                    Expression.Lambda(Expression.Add(a, a), a),
                    p),
                p);
            var actual   = InlineLambdaInvocations.Visit(input);
            var expected = Expression.Lambda(Expression.Add(p, p), p);

            AreEqual(expected, actual);
        }
예제 #2
0
        public void Append(ProcessorInfo o)
        {
            if (o == null)
            {
                throw new ArgumentNullException(nameof(o));
            }

            var ps      = o.Inputs.Select(ParameterFor);
            var ifs     = IsPresentFor(o.Inputs);
            var isMaybe = ifs != null;

            Expression e = Expression.Invoke(o.Expr, ps);

            // apply some performance improvements
            e = ImproveDelegateInvocations.Apply(e);
            e = InlineLambdaInvocations.Visit(e);

            if (o.Output != null)
            {
                var t = o.Expr.Body.Type;

                // does the result type need to be lifted from T to Maybe<T>?
                var needsLift = isMaybe && !o.Expr.Type.IsMaybe();

                if (needsLift)
                {
                    t = t.ToMaybe();
                    e = Expression.Call(t, "Just", null, e);
                }

                var v = Expression.Variable(t, o.Output.Description.Name);
                e = Expression.Assign(v, e);
                _variables.Add(v);
            }

            if (ifs != null)
            {
                e = Expression.IfThen(ifs, e);
            }

            _expressions.Add(e);
        }
예제 #3
0
        public void InlineLambdas_Five()
        {
            var p = Expression.Parameter(typeof(int), "p");
            var a = Expression.Parameter(typeof(int), "a");
            var b = Expression.Parameter(typeof(int), "b");
            var c = Expression.Parameter(typeof(int), "c");
            var d = Expression.Parameter(typeof(int), "d");
            var e = Expression.Parameter(typeof(int), "e");

            var input = Expression.Lambda(
                Expression.Invoke(
                    Expression.Lambda(
                        Expression.Invoke(
                            Expression.Lambda(
                                Expression.Invoke(
                                    Expression.Lambda(
                                        Expression.Invoke(
                                            Expression.Lambda(
                                                Expression.Invoke(
                                                    Expression.Lambda(Expression.Add(e, e), e),
                                                    d),
                                                d),
                                            c),
                                        c),
                                    b),
                                b),
                            a),
                        a),
                    p),
                p);

            var actual   = InlineLambdaInvocations.Visit(input);
            var expected = Expression.Lambda(Expression.Add(p, p), p);

            AreEqual(expected, actual);
        }