Ejemplo n.º 1
0
        private static void AssertAreEqual(Func <int, int> func, Expression <Func <int> > expected)
        {
            var orig = Expression.Lambda <Func <int> >(
                Expression.Invoke(
                    Expression.Constant(func, typeof(Func <int, int>)),
                    Expression.Constant(1)));

            var actual = ImproveDelegateInvocations.Apply(orig);

            Assert.AreEqual(expected.GetDebugString(), actual.GetDebugString());
        }
Ejemplo n.º 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);
        }