Пример #1
0
 public AphidPartialFunction(
     //AphidExpression expression,
     AphidFunction function,
     AphidObject[] applied)
 {
     //Expression = expression;
     Function = function;
     Applied  = applied;
 }
Пример #2
0
        public Delegate ConvertFunctionWrapper(
            MethodInfo method,
            Type delegateType,
            AphidFunction function,
            Type[] genericArguments)
        {
            var methodParams = method.GetParameters();
            var wrapper      = new AphidFunctionWrapper(Interpreter, function);

            var call = wrapper
                       .GetType()
                       .GetMethods()
                       .SingleOrDefault(x =>
                                        x.Name == "Call" &&
                                        (method.ReturnType == typeof(void) ?
                                         x.ReturnType == typeof(void) :
                                         x.ReturnType != typeof(void)) &&
                                        methodParams.Length == x.GetParameters().Length);

            if (call == null)
            {
                throw Interpreter.CreateRuntimeException(
                          "Could not match conversion function for {0}",
                          method);
            }

            if (methodParams.Length > 0 ||
                method.ReturnType != typeof(void))
            {
                var paramTypes = methodParams
                                 .Select(x => !x.ParameterType.IsGenericParameter ?
                                         x.ParameterType :
                                         genericArguments[x.ParameterType.GenericParameterPosition]);

                call = call.MakeGenericMethod(
                    (method.ReturnType.IsGenericParameter ?
                     paramTypes.Concat(new[] { genericArguments[method.ReturnType.GenericParameterPosition] }) :
                     method.ReturnType != typeof(void) ?
                     paramTypes.Concat(new[] { method.ReturnType }) :
                     paramTypes)
                    .ToArray());
            }

            if (delegateType.ContainsGenericParameters)
            {
                var curDelegateArgs    = delegateType.GetGenericArguments();
                var delegateTypeParams = new Type[curDelegateArgs.Length];

                for (var i = 0; i < curDelegateArgs.Length; i++)
                {
                    delegateTypeParams[i] = !curDelegateArgs[i].IsGenericParameter ?
                                            curDelegateArgs[i] :
                                            genericArguments[curDelegateArgs[i].GenericParameterPosition];
                }

                delegateType = delegateType
                               .GetGenericTypeDefinition()
                               .MakeGenericType(delegateTypeParams);
            }

            return(call.CreateDelegate(delegateType, wrapper));
        }
Пример #3
0
 public AphidFunctionWrapper(AphidInterpreter interpreter, AphidFunction function)
     : base(interpreter) => Function = function;