Esempio n. 1
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            // arguments for the dynamic method call
            LinkedList <DLR.Expression> callArguments = new LinkedList <DLR.Expression>();

            // add the parameters in !reverse! order
            if (this.leftarg != null)
            {
                callArguments.AddFirst(this.leftarg.Generate(scope));
            }

            callArguments.AddFirst(this.function.Generate(scope));

            if (this.condition != null)
            {
                callArguments.AddFirst(this.condition.Generate(scope));
            }

            callArguments.AddFirst(this.rightarg.Generate(scope));

            // add A+ environment as first argument for user defined functions
            callArguments.AddFirst(scope.GetRuntimeExpression());
            callArguments.AddFirst(this.name.Generate(scope));

            return(AST.UserDefInvoke.BuildInvoke(runtime, callArguments));
        }
Esempio n. 2
0
        private DLR.Expression GenerateMonadic(AplusScope scope, DLR.Expression argument)
        {
            DLR.Expression result;
            // Handle the result monadic function a little differently:
            if (this.token.Type == Tokens.RESULT)
            {
                result = scope.ReturnTarget != null
                         // If inside of a function create a return expression Tree
                    ? DLR.Expression.Return(scope.ReturnTarget, argument, typeof(AType))
                         // Otherwise just return the value
                    : argument
                ;
            }
            else
            {
                AbstractMonadicFunction method = MethodChooser.GetMonadicMethod(this.token);

                if (method == null)
                {
                    throw new ParseException(String.Format("Not supported Monadic function[{0}]", this.token));
                }

                result = DLR.Expression.Call(
                    DLR.Expression.Constant(method),
                    method.GetType().GetMethod("Execute"),
                    argument,
                    scope.GetRuntimeExpression()
                    );
            }

            return(result);
        }
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            // arguments for the dynamic method call
            LinkedList<DLR.Expression> callArguments = new LinkedList<DLR.Expression>();

            // add the parameters in !reverse! order
            if (this.leftarg != null)
            {
                callArguments.AddFirst(this.leftarg.Generate(scope));
            }

            callArguments.AddFirst(this.function.Generate(scope));

            if (this.condition != null)
            {
                callArguments.AddFirst(this.condition.Generate(scope));
            }

            callArguments.AddFirst(this.rightarg.Generate(scope));

            // add A+ environment as first argument for user defined functions
            callArguments.AddFirst(scope.GetRuntimeExpression());
            callArguments.AddFirst(this.name.Generate(scope));

            return AST.UserDefInvoke.BuildInvoke(runtime, callArguments);
        }
Esempio n. 4
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression func;

            if (this.function is Token)
            {
                Node wrappedFunction = new BuiltInFunction((Token)this.function);
                func = wrappedFunction.Generate(scope);
            }
            else
            {
                func = this.function.Generate(scope);
            }

            DLR.Expression result;

            if (isDyadic)
            {
                result = DLR.Expression.Call(
                    DLR.Expression.Constant(DyadicOperatorInstance.Rank),
                    DyadicOperatorInstance.Rank.GetType().GetMethod("Execute"),
                    func,
                    this.condition.Generate(scope),
                    this.rightarg.Generate(scope),
                    this.leftarg.Generate(scope),
                    scope.GetRuntimeExpression()
                    );
            }
            else
            {
                result = DLR.Expression.Call(
                    DLR.Expression.Constant(MonadicOperatorInstance.Rank),
                    MonadicOperatorInstance.Rank.GetType().GetMethod("Execute"),
                    func,
                    this.condition.Generate(scope),
                    this.rightarg.Generate(scope),
                    scope.GetRuntimeExpression()
                    );
            }

            return(result);
        }
Esempio n. 5
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression func;

            if (this.function is Token)
            {
                Node wrappedFunction = new BuiltInFunction((Token)this.function);
                func = wrappedFunction.Generate(scope);
            }
            else
            {
                func = this.function.Generate(scope);
            }

            DLR.Expression result;

            if (isDyadic)
            {
                result = DLR.Expression.Call(
                    DLR.Expression.Constant(DyadicOperatorInstance.Rank),
                    DyadicOperatorInstance.Rank.GetType().GetMethod("Execute"),
                    func,
                    this.condition.Generate(scope),
                    this.rightarg.Generate(scope),
                    this.leftarg.Generate(scope),
                    scope.GetRuntimeExpression()
                );
            }
            else
            {
                result = DLR.Expression.Call(
                DLR.Expression.Constant(MonadicOperatorInstance.Rank),
                MonadicOperatorInstance.Rank.GetType().GetMethod("Execute"),
                func,
                this.condition.Generate(scope),
                this.rightarg.Generate(scope),
                scope.GetRuntimeExpression()
            );
            }

            return result;
        }
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            if (scope.IsEval && scope.IsMethod)
            {
                // we are inside a function and an eval block.

                // override the original eval scope
                // create a top level scope
                scope = new AplusScope(null, "_EVAL_FUNC_SCOPE_",
                    scope.GetRuntime(),
                    scope.GetRuntimeExpression(),
                    scope.Parent.GetModuleExpression(),
                    scope.ReturnTarget,
                    isEval: true
                );
            }

            string operatorName = this.name.BuildQualifiedName(runtime.CurrentContext);

            string scopename = String.Format("__operator_{0}_scope__", this.name.Name);
            AplusScope methodScope = new AplusScope(scope, scopename,
                runtimeParam: DLR.Expression.Parameter(typeof(Aplus), "_EXTERNAL_RUNTIME_"),
                moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), scopename),
                returnTarget: DLR.Expression.Label(typeof(AType), "RETURN"),
                isMethod: true
            );

            // create a result parameter
            DLR.ParameterExpression resultParameter = DLR.Expression.Parameter(typeof(AType), "__RESULT__");

            // create function's parameters
            LinkedList<DLR.ParameterExpression> methodParameters = new LinkedList<DLR.ParameterExpression>();

            // add parameters to the linkedlist
            BuildParameterExpression(methodScope, methodParameters, leftArgument);
            BuildParameterExpression(methodScope, methodParameters, function);
            BuildParameterExpression(methodScope, methodParameters, condition);
            BuildParameterExpression(methodScope, methodParameters, rightArgument);

            // add parameter for AplusEnviroment
            methodParameters.AddFirst(methodScope.RuntimeExpression);

            // create the lambda method for the function
            DLR.LambdaExpression method = DLR.Expression.Lambda(
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { methodScope.ModuleExpression, resultParameter },
                // add the local scope's store
                    DLR.Expression.Assign(methodScope.ModuleExpression, DLR.Expression.Constant(new DYN.ExpandoObject())),
                // set AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        methodScope.ModuleExpression
                    ),
                // calculate the result of the defined function
                    DLR.Expression.Assign(
                        resultParameter,
                        DLR.Expression.Label(methodScope.ReturnTarget, this.codeblock.Generate(methodScope))
                    ),
                // reset  AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        DLR.Expression.Constant(null, typeof(DYN.ExpandoObject))
                    ),
                // return the result
                    resultParameter
                ),
                operatorName,
                methodParameters
            );

            // wrap the lambda method inside an AFunc
            DLR.Expression wrappedLambda = DLR.Expression.Call(
                typeof(AFunc).GetMethod("CreateUserOperator"),
                DLR.Expression.Constant(operatorName),
                DLR.Expression.Constant(this.IsDyadicOperator),
                method,
                DLR.Expression.Constant(methodParameters.Count),
                DLR.Expression.Constant(this.codeText)
            );

            // set the variable to the lambda function
            DLR.Expression setMethod = VariableHelper.SetVariable(
                runtime,
                scope.ModuleExpression,
                this.name.CreateContextNames(runtime.CurrentContext),
                wrappedLambda
            );

            // ensure the result type to be an AType
            DLR.Expression result = DLR.Expression.Convert(setMethod, typeof(AType));

            return result;
        }
Esempio n. 7
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression result;

            if (scope.IsAssignment && TokenUtils.AllowedPrimitiveFunction(this.token.Type))
            {
                /*
                 * input: y -> left side, x -> right side, value
                 * Perform the function like this:
                 *
                 * i := f{y; iota rho x}
                 * (,x)[i] := value
                 *
                 * where 'f' is the dyadic function
                 */
                DLR.Expression left  = this.leftExpression.Generate(scope);
                DLR.Expression right = Node.TestMonadicToken(this.rightExpression, Tokens.RAVEL)
                    ? ((MonadicFunction)this.rightExpression).Expression.Generate(scope)
                    : this.rightExpression.Generate(scope)
                ;

                // i:=(iota rho x)
                DLR.Expression indices = AST.Assign.BuildIndicesList(scope, right);
                // (,x)[f{a;i}]
                result = AST.Assign.BuildIndexing(scope, right, GenerateDyadic(scope, indices, left));
            }
            else if (scope.IsAssignment && this.token.Type == Tokens.CHOOSE)
            {
                scope.IsAssignment = false;
                DLR.Expression left = this.leftExpression.Generate(scope);

                scope.IsAssignment = true;
                DLR.Expression right = this.rightExpression.Generate(scope);

                result =
                    DLR.Expression.Block(
                        DLR.Expression.Assign(scope.CallbackInfo.Index, left),
                        DLR.Expression.Call(
                            DLR.Expression.Constant(DyadicFunctionInstance.Choose),
                            DyadicFunctionInstance.Choose.GetType().GetMethod("Assign"),
                            right, left, scope.GetRuntimeExpression()
                            )
                        );
            }
            else if (scope.IsAssignment && this.token.Type == Tokens.PICK)
            {
                scope.IsAssignment = false;
                DLR.Expression left = this.leftExpression.Generate(scope);

                scope.IsAssignment = true;
                DLR.Expression right = this.rightExpression.Generate(scope);

                result =
                    DLR.Expression.Block(
                        DLR.Expression.Assign(scope.CallbackInfo.Path, left),
                        DLR.Expression.Call(
                            DLR.Expression.Constant(DyadicFunctionInstance.Pick),
                            DyadicFunctionInstance.Pick.GetType().GetMethod("Execute"),
                            right, left, scope.GetRuntimeExpression()
                            )
                        );
            }
            else
            {
                DLR.Expression left  = this.leftExpression.Generate(scope);
                DLR.Expression right = this.rightExpression.Generate(scope);
                result = GenerateDyadic(scope, right, left);
            }

            return(result);
        }
Esempio n. 8
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            PreprocessVariables(scope);

            DLR.Expression result = null;
            Aplus runtime = scope.GetRuntime();

            if (scope.IsEval && scope.IsMethod)
            {
                // override the original scope
                // create a top level scope
                scope = new AplusScope(null, "_EVAL_DEP_SCOPE_",
                    scope.GetRuntime(),
                    scope.GetRuntimeExpression(),
                    scope.Parent.GetModuleExpression(),
                    scope.ReturnTarget,
                    isEval: true
                );
            }

            // 1. Create a function scope
            string dependencyName = this.variable.BuildQualifiedName(runtime.CurrentContext);
            string scopename = String.Format("__dependency_{0}_scope__", this.variable.Name);
            AplusScope dependencyScope = new AplusScope(scope, scopename,
                runtimeParam: scope.GetRuntimeExpression(),
                moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), scopename),
                returnTarget: DLR.Expression.Label(typeof(AType), "RETURN"),
                isMethod: true
            );
            // 1.5. Method for registering dependencies
            MethodInfo registerMethod;

            // 2. Prepare the method arguments (RuntimeExpression)
            DLR.ParameterExpression[] methodParameters;

            if (this.IsItemwise)
            {
                DLR.ParameterExpression index = 
                    DLR.Expression.Parameter(typeof(AType), string.Format("__INDEX[{0}]__", this.Indexer.Name));
                dependencyScope.Variables.Add(this.Indexer.Name, index);

                methodParameters = new DLR.ParameterExpression[] { dependencyScope.RuntimeExpression, index };
                registerMethod = typeof(DependencyManager).GetMethod("RegisterItemwise");
            }
            else
            {
                methodParameters = new DLR.ParameterExpression[] { dependencyScope.RuntimeExpression };
                registerMethod = typeof(DependencyManager).GetMethod("Register");
            }

            // 2.5  Create a result parameter
            DLR.ParameterExpression resultParameter = DLR.Expression.Parameter(typeof(AType), "__RESULT__");

            // 2.75 Get the dependency informations
            DLR.Expression dependencyInformation =
                DLR.Expression.Call(
                    DLR.Expression.Property(dependencyScope.GetRuntimeExpression(), "DependencyManager"),
                    typeof(DependencyManager).GetMethod("GetDependency"),
                    DLR.Expression.Constant(dependencyName)
                );

            // 3. Build the method
            DLR.LambdaExpression method = DLR.Expression.Lambda(
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { dependencyScope.ModuleExpression, resultParameter },
                    // Add the local scope's store
                    DLR.Expression.Assign(dependencyScope.ModuleExpression, DLR.Expression.Constant(new DYN.ExpandoObject())),
                    // set AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(dependencyScope.RuntimeExpression, "FunctionScope"),
                        dependencyScope.ModuleExpression
                    ),
                    // Mark the dependency as under evaluation
                    DLR.Expression.Call(
                        dependencyInformation,
                        typeof(DependencyItem).GetMethod("Mark"),
                        DLR.Expression.Constant(DependencyState.Evaluating)
                    ),
                    // Calculate the result of the defined function
                    DLR.Expression.Assign(
                        resultParameter,
                        DLR.Expression.Label(dependencyScope.ReturnTarget, this.functionBody.Generate(dependencyScope))
                    ),
                    // Mark the dependency as valid
                    DLR.Expression.Call(
                        dependencyInformation,
                        typeof(DependencyItem).GetMethod("Mark"),
                        DLR.Expression.Constant(DependencyState.Valid)
                    ),
                    // reset  AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(dependencyScope.RuntimeExpression, "FunctionScope"),
                        DLR.Expression.Constant(null, typeof(DYN.ExpandoObject))
                    ),
                    // Return the result
                    resultParameter
                ),
                dependencyName,
                methodParameters
            );

            DLR.Expression wrappedLambda = DLR.Expression.Call(
                typeof(AFunc).GetMethod("Create"),
                DLR.Expression.Constant(dependencyName),
                method,
                DLR.Expression.Constant(methodParameters.Length),
                DLR.Expression.Constant(this.codeText)
            );

            // 3.5 Build dependant set
            // filter out the variables from the dependant set if it is a local variable
            HashSet<string> dependents = new HashSet<string>(
                from pair in this.variables.Accessing                           // get all variables
                where !this.variables.LocalAssignment.ContainsKey(pair.Key)     // but skip the local variables 
                select pair.Value[0].BuildQualifiedName(runtime.CurrentContext) // then build the correct name
            );

            // 4. Register the method for the Dependency manager
            DLR.ParameterExpression dependencyMethodParam = DLR.Expression.Parameter(typeof(AType), "__DEP._METHOD__");
            DLR.Expression dependencyManager = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");
            result = DLR.Expression.Block(
                new DLR.ParameterExpression[] { dependencyMethodParam },
                DLR.Expression.Assign(dependencyMethodParam, wrappedLambda),
                DLR.Expression.Call(
                    dependencyManager,
                    registerMethod,
                    DLR.Expression.Constant(dependencyName, typeof(string)),
                    DLR.Expression.Constant(dependents, typeof(HashSet<string>)),
                    dependencyMethodParam
                ),
                dependencyMethodParam
            );

            return result;
        }
Esempio n. 9
0
        private DLR.Expression BuildPickAssign(AplusScope scope, DLR.Expression value)
        {
            DyadicFunction function = (DyadicFunction)this.target;
            bool isValueinContext = Node.TestDyadicToken(function.Left, Grammar.Tokens.VALUEINCONTEXT);
            bool isValue = Node.TestMonadicToken(function.Right, Grammar.Tokens.VALUE);

            if (!(function.Right is Identifier || isValueinContext || isValue))
            {
                throw new ParseException("assign pick target", false);
            }

            DLR.Expression left = function.Left.Generate(scope);

            scope.IsAssignment = true;
            DLR.Expression right = function.Right.Generate(scope);
            scope.IsAssignment = false;

            var method = DyadicFunctionInstance.Pick;
            DLR.Expression pickDLR = DLR.Expression.Call(
                DLR.Expression.Constant(method),
                method.GetType().GetMethod("AssignExecute"),
                right,
                left,
                scope.GetRuntimeExpression()
            );

            DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal));

            var flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static;

            DLR.ParameterExpression temp = DLR.Expression.Parameter(typeof(AType));
            DLR.Expression callback = BuildCallbackCall(scope, temp);
            DLR.Expression presetCallback = BuildPresetCallbackCall(scope, temp);

            DLR.Expression result =
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { temp },
                    DLR.Expression.Assign(scope.CallbackInfo.Path, left),
                    DLR.Expression.Assign(temp, value),
                    DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                    DLR.Expression.TryCatch(
                        DLR.Expression.Block(
                            typeof(void),
                            DLR.Expression.Assign(temp, presetCallback),
                            DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                        ),
                        DLR.Expression.Catch(
                            errorParam,
                            DLR.Expression.Empty()
                        )
                    ),
                    DLR.Expression.IfThen(
                        scope.AssignDone,
                        DLR.Expression.Block(
                            DLR.Expression.Call(
                                typeof(Assign).GetMethod("PickAssignHelper", flags),
                                temp,
                                pickDLR
                            ),
                            callback
                        )
                    ),
                    scope.CallbackInfo.NonPresetValue
                );

            return result;
        }
Esempio n. 10
0
        /// <summary>
        /// Builds DLR expression for:  (iota rho argument)
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="argument"></param>
        /// <returns></returns>
        internal static DLR.Expression BuildIndicesList(AplusScope scope, DLR.Expression argument)
        {
            DLR.ParameterExpression environment = scope.GetRuntimeExpression();
            var execute = typeof(AbstractMonadicFunction).GetMethod("Execute");

            // (iota rho x)
            DLR.Expression indexes = DLR.Expression.Call(
                DLR.Expression.Constant(MonadicFunctionInstance.Interval),
                execute,
                DLR.Expression.Call(
                    DLR.Expression.Constant(MonadicFunctionInstance.Shape),
                    execute,
                    argument,
                    environment
                ),
                environment
            );

            return indexes;
        }
Esempio n. 11
0
 internal static DLR.Expression BuildPresetCallbackCall(AplusScope scope, DLR.ParameterExpression valueParam)
 {
     DLR.ParameterExpression callbackParameter = DLR.Expression.Parameter(typeof(CallbackItem), "__CALLBACK__");
     DLR.Expression callback = DLR.Expression.Block(
         new DLR.ParameterExpression[] { callbackParameter },
         DLR.Expression.Condition(
             DLR.Expression.Call(
                 scope.GetRuntimeExpression().Property("CallbackManager"),
                 typeof(CallbackManager).GetMethod("TryGetPresetCallback"),
                 scope.CallbackInfo.QualifiedName,
                 callbackParameter
             ),
             DLR.Expression.Dynamic(
         // TODO: do not instantiate the binder here
                 new Runtime.Binder.CallbackBinder(),
                 typeof(object),
                 callbackParameter,
                 scope.GetRuntimeExpression(),
                 valueParam,
                 scope.CallbackInfo.Index,
                 scope.CallbackInfo.Path
             ).To<AType>(),
             (DLR.Expression)valueParam.To<AType>()
         )
     );
     return callback;
 }
Esempio n. 12
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression result;

            if (scope.IsAssignment && TokenUtils.AllowedPrimitiveFunction(this.token.Type))
            {
                /*
                 * input: y -> left side, x -> right side, value
                 * Perform the function like this:
                 * 
                 * i := f{y; iota rho x}
                 * (,x)[i] := value
                 * 
                 * where 'f' is the dyadic function
                 */
                DLR.Expression left = this.leftExpression.Generate(scope);
                DLR.Expression right = Node.TestMonadicToken(this.rightExpression, Tokens.RAVEL)
                    ? ((MonadicFunction)this.rightExpression).Expression.Generate(scope)
                    : this.rightExpression.Generate(scope)
                ;

                // i:=(iota rho x)
                DLR.Expression indices = AST.Assign.BuildIndicesList(scope, right);
                // (,x)[f{a;i}]
                result = AST.Assign.BuildIndexing(scope, right, GenerateDyadic(scope, indices, left));
            }
            else if (scope.IsAssignment && this.token.Type == Tokens.CHOOSE)
            {
                scope.IsAssignment = false;
                DLR.Expression left = this.leftExpression.Generate(scope);

                scope.IsAssignment = true;
                DLR.Expression right = this.rightExpression.Generate(scope);

                result =
                    DLR.Expression.Block(
                        DLR.Expression.Assign(scope.CallbackInfo.Index, left),
                        DLR.Expression.Call(
                            DLR.Expression.Constant(DyadicFunctionInstance.Choose),
                            DyadicFunctionInstance.Choose.GetType().GetMethod("Assign"),
                            right, left, scope.GetRuntimeExpression()
                        )
                    );
            }
            else if (scope.IsAssignment && this.token.Type == Tokens.PICK)
            {
                scope.IsAssignment = false;
                DLR.Expression left = this.leftExpression.Generate(scope);

                scope.IsAssignment = true;
                DLR.Expression right = this.rightExpression.Generate(scope);

                result =
                    DLR.Expression.Block(
                        DLR.Expression.Assign(scope.CallbackInfo.Path, left),
                        DLR.Expression.Call(
                            DLR.Expression.Constant(DyadicFunctionInstance.Pick),
                            DyadicFunctionInstance.Pick.GetType().GetMethod("Execute"),
                            right, left, scope.GetRuntimeExpression()
                        )
                    );
            }
            else
            {
                DLR.Expression left = this.leftExpression.Generate(scope);
                DLR.Expression right = this.rightExpression.Generate(scope);
                result = GenerateDyadic(scope, right, left);
            }

            return result;
        }
Esempio n. 13
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            if (scope.IsEval && scope.IsMethod)
            {
                // we are inside a function and an eval block.

                // override the original eval scope
                // create a top level scope
                scope = new AplusScope(null, "_EVAL_FUNC_SCOPE_",
                                       scope.GetRuntime(),
                                       scope.GetRuntimeExpression(),
                                       scope.Parent.GetModuleExpression(),
                                       scope.ReturnTarget,
                                       isEval: true
                                       );
            }

            string operatorName = this.name.BuildQualifiedName(runtime.CurrentContext);

            string     scopename   = String.Format("__operator_{0}_scope__", this.name.Name);
            AplusScope methodScope = new AplusScope(scope, scopename,
                                                    runtimeParam: DLR.Expression.Parameter(typeof(Aplus), "_EXTERNAL_RUNTIME_"),
                                                    moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), scopename),
                                                    returnTarget: DLR.Expression.Label(typeof(AType), "RETURN"),
                                                    isMethod: true
                                                    );

            // create a result parameter
            DLR.ParameterExpression resultParameter = DLR.Expression.Parameter(typeof(AType), "__RESULT__");

            // create function's parameters
            LinkedList <DLR.ParameterExpression> methodParameters = new LinkedList <DLR.ParameterExpression>();

            // add parameters to the linkedlist
            BuildParameterExpression(methodScope, methodParameters, leftArgument);
            BuildParameterExpression(methodScope, methodParameters, function);
            BuildParameterExpression(methodScope, methodParameters, condition);
            BuildParameterExpression(methodScope, methodParameters, rightArgument);

            // add parameter for AplusEnviroment
            methodParameters.AddFirst(methodScope.RuntimeExpression);

            // create the lambda method for the function
            DLR.LambdaExpression method = DLR.Expression.Lambda(
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { methodScope.ModuleExpression, resultParameter },
                    // add the local scope's store
                    DLR.Expression.Assign(methodScope.ModuleExpression, DLR.Expression.Constant(new DYN.ExpandoObject())),
                    // set AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        methodScope.ModuleExpression
                        ),
                    // calculate the result of the defined function
                    DLR.Expression.Assign(
                        resultParameter,
                        DLR.Expression.Label(methodScope.ReturnTarget, this.codeblock.Generate(methodScope))
                        ),
                    // reset  AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        DLR.Expression.Constant(null, typeof(DYN.ExpandoObject))
                        ),
                    // return the result
                    resultParameter
                    ),
                operatorName,
                methodParameters
                );

            // wrap the lambda method inside an AFunc
            DLR.Expression wrappedLambda = DLR.Expression.Call(
                typeof(AFunc).GetMethod("CreateUserOperator"),
                DLR.Expression.Constant(operatorName),
                DLR.Expression.Constant(this.IsDyadicOperator),
                method,
                DLR.Expression.Constant(methodParameters.Count),
                DLR.Expression.Constant(this.codeText)
                );

            // set the variable to the lambda function
            DLR.Expression setMethod = VariableHelper.SetVariable(
                runtime,
                scope.ModuleExpression,
                this.name.CreateContextNames(runtime.CurrentContext),
                wrappedLambda
                );

            // ensure the result type to be an AType
            DLR.Expression result = DLR.Expression.Convert(setMethod, typeof(AType));

            return(result);
        }
Esempio n. 14
0
        private DLR.Expression GenerateMonadic(AplusScope scope, DLR.Expression argument)
        {
            DLR.Expression result;
            // Handle the result monadic function a little differently:
            if (this.token.Type == Tokens.RESULT)
            {
                result = scope.ReturnTarget != null
                    // If inside of a function create a return expression Tree
                    ? DLR.Expression.Return(scope.ReturnTarget, argument, typeof(AType))
                    // Otherwise just return the value
                    : argument
                ;
            }
            else
            {
                AbstractMonadicFunction method = MethodChooser.GetMonadicMethod(this.token);

                if (method == null)
                {
                    throw new ParseException(String.Format("Not supported Monadic function[{0}]", this.token));
                }

                result = DLR.Expression.Call(
                    DLR.Expression.Constant(method),
                    method.GetType().GetMethod("Execute"),
                    argument,
                    scope.GetRuntimeExpression()
                );
            }

            return result;
        }
Esempio n. 15
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression func, result;

            if (this.function is Token)
            {
                Node wrappedFunction = new BuiltInFunction((Token)this.function);
                func = wrappedFunction.Generate(scope);
            }
            else
            {
                func = this.function.Generate(scope);
            }

            DLR.ParameterExpression environment = scope.GetRuntimeExpression();

            DLR.ParameterExpression functionParam = DLR.Expression.Variable(typeof(AType), "$$functionParam");
            DLR.ParameterExpression rightParam    = DLR.Expression.Variable(typeof(AType), "$$rightParam");
            DLR.ParameterExpression valueParam    = DLR.Expression.Variable(typeof(AType), "$$valueParam");

            // TODO: rewrite this
            if (this.IsGeneralApply)
            {
                ExpressionList argumnets = (ExpressionList)this.rightarg;
                LinkedList <DLR.Expression> callArguments = new LinkedList <DLR.Expression>();

                // 2. Add the parameters in !reverse! order
                foreach (Node item in argumnets.Items)
                {
                    callArguments.AddFirst(item.Generate(scope));
                }

                // 0. Add A+ environment as first argument for user defined functions
                callArguments.AddFirst(environment);

                // 1. Construct the method body
                callArguments.AddFirst(functionParam.Property("NestedItem"));

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { functionParam, valueParam },
                    DLR.Expression.Assign(functionParam, func),
                    DLR.Expression.IfThenElse(
                        DLR.Expression.PropertyOrField(functionParam, "IsFunctionScalar"),
                        DLR.Expression.Assign(
                            valueParam,
                            AST.UserDefInvoke.BuildInvoke(scope.GetRuntime(), callArguments)
                            ),
                        DLR.Expression.Throw(
                            DLR.Expression.New(
                                typeof(Error.Valence).GetConstructor(new Type[] { typeof(string) }),
                                DLR.Expression.Constant("apply")
                                )
                            )
                        ),
                    valueParam
                    );
            }
            else if (isDyadic)
            {
                DLR.Expression          right     = this.rightarg.Generate(scope);
                DLR.Expression          left      = this.leftarg.Generate(scope);
                DLR.ParameterExpression leftParam = DLR.Expression.Variable(typeof(AType), "$$leftParam");

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { functionParam, rightParam, leftParam, valueParam },
                    DLR.Expression.Assign(functionParam, func),
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.Assign(leftParam, left),
                    DLR.Expression.IfThenElse(
                        DLR.Expression.IsTrue(
                            DLR.Expression.PropertyOrField(functionParam, "IsFunctionScalar")
                            ),
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(DyadicOperatorInstance.Apply),
                                DyadicOperatorInstance.Apply.GetType().GetMethod("Execute"),
                                functionParam, rightParam, leftParam, environment
                                )
                            ),
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(DyadicOperatorInstance.Each),
                                DyadicOperatorInstance.Each.GetType().GetMethod("Execute"),
                                functionParam, rightParam, leftParam, environment
                                )
                            )
                        ),
                    valueParam
                    );
            }
            else
            {
                DLR.Expression right = this.rightarg.Generate(scope);
                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { functionParam, rightParam, valueParam },
                    DLR.Expression.Assign(functionParam, func),
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.IfThenElse(
                        DLR.Expression.IsTrue(
                            DLR.Expression.PropertyOrField(functionParam, "IsFunctionScalar")
                            ),
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(MonadicOperatorInstance.Apply),
                                MonadicOperatorInstance.Apply.GetType().GetMethod("Execute"),
                                functionParam, rightParam, environment
                                )
                            ),
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(MonadicOperatorInstance.Each),
                                MonadicOperatorInstance.Each.GetType().GetMethod("Execute"),
                                functionParam, rightParam, environment
                                )
                            )
                        ),
                    valueParam
                    );
            }

            return(result);
        }
Esempio n. 16
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            // arguments for the dynamic method call
            LinkedList<DLR.Expression> callArguments = new LinkedList<DLR.Expression>();

            if (scope.IsAssignment)
            {
                // Generate the paramters differently for assignment

                // f{...;x} := value  <=> i := f{...; iota rho x}; (,x)[i] := value

                List<Node> items = new List<Node>(this.arguments.Items);
                // 2. Add the parameters in !reverse! order except the last one
                for (int i = 0; i < items.Count - 1; i++)
                {
                    callArguments.AddFirst(items[i].Generate(scope));
                }

                Node lastItem = items[items.Count - 1];
                bool isPick = Node.TestDyadicToken(lastItem, Grammar.Tokens.PICK);
                bool isRavel = Node.TestMonadicToken(lastItem, Grammar.Tokens.RAVEL);

                if (!(lastItem is Identifier || isPick || isRavel))
                {
                    throw new ParseException("user-def invoke assign", false);
                }

                DLR.Expression indexList = AST.Assign.BuildIndicesList(scope, lastItem.Generate(scope));

                callArguments.AddFirst(indexList);
            }
            else
            {
                // 2. Add the parameters in !reverse! order
                foreach (Node item in this.arguments.Items)
                {
                    callArguments.AddFirst(item.Generate(scope));
                }
            }

            // 0. Add A+ environment as first argument for user defined functions
            callArguments.AddFirst(scope.GetRuntimeExpression());

            // 1. Construct the method body
            callArguments.AddFirst(this.method.Generate(scope));

            DLR.Expression result;
            if (scope.IsAssignment)
            {
                // (,x)[f{...;iota rho x}]
                result = AST.Assign.BuildIndexing(
                    scope,
                    this.arguments.Items.Last.Value.Generate(scope),
                    BuildInvoke(runtime, callArguments)
                );
            }
            else
            {
                // 3. Dynamic invoke of method
                // Order of arguments:
                //  (method, Enviroment, argN, ... arg1, arg0)
                result = BuildInvoke(runtime, callArguments);
            }

            return result;
        }
Esempio n. 17
0
        private DLR.Expression BuildGlobalAccessor(
            AplusScope scope, Aplus runtime, DLR.Expression variableContainer, string[] contextParts)
        {
            DLR.ParameterExpression environment       = scope.GetRuntimeExpression();
            DLR.Expression          name              = DLR.Expression.Constant(BuildQualifiedName(runtime.CurrentContext));
            DLR.Expression          dependencyManager = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");
            // Build the ET for getting the dependecy for the variable
            DLR.Expression dependencyInformation =
                DLR.Expression.Call(
                    dependencyManager,
                    typeof(DependencyManager).GetMethod("GetDependency"),
                    name
                    );

            // Build the ET for invoking the dependecy.
            DLR.Expression dependencyEvaulate =
                DLR.Expression.Condition(
                    dependencyInformation.Property("IsItemwise"),
                    AST.UserDefInvoke.BuildInvoke(
                        runtime,
                        new DLR.Expression[]
            {
                dependencyInformation.Property("Function"),
                environment,
                dependencyInformation.Property("InvalidIndex")
            }
                        ),
                    AST.UserDefInvoke.BuildInvoke(
                        runtime,
                        new DLR.Expression[]
            {
                DLR.Expression.Property(dependencyInformation, "Function"),
                environment
            }
                        )
                    );

            /*
             * Simplified code of the resulting ET:
             *
             * result = $runtime.DependecyManager.IsInvalid($$variable)
             *          ? { $$value = $runtime.DependencyManager.GetDependency($$name).Function()
             *              path, index = Utils.ANull();
             *              $$nonPresetvalue = $$value.IsMemoryMappedFile ? $$value : $$value.Clone();
             *              $$qualifiedName = qualifiedname;
             *              $$value = presetCallback($$value);
             *              $$variable = $$value
             *              $$nonPresetvalue
             *          : $$variable
             */

            DLR.ParameterExpression errorParam     = DLR.Expression.Parameter(typeof(Error.Signal));
            DLR.ParameterExpression value          = DLR.Expression.Parameter(typeof(AType));
            DLR.Expression          callback       = AST.Assign.BuildCallbackCall(scope, value);
            DLR.Expression          presetCallback = AST.Assign.BuildPresetCallbackCall(scope, value);

            string qualifiedName = this.BuildQualifiedName(scope.GetRuntime().CurrentContext);

            DLR.Expression temp =
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { value },
                    DLR.Expression.Condition(
                        DLR.Expression.Call(
                            dependencyManager,
                            typeof(DependencyManager).GetMethod("IsInvalid"),
                            name
                            ),
                        DLR.Expression.Block(
                            new DLR.ParameterExpression[] { scope.CallbackInfo.QualifiedName, scope.CallbackInfo.NonPresetValue,
                                                            scope.CallbackInfo.Path, scope.CallbackInfo.Index },
                            DLR.Expression.Assign(value, dependencyEvaulate),
                            DLR.Expression.Assign(scope.CallbackInfo.Path, DLR.Expression.Constant(Utils.ANull())),
                            DLR.Expression.Assign(scope.CallbackInfo.Index, DLR.Expression.Constant(Utils.ANull())),
                            DLR.Expression.Assign(
                                scope.CallbackInfo.NonPresetValue,
                                DLR.Expression.Condition(
                                    DLR.Expression.Property(value, "IsMemoryMappedFile"),
                                    value,
                                    DLR.Expression.Call(
                                        value,
                                        typeof(AType).GetMethod("Clone")
                                        )
                                    )
                                ),
                            DLR.Expression.Assign(
                                scope.CallbackInfo.QualifiedName,
                                DLR.Expression.Constant(qualifiedName)
                                ),
                            DLR.Expression.TryCatch(
                                DLR.Expression.Block(
                                    typeof(void),
                                    DLR.Expression.Assign(value, presetCallback)
                                    ),
                                DLR.Expression.Catch(
                                    errorParam,
                                    DLR.Expression.Empty()
                                    )
                                ),
                            VariableHelper.SetVariable(
                                runtime,
                                variableContainer,
                                contextParts,
                                value
                                ),
                            scope.CallbackInfo.NonPresetValue
                            ),
                        VariableHelper.GetVariable(
                            runtime,
                            variableContainer,
                            contextParts
                            ),
                        typeof(object)
                        )
                    ).ToAType(runtime);

            DLR.Expression result = temp;

            if (scope.IsAssignment)
            {
                result =
                    DLR.Expression.Block(
                        DLR.Expression.Assign(
                            scope.CallbackInfo.QualifiedName,
                            DLR.Expression.Constant(qualifiedName)
                            ),
                        temp
                        );
            }

            return(result);
        }
Esempio n. 18
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            // arguments for the dynamic method call
            LinkedList <DLR.Expression> callArguments = new LinkedList <DLR.Expression>();

            if (scope.IsAssignment)
            {
                // Generate the paramters differently for assignment

                // f{...;x} := value  <=> i := f{...; iota rho x}; (,x)[i] := value

                List <Node> items = new List <Node>(this.arguments.Items);
                // 2. Add the parameters in !reverse! order except the last one
                for (int i = 0; i < items.Count - 1; i++)
                {
                    callArguments.AddFirst(items[i].Generate(scope));
                }

                Node lastItem = items[items.Count - 1];
                bool isPick   = Node.TestDyadicToken(lastItem, Grammar.Tokens.PICK);
                bool isRavel  = Node.TestMonadicToken(lastItem, Grammar.Tokens.RAVEL);

                if (!(lastItem is Identifier || isPick || isRavel))
                {
                    throw new ParseException("user-def invoke assign", false);
                }

                DLR.Expression indexList = AST.Assign.BuildIndicesList(scope, lastItem.Generate(scope));

                callArguments.AddFirst(indexList);
            }
            else
            {
                // 2. Add the parameters in !reverse! order
                foreach (Node item in this.arguments.Items)
                {
                    callArguments.AddFirst(item.Generate(scope));
                }
            }

            // 0. Add A+ environment as first argument for user defined functions
            callArguments.AddFirst(scope.GetRuntimeExpression());

            // 1. Construct the method body
            callArguments.AddFirst(this.method.Generate(scope));

            DLR.Expression result;
            if (scope.IsAssignment)
            {
                // (,x)[f{...;iota rho x}]
                result = AST.Assign.BuildIndexing(
                    scope,
                    this.arguments.Items.Last.Value.Generate(scope),
                    BuildInvoke(runtime, callArguments)
                    );
            }
            else
            {
                // 3. Dynamic invoke of method
                // Order of arguments:
                //  (method, Enviroment, argN, ... arg1, arg0)
                result = BuildInvoke(runtime, callArguments);
            }

            return(result);
        }
Esempio n. 19
0
        internal static DLR.Expression <Func <Aplus, AType> > BuildExecuteMethod(
            string sourceCode, Aplus environment
            )
        {
            DLR.Expression codebody;
            // TODO: fix the third function info argument
            AplusCore.Compiler.AST.Node tree = Compiler.Parse.String(sourceCode, environment.LexerMode, null);

            AplusScope scope = new AplusScope(null, "__EVAL__", environment,
                                              DLR.Expression.Parameter(typeof(Aplus), "__EVAL_RUNTIME__"),
                                              DLR.Expression.Parameter(typeof(DYN.IDynamicMetaObjectProvider), "__EVAL_MODULE__"),
                                              //DLR.Expression.Parameter(typeof(Aplus), "__EVAL_ENVIRONMENT__"),
                                              DLR.Expression.Label(typeof(AType), "__EVAL_EXIT__"),
                                              isEval: true
                                              );

            if (tree == null)
            {
                codebody = DLR.Expression.Constant(null);
            }
            else if (environment.FunctionScope != null)
            {
                AplusScope functionScope = new AplusScope(scope, "__EVAL_IN_FUNCTION__",
                                                          moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), "__EVAL_FUNCTION_SCOPE__"),
                                                          returnTarget: scope.ReturnTarget,
                                                          isMethod: true
                                                          );

                codebody = DLR.Expression.Block(
                    new DLR.ParameterExpression[] {
                    //scope.RuntimeExpression,         // runtime
                    scope.ModuleExpression,              // root context
                    functionScope.ModuleExpression       // Function local scope
                },
                    //DLR.Expression.Assign(
                    //    scope.RuntimeExpression, scope.RuntimeExpression
                    //),
                    DLR.Expression.Assign(
                        scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context")
                        ),
                    DLR.Expression.Assign(
                        functionScope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "FunctionScope")
                        ),
                    DLR.Expression.Label(
                        scope.ReturnTarget,
                        tree.Generate(functionScope)
                        )
                    );
            }
            else
            {
                codebody = DLR.Expression.Block(
                    new DLR.ParameterExpression[] {
                    //scope.RuntimeExpression,         // runtime
                    scope.ModuleExpression              // root context
                },
                    //DLR.Expression.Assign(
                    //   scope.RuntimeExpression, scope.RuntimeExpression
                    //),
                    DLR.Expression.Assign(
                        scope.ModuleExpression, DLR.Expression.PropertyOrField(scope.RuntimeExpression, "Context")
                        ),
                    DLR.Expression.Label(
                        scope.ReturnTarget,
                        tree.Generate(scope)
                        )
                    );
            }

            DLR.Expression <Func <Aplus, AType> > lambda = DLR.Expression.Lambda <Func <Aplus, AType> >(
                codebody,
                scope.GetRuntimeExpression()
                );

            return(lambda);
        }
Esempio n. 20
0
        private DLR.Expression GenerateDyadic(AplusScope scope, DLR.Expression right, DLR.Expression left)
        {
            DLR.Expression result;

            DLR.ParameterExpression environment = scope.GetRuntimeExpression();

            if (this.token.Type == Tokens.OR)
            {
                DLR.ParameterExpression leftParam = DLR.Expression.Variable(typeof(AType), "$$leftParam");
                DLR.ParameterExpression rightParam = DLR.Expression.Variable(typeof(AType), "$$rightParam");
                DLR.ParameterExpression valueParam = DLR.Expression.Variable(typeof(AType), "$$valueParam");

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { leftParam, rightParam, valueParam },
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.Assign(leftParam, left),
                    DLR.Expression.IfThenElse(
                    // $left.IsNumber || ($left.Type == ATypes.ANull)
                        DLR.Expression.OrElse(
                            DLR.Expression.IsTrue(
                                DLR.Expression.PropertyOrField(leftParam, "IsNumber")
                            ),
                            DLR.Expression.Equal(
                                DLR.Expression.PropertyOrField(leftParam, "Type"),
                                DLR.Expression.Constant(ATypes.ANull)
                            )
                        ),
                    // Or($right, $left)
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(DyadicFunctionInstance.Or),
                                DyadicFunctionInstance.Or.GetType().GetMethod("Execute"),
                                rightParam, leftParam, environment
                            )
                        ),
                    // Cast($right, $left)
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(DyadicFunctionInstance.Cast),
                                DyadicFunctionInstance.Cast.GetType().GetMethod("Execute"),
                                rightParam, leftParam, environment
                            )
                        )
                    ),
                    valueParam
                 );
            }
            else if (this.token.Type == Tokens.BWOR)
            {
                DLR.ParameterExpression rightParam = DLR.Expression.Variable(typeof(AType), "$$rightParam");
                DLR.ParameterExpression leftParam = DLR.Expression.Variable(typeof(AType), "$$leftParam");

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { leftParam, rightParam },
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.Assign(leftParam, left),
                    DLR.Expression.Condition(
                    // $left.Type == ATypes.ASymbol
                        DLR.Expression.Equal(
                            leftParam.Property("Type"),
                            DLR.Expression.Constant(ATypes.ASymbol)
                        ),
                        DLR.Expression.Constant(DyadicFunctionInstance.BitwiseCast).Call<BitwiseCast>(
                            "Execute", rightParam, leftParam, environment
                        ),
                        DLR.Expression.Constant(DyadicFunctionInstance.BitwiseOr).Call<BitwiseOr>(
                            "Execute", rightParam, leftParam, environment
                        )
                    )
                );
            }
            else
            {
                AbstractDyadicFunction method = MethodChooser.GetDyadicMethod(this.token);

                if (method == null)
                {
                    throw new ParseException(String.Format("Not supported Dyadic function[{0}]", this.token));
                }

                result = DLR.Expression.Call(
                    DLR.Expression.Constant(method),
                    method.GetType().GetMethod("Execute"),
                    right, left, environment
                );
            }

            return result;
        }
Esempio n. 21
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            if (scope.IsEval && scope.IsMethod)
            {
                // we are inside a function and an eval block.

                // override the original eval scope
                // create a top level scope
                scope = new AplusScope(null, "_EVAL_FUNC_SCOPE_",
                    scope.GetRuntime(),
                    scope.GetRuntimeExpression(),
                    scope.Parent.GetModuleExpression(),
                    scope.ReturnTarget,
                    isEval: true
                );
            }

            string methodName = this.name.BuildQualifiedName(runtime.CurrentContext);

            // 1. Create a new scope for the function
            string scopename = String.Format("__method_{0}_scope__", this.name.Name);
            AplusScope methodScope = new AplusScope(scope, scopename,
                runtimeParam: DLR.Expression.Parameter(typeof(Aplus), "_EXTERNAL_RUNTIME_"),
                moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), scopename),
                returnTarget: DLR.Expression.Label(typeof(AType), "RETURN"),
                isMethod: true
            );

            // 1.5 Create a result parameter
            DLR.ParameterExpression resultParameter = DLR.Expression.Parameter(typeof(AType), "__RESULT__");

            // 2. Create function's parameters
            LinkedList<DLR.ParameterExpression> methodParameters = new LinkedList<DLR.ParameterExpression>();

            foreach (Node parameter in this.parameters.Items)
            {
                string parameterName = ((Identifier)parameter).Name;
                DLR.ParameterExpression parameterExpression = DLR.Expression.Parameter(typeof(AType), parameterName);

                // Add parameter to the scope's variable list
                methodScope.Variables[parameterName] = parameterExpression;

                // Add parameters in !reverse! order
                methodParameters.AddFirst(parameterExpression);
            }

            // Add parameter for AplusEnviroment
            methodParameters.AddFirst(methodScope.RuntimeExpression);

            // Create a return label for exiting from the function
            //methodScope.ReturnTarget = ;

            // 3. Create the lambda method for the function
            DLR.LambdaExpression method = DLR.Expression.Lambda(
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { methodScope.ModuleExpression, resultParameter },
                // Add the local scope's store
                    DLR.Expression.Assign(methodScope.ModuleExpression, DLR.Expression.Constant(new DYN.ExpandoObject())),
                // set AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        methodScope.ModuleExpression
                    ),
                // Calculate the result of the defined function
                    DLR.Expression.Assign(
                        resultParameter,
                        DLR.Expression.Label(methodScope.ReturnTarget, this.codeblock.Generate(methodScope))
                    ),
                // reset  AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        DLR.Expression.Constant(null, typeof(DYN.ExpandoObject))
                    ),
                // Return the result
                    resultParameter
                ),
                methodName,
                methodParameters
            );

            // 3.5. Wrap the lambda method inside an AFunc
            DLR.Expression wrappedLambda = DLR.Expression.Call(
                typeof(AFunc).GetMethod("Create", new Type[] { typeof(string), typeof(object), typeof(int), typeof(string) }),
                DLR.Expression.Constant(methodName),
                method,
                DLR.Expression.Constant(methodParameters.Count),
                DLR.Expression.Constant(this.code)
            );

            // 4. Set the variable to the lambda function
            DLR.Expression setMethod = VariableHelper.SetVariable(
                runtime,
                scope.ModuleExpression,
                this.name.CreateContextNames(runtime.CurrentContext),
                wrappedLambda
            );

            // ensure the result type to be an AType
            DLR.Expression result = DLR.Expression.Convert(setMethod, typeof(AType));

            return result;
        }
Esempio n. 22
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            Aplus runtime = scope.GetRuntime();

            if (scope.IsEval && scope.IsMethod)
            {
                // we are inside a function and an eval block.

                // override the original eval scope
                // create a top level scope
                scope = new AplusScope(null, "_EVAL_FUNC_SCOPE_",
                                       scope.GetRuntime(),
                                       scope.GetRuntimeExpression(),
                                       scope.Parent.GetModuleExpression(),
                                       scope.ReturnTarget,
                                       isEval: true
                                       );
            }

            string methodName = this.name.BuildQualifiedName(runtime.CurrentContext);

            // 1. Create a new scope for the function
            string     scopename   = String.Format("__method_{0}_scope__", this.name.Name);
            AplusScope methodScope = new AplusScope(scope, scopename,
                                                    runtimeParam: DLR.Expression.Parameter(typeof(Aplus), "_EXTERNAL_RUNTIME_"),
                                                    moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), scopename),
                                                    returnTarget: DLR.Expression.Label(typeof(AType), "RETURN"),
                                                    isMethod: true
                                                    );

            // 1.5 Create a result parameter
            DLR.ParameterExpression resultParameter = DLR.Expression.Parameter(typeof(AType), "__RESULT__");

            // 2. Create function's parameters
            LinkedList <DLR.ParameterExpression> methodParameters = new LinkedList <DLR.ParameterExpression>();

            foreach (Node parameter in this.parameters.Items)
            {
                string parameterName = ((Identifier)parameter).Name;
                DLR.ParameterExpression parameterExpression = DLR.Expression.Parameter(typeof(AType), parameterName);

                // Add parameter to the scope's variable list
                methodScope.Variables[parameterName] = parameterExpression;

                // Add parameters in !reverse! order
                methodParameters.AddFirst(parameterExpression);
            }

            // Add parameter for AplusEnviroment
            methodParameters.AddFirst(methodScope.RuntimeExpression);

            // Create a return label for exiting from the function
            //methodScope.ReturnTarget = ;

            // 3. Create the lambda method for the function
            DLR.LambdaExpression method = DLR.Expression.Lambda(
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { methodScope.ModuleExpression, resultParameter },
                    // Add the local scope's store
                    DLR.Expression.Assign(methodScope.ModuleExpression, DLR.Expression.Constant(new DYN.ExpandoObject())),
                    // set AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        methodScope.ModuleExpression
                        ),
                    // Calculate the result of the defined function
                    DLR.Expression.Assign(
                        resultParameter,
                        DLR.Expression.Label(methodScope.ReturnTarget, this.codeblock.Generate(methodScope))
                        ),
                    // reset  AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(methodScope.RuntimeExpression, "FunctionScope"),
                        DLR.Expression.Constant(null, typeof(DYN.ExpandoObject))
                        ),
                    // Return the result
                    resultParameter
                    ),
                methodName,
                methodParameters
                );

            // 3.5. Wrap the lambda method inside an AFunc
            DLR.Expression wrappedLambda = DLR.Expression.Call(
                typeof(AFunc).GetMethod("Create", new Type[] { typeof(string), typeof(object), typeof(int), typeof(string) }),
                DLR.Expression.Constant(methodName),
                method,
                DLR.Expression.Constant(methodParameters.Count),
                DLR.Expression.Constant(this.code)
                );

            // 4. Set the variable to the lambda function
            DLR.Expression setMethod = VariableHelper.SetVariable(
                runtime,
                scope.ModuleExpression,
                this.name.CreateContextNames(runtime.CurrentContext),
                wrappedLambda
                );

            // ensure the result type to be an AType
            DLR.Expression result = DLR.Expression.Convert(setMethod, typeof(AType));

            return(result);
        }
Esempio n. 23
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.ParameterExpression value = DLR.Expression.Parameter(typeof(AType), "__VALUE__");
            DLR.ParameterExpression targetParameter = DLR.Expression.Parameter(typeof(AType), "__TARGET__");
            DLR.ParameterExpression assignDone = DLR.Expression.Parameter(typeof(bool), "__ASSIGNDONE__");
            DLR.ParameterExpression prevAssignDone = scope.AssignDone;

            scope.AssignDone = assignDone;
            CallbackInfoStorage savedCallbackInfos = scope.CallbackInfo;

            CallbackInfoStorage callbackInfos =
                new CallbackInfoStorage()
                {
                    Index = DLR.Expression.Parameter(typeof(AType), "__CALLBACKINDEX__"),
                    QualifiedName = DLR.Expression.Parameter(typeof(string), "__QUALIFIEDNAME__"),
                    Path = DLR.Expression.Parameter(typeof(AType), "__PATH__"),
                    NonPresetValue = DLR.Expression.Parameter(typeof(AType), "__NONPRESETVALUE__")
                };

            scope.CallbackInfo = callbackInfos;

            // Clone the rhs value of the assignment to ensure correct results
            // in case of: a:=b:=[...:=] [rhs]  assignments
            DLR.ParameterExpression environment = scope.GetRuntimeExpression();
            DLR.Expression result = null;

            if (this.target is Identifier)
            {
                result = GenerateIdentifierAssign(scope, (AST.Identifier)this.target, value);
            }
            else if (this.target is Strand)
            {
                result = GenerateStrandAssign(scope, (Strand)this.target, value);
            }
            else if (this.target is Indexing)
            {
                Indexing target = (Indexing)this.target;

                // in case of:  a[,] := ...
                if (target.IndexExpression != null && target.IndexExpression[0] is BuiltInFunction)
                {
                    BuiltInFunction function = (BuiltInFunction)target.IndexExpression[0];
                    if (function.Function.Type != Grammar.Tokens.RAVEL)
                    {
                        // incorrect function inside the index expression
                        throw new ParseException("Incorrect function", false);
                    }

                    var flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static;

                    scope.IsAssignment = true;
                    DLR.Expression item = target.Item.Generate(scope);
                    scope.IsAssignment = false;

                    DLR.ParameterExpression param = DLR.Expression.Parameter(typeof(AType));
                    DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal));
                    DLR.Expression callback = BuildCallbackCall(scope, value);
                    DLR.Expression presetCallback = BuildPresetCallbackCall(scope, value);

                    result =
                        DLR.Expression.Block(
                            new DLR.ParameterExpression[] { param },
                            DLR.Expression.Assign(param, item),
                            DLR.Expression.Call(
                                typeof(Assign).GetMethod("CalculateIndexes", flags),
                                value,
                                param,
                                environment,
                                scope.CallbackInfo.Index
                            ),
                            DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                            DLR.Expression.TryCatch(
                                DLR.Expression.Block(
                                    typeof(void),
                                    DLR.Expression.Assign(value, presetCallback),
                                    DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                                ),
                                DLR.Expression.Catch(
                                    errorParam,
                                    DLR.Expression.Empty()
                                )
                            ),
                            DLR.Expression.IfThen(
                                scope.AssignDone,
                                DLR.Expression.Block(
                                    DLR.Expression.Call(
                                        typeof(Assign).GetMethod("AppendItem", flags),
                                        value,
                                        param,
                                        environment
                                    ),
                                    callback
                                )
                            ),
                            scope.CallbackInfo.NonPresetValue
                        );
                }
                else
                {
                    result = GenerateIndexAssign(scope, target, value);
                }
            }
            else if (Node.TestMonadicToken(this.target, Grammar.Tokens.VALUE))
            {
                var method = typeof(Value).GetMethod("Assign");
                var targetDLR = ((MonadicFunction)this.target).Expression.Generate(scope);

                DLR.Expression callback = BuildCallbackCall(scope, value);
                DLR.Expression presetCallback = BuildPresetCallbackCall(scope, value);

                DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal));
                DLR.ParameterExpression target = DLR.Expression.Parameter(typeof(AType), "__VALUETARGET__");

                MethodInfo genericMethod =
                    typeof(Value).GetMethod("CheckArgument", BindingFlags.Static | BindingFlags.NonPublic);

                DLR.Expression nameMaker =
                        DLR.Expression.Block(
                            DLR.Expression.Call(
                                genericMethod.MakeGenericMethod(typeof(Value)),
                                target
                            ),
                            DLR.Expression.Call(
                                VariableHelper.BuildValueQualifiedNameMethod,
                                scope.RuntimeExpression.Property("CurrentContext"),
                                target.Property("asString")
                            )
                        );

                result =
                    DLR.Expression.Block(
                        new DLR.ParameterExpression[] { target },
                        DLR.Expression.Assign(target, targetDLR),
                        DLR.Expression.Assign(
                            scope.CallbackInfo.QualifiedName,
                            nameMaker
                        ),
                        DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                        DLR.Expression.TryCatch(
                            DLR.Expression.Block(
                                typeof(void),
                                DLR.Expression.Assign(value, presetCallback),
                                DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                            ),
                            DLR.Expression.Catch(
                                errorParam,
                                DLR.Expression.Empty()
                            )
                        ),
                        DLR.Expression.IfThen(
                            scope.AssignDone,
                            DLR.Expression.Block(
                                DLR.Expression.Call(
                                    method,
                                    target,
                                    value,
                                    environment
                                ),
                                callback
                            )
                        ),
                        scope.CallbackInfo.NonPresetValue
                    );
            }
            else if (Node.TestDyadicToken(this.target, Grammar.Tokens.VALUEINCONTEXT))
            {
                var targetNode = (DyadicFunction)this.target;
                var method = typeof(ValueInContext).GetMethod("Assign");
                var targetDLR = targetNode.Right.Generate(scope);
                var contextNameDLR = targetNode.Left.Generate(scope);

                DLR.Expression callback = BuildCallbackCall(scope, value);
                DLR.Expression presetCallback = BuildPresetCallbackCall(scope, value);

                DLR.ParameterExpression target = DLR.Expression.Parameter(typeof(AType), "__VALUETARGET__");
                DLR.ParameterExpression contextName = DLR.Expression.Parameter(typeof(AType), "__CONTEXTNAME__");
                DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal));

                MethodInfo genericMethod =
                    typeof(Value).GetMethod("CheckArgument", BindingFlags.Static | BindingFlags.NonPublic);
                MethodInfo chosenMethod = genericMethod.MakeGenericMethod(typeof(ValueInContext));

                DLR.Expression nameMaker =
                        DLR.Expression.Block(
                            DLR.Expression.Call(chosenMethod, contextName),
                            DLR.Expression.Call(chosenMethod, target),
                            DLR.Expression.Call(
                                VariableHelper.BuildValueQualifiedNameMethod,
                                contextName.Property("asString"),
                                target.Property("asString")
                            )
                        );

                result =
                    DLR.Expression.Block(
                        new DLR.ParameterExpression[] { target, contextName },
                        DLR.Expression.Assign(target, targetDLR),
                        DLR.Expression.Assign(contextName, contextNameDLR),
                        DLR.Expression.Assign(
                            scope.CallbackInfo.QualifiedName,
                            nameMaker
                        ),
                        DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                        DLR.Expression.TryCatch(
                            DLR.Expression.Block(
                                typeof(void),
                                DLR.Expression.Assign(value, presetCallback),
                                DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                            ),
                            DLR.Expression.Catch(
                                errorParam,
                                DLR.Expression.Empty()
                            )
                        ),
                        DLR.Expression.IfThen(
                            scope.AssignDone,
                            DLR.Expression.Block(
                                DLR.Expression.Call(
                                    method,
                                    target,
                                    contextName,
                                    value,
                                    environment
                                ),
                                callback
                            )
                        ),
                        scope.CallbackInfo.NonPresetValue
                    );
            }
            else if (Node.TestDyadicToken(this.target, Grammar.Tokens.PICK))
            {
                result = BuildPickAssign(scope, value);
            }
            else
            {
                var flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static;
                // switch on Assignment flag
                scope.IsAssignment = true;

                // Find the correct method for assignment
                // for pick we need a different method currently..
                var method = typeof(Assign).GetMethod("AssignHelper", flags);

                DLR.Expression callback = AST.Assign.BuildCallbackCall(scope, value);
                DLR.Expression presetCallback = BuildPresetCallbackCall(scope, value);

                DLR.ParameterExpression temp = DLR.Expression.Parameter(typeof(AType));
                DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal));

                result =
                    DLR.Expression.Block(
                        new DLR.ParameterExpression[] { temp },
                        DLR.Expression.Assign(temp, this.target.Generate(scope)),
                        DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                        DLR.Expression.TryCatch(
                            DLR.Expression.Block(
                                typeof(void),
                                DLR.Expression.Assign(value, presetCallback),
                                DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                            ),
                            DLR.Expression.Catch(
                                errorParam,
                                DLR.Expression.Empty()
                            )
                        ),
                        DLR.Expression.IfThen(
                            scope.AssignDone,
                            DLR.Expression.Block(
                                DLR.Expression.Call(
                                    method,
                                    value,
                                    temp
                                ),
                                callback
                            )
                        )
                    );

                // switch off Assignment flag
                scope.IsAssignment = false;
            }

            // error parameter
            DLR.ParameterExpression errorVariable = DLR.Expression.Parameter(typeof(Error.Signal), "error");

            /**
             * The dark magic behind callbacks:
             * $value := the value of the right side of assignment
             * $target := the left side of the assignment
             * $qualifiedName := the name of the assigned variable
             * $index := the index of the assigned variable (if any)
             * $path := the path of the assigned variable (if any)
             * 
             * example for preset callback assignment
             * a := x := y
             * if we have a preset callback on x, then the x will get the value of the preset callback on x,
             * a will be y (or the value of the preset callback on a)
             * in any case we will result y
             */

            DLR.Expression blockedResult =
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { value, scope.AssignDone,
                        scope.CallbackInfo.QualifiedName, scope.CallbackInfo.Index,
                        scope.CallbackInfo.Path,scope.CallbackInfo.NonPresetValue },
                    DLR.Expression.Assign(scope.CallbackInfo.QualifiedName, DLR.Expression.Constant("")),
                    DLR.Expression.Assign(scope.CallbackInfo.Index, DLR.Expression.Constant(Utils.ANull())),
                    DLR.Expression.Assign(scope.CallbackInfo.Path, DLR.Expression.Constant(Utils.ANull())),
                    DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true, typeof(bool))),
                    DLR.Expression.Assign(scope.CallbackInfo.NonPresetValue, DLR.Expression.Constant(null, typeof(AType))),
                    DLR.Expression.Assign(value, this.expression.Generate(scope)),
                    DLR.Expression.IfThen(
                        DLR.Expression.Equal(scope.CallbackInfo.NonPresetValue, DLR.Expression.Constant(null, typeof(AType))),
                        DLR.Expression.Assign(scope.CallbackInfo.NonPresetValue, value)
                    ),
                    DLR.Expression.Assign(
                            value,
                            DLR.Expression.Condition(
                                DLR.Expression.Property(value, "IsMemoryMappedFile"),
                                value,
                                DLR.Expression.Call(
                                    value,
                                    typeof(AType).GetMethod("Clone")
                                )
                            )
                        ),
                    result,
                    scope.CallbackInfo.NonPresetValue
                );

            scope.CallbackInfo = savedCallbackInfos;
            scope.AssignDone = prevAssignDone;

            return blockedResult;
        }
Esempio n. 24
0
 internal static DLR.Expression BuildCallbackCall(AplusScope scope, DLR.ParameterExpression valueParam)
 {
     // callback
     /* 
      * CallbackItem callback;
      * if(CallbackManager.TryGetCallback(globalName, out callback)
      * {
      *     CallbackBinder.Invoke(...);
      * }
      */
     DLR.ParameterExpression callbackParameter = DLR.Expression.Parameter(typeof(CallbackItem), "__CALLBACK__");
     DLR.Expression callback = DLR.Expression.Block(
         new DLR.ParameterExpression[] { callbackParameter },
         DLR.Expression.IfThen(
             DLR.Expression.Call(
                 scope.GetRuntimeExpression().Property("CallbackManager"),
                 typeof(CallbackManager).GetMethod("TryGetCallback"),
                 scope.CallbackInfo.QualifiedName,
                 callbackParameter
             ),
             DLR.Expression.Dynamic(
         // TODO: do not instantiate the binder here
                 new Runtime.Binder.CallbackBinder(),
                 typeof(object),
                 callbackParameter,
                 scope.GetRuntimeExpression(),
                 valueParam,
                 scope.CallbackInfo.Index,
                 scope.CallbackInfo.Path
             )
         )
     );
     return callback;
 }
Esempio n. 25
0
        private DLR.Expression BuildGlobalAccessor(
            AplusScope scope, Aplus runtime, DLR.Expression variableContainer, string[] contextParts)
        {
            DLR.ParameterExpression environment = scope.GetRuntimeExpression();
            DLR.Expression name = DLR.Expression.Constant(BuildQualifiedName(runtime.CurrentContext));
            DLR.Expression dependencyManager = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");
            // Build the ET for getting the dependecy for the variable
            DLR.Expression dependencyInformation =
                DLR.Expression.Call(
                    dependencyManager,
                    typeof(DependencyManager).GetMethod("GetDependency"),
                    name
                );

            // Build the ET for invoking the dependecy.
            DLR.Expression dependencyEvaulate =
                DLR.Expression.Condition(
                    dependencyInformation.Property("IsItemwise"),
                    AST.UserDefInvoke.BuildInvoke(
                        runtime,
                        new DLR.Expression[]
                        {
                            dependencyInformation.Property("Function"),
                            environment,
                            dependencyInformation.Property("InvalidIndex")
                        }
                    ),
                    AST.UserDefInvoke.BuildInvoke(
                        runtime,
                        new DLR.Expression[] 
                        {
                            DLR.Expression.Property(dependencyInformation, "Function"),
                            environment
                        }
                    )
                );

            /* 
             * Simplified code of the resulting ET:
             * 
             * result = $runtime.DependecyManager.IsInvalid($$variable)
             *          ? { $$value = $runtime.DependencyManager.GetDependency($$name).Function()
             *              path, index = Utils.ANull();
             *              $$nonPresetvalue = $$value.IsMemoryMappedFile ? $$value : $$value.Clone();
             *              $$qualifiedName = qualifiedname;
             *              $$value = presetCallback($$value);
             *              $$variable = $$value
             *              $$nonPresetvalue
             *          : $$variable
             */

            DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal));
            DLR.ParameterExpression value = DLR.Expression.Parameter(typeof(AType));
            DLR.Expression callback = AST.Assign.BuildCallbackCall(scope, value);
            DLR.Expression presetCallback = AST.Assign.BuildPresetCallbackCall(scope, value);

            string qualifiedName = this.BuildQualifiedName(scope.GetRuntime().CurrentContext);

            DLR.Expression temp =
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { value },
                    DLR.Expression.Condition(
                        DLR.Expression.Call(
                            dependencyManager,
                            typeof(DependencyManager).GetMethod("IsInvalid"),
                            name
                        ),
                        DLR.Expression.Block(
                            new DLR.ParameterExpression[] { scope.CallbackInfo.QualifiedName, scope.CallbackInfo.NonPresetValue,
                                                            scope.CallbackInfo.Path, scope.CallbackInfo.Index },
                            DLR.Expression.Assign(value, dependencyEvaulate),
                            DLR.Expression.Assign(scope.CallbackInfo.Path, DLR.Expression.Constant(Utils.ANull())),
                            DLR.Expression.Assign(scope.CallbackInfo.Index, DLR.Expression.Constant(Utils.ANull())),
                            DLR.Expression.Assign(
                                scope.CallbackInfo.NonPresetValue,
                                DLR.Expression.Condition(
                                    DLR.Expression.Property(value, "IsMemoryMappedFile"),
                                    value,
                                    DLR.Expression.Call(
                                        value,
                                        typeof(AType).GetMethod("Clone")
                                    )
                                )
                            ),
                            DLR.Expression.Assign(
                                scope.CallbackInfo.QualifiedName,
                                DLR.Expression.Constant(qualifiedName)
                            ),
                            DLR.Expression.TryCatch(
                                DLR.Expression.Block(
                                    typeof(void),
                                    DLR.Expression.Assign(value, presetCallback)
                                ),
                                DLR.Expression.Catch(
                                    errorParam,
                                    DLR.Expression.Empty()
                                )
                            ),
                            VariableHelper.SetVariable(
                                runtime,
                                variableContainer,
                                contextParts,
                                value
                            ),
                            scope.CallbackInfo.NonPresetValue
                        ),
                        VariableHelper.GetVariable(
                                runtime,
                                variableContainer,
                                contextParts
                        ),
                        typeof(object)
                    )
                ).ToAType(runtime);

            DLR.Expression result = temp;

            if (scope.IsAssignment)
            {
                result =
                    DLR.Expression.Block(
                        DLR.Expression.Assign(
                            scope.CallbackInfo.QualifiedName,
                            DLR.Expression.Constant(qualifiedName)
                            ),
                        temp
                    );
            }

            return result;
        }
Esempio n. 26
0
        /// <summary>
        /// Build DLR expression for: (,target)[indexExpression]
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="target"></param>
        /// <param name="indexExpression">The indexer expression containing the expression returned from <see cref="BuildIndicesList"/></param>
        /// <returns></returns>
        internal static DLR.Expression BuildIndexing(AplusScope scope, DLR.Expression target, DLR.Expression indexExpression)
        {
            var execute = typeof(AbstractMonadicFunction).GetMethod("Execute");
            DLR.ParameterExpression indexes = DLR.Expression.Parameter(typeof(List<AType>), "_INDEX_");

            // (,x)
            DLR.Expression raveledRight = DLR.Expression.Call(
                DLR.Expression.Constant(MonadicFunctionInstance.Ravel),
                execute,
                target,
                scope.GetRuntimeExpression()
            );

            // (,x)[indexExpression] := ..
            DLR.Expression result =
                DLR.Expression.Convert(
                    DLR.Expression.Block(
                        new DLR.ParameterExpression[] { indexes },
                        DLR.Expression.Assign(
                            indexes,
                            DLR.Expression.Call(
                                typeof(Helpers).GetMethod("BuildIndexerArray"),
                                DLR.Expression.NewArrayInit(typeof(AType), indexExpression)
                            )
                        ),
                        DLR.Expression.Assign(
                            scope.CallbackInfo.Index,
                            DLR.Expression.Call(
                                typeof(Tools).GetMethod("ConvertATypeListToAType", BindingFlags.NonPublic | BindingFlags.Static),
                                indexes
                            )
                        ),
                        DLR.Expression.Dynamic(
                            scope.GetRuntime().GetIndexBinder(new DYN.CallInfo(1)),
                            typeof(object),
                            raveledRight,
                            indexes
                        )
                    ),
                    typeof(AType)
                );

            return result;
        }
Esempio n. 27
0
        private DLR.Expression GenerateDyadic(AplusScope scope, DLR.Expression right, DLR.Expression left)
        {
            DLR.Expression result;

            DLR.ParameterExpression environment = scope.GetRuntimeExpression();

            if (this.token.Type == Tokens.OR)
            {
                DLR.ParameterExpression leftParam  = DLR.Expression.Variable(typeof(AType), "$$leftParam");
                DLR.ParameterExpression rightParam = DLR.Expression.Variable(typeof(AType), "$$rightParam");
                DLR.ParameterExpression valueParam = DLR.Expression.Variable(typeof(AType), "$$valueParam");

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { leftParam, rightParam, valueParam },
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.Assign(leftParam, left),
                    DLR.Expression.IfThenElse(
                        // $left.IsNumber || ($left.Type == ATypes.ANull)
                        DLR.Expression.OrElse(
                            DLR.Expression.IsTrue(
                                DLR.Expression.PropertyOrField(leftParam, "IsNumber")
                                ),
                            DLR.Expression.Equal(
                                DLR.Expression.PropertyOrField(leftParam, "Type"),
                                DLR.Expression.Constant(ATypes.ANull)
                                )
                            ),
                        // Or($right, $left)
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(DyadicFunctionInstance.Or),
                                DyadicFunctionInstance.Or.GetType().GetMethod("Execute"),
                                rightParam, leftParam, environment
                                )
                            ),
                        // Cast($right, $left)
                        DLR.Expression.Assign(
                            valueParam,
                            DLR.Expression.Call(
                                DLR.Expression.Constant(DyadicFunctionInstance.Cast),
                                DyadicFunctionInstance.Cast.GetType().GetMethod("Execute"),
                                rightParam, leftParam, environment
                                )
                            )
                        ),
                    valueParam
                    );
            }
            else if (this.token.Type == Tokens.BWOR)
            {
                DLR.ParameterExpression rightParam = DLR.Expression.Variable(typeof(AType), "$$rightParam");
                DLR.ParameterExpression leftParam  = DLR.Expression.Variable(typeof(AType), "$$leftParam");

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { leftParam, rightParam },
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.Assign(leftParam, left),
                    DLR.Expression.Condition(
                        // $left.Type == ATypes.ASymbol
                        DLR.Expression.Equal(
                            leftParam.Property("Type"),
                            DLR.Expression.Constant(ATypes.ASymbol)
                            ),
                        DLR.Expression.Constant(DyadicFunctionInstance.BitwiseCast).Call <BitwiseCast>(
                            "Execute", rightParam, leftParam, environment
                            ),
                        DLR.Expression.Constant(DyadicFunctionInstance.BitwiseOr).Call <BitwiseOr>(
                            "Execute", rightParam, leftParam, environment
                            )
                        )
                    );
            }
            else
            {
                AbstractDyadicFunction method = MethodChooser.GetDyadicMethod(this.token);

                if (method == null)
                {
                    throw new ParseException(String.Format("Not supported Dyadic function[{0}]", this.token));
                }

                result = DLR.Expression.Call(
                    DLR.Expression.Constant(method),
                    method.GetType().GetMethod("Execute"),
                    right, left, environment
                    );
            }

            return(result);
        }
Esempio n. 28
0
        /// <summary>Generates a DLR Expression for Strand assignment.</summary>
        /// <remarks>
        ///  Transform this:
        /// (a;b;...) := .....    // don't care what is there :)
        /// 
        /// to this in DLR:
        /// 
        /// $__VALUES__ = (AType) .....; // ..... is a generated DLR expression now!
        /// 
        /// if($__VALUES__.IsArray)     // case A
        /// {
        ///     if($VALUES.length != 2)  // 2 the # of targets
        ///     {
        ///         throw new Error.Length("assign");
        ///     }
        ///     
        ///     a = disclose( ((AArray)$__VALUES__)[0] );
        ///     b = disclose( ((AArray)$__VALUES__)[1] );
        ///     ...
        /// }
        /// else    // case B
        /// {
        ///     // need to disclose the box
        ///     if($__VALUES__.Type == ATypes.Box)
        ///     {
        ///         $__VALUES__ = disclose($__VALUES__);
        ///     }
        ///     a = $VALUES.Clone();
        ///     b = $VALUES.Clone();
        ///     ...
        /// }
        /// $__DependencyManager__.InvalidateDependencies(string[] { a,b, .. })
        /// $__DependencyManager__.ValidateDependencies(string[] { a,b, .. })
        /// </remarks>
        /// <param name="scope"></param>
        /// <param name="targets">
        /// Strand containing Identifiers.
        /// If a node which in not an Identifier found, ParseException is thrown
        /// </param>
        /// <param name="value">The generated value which will be assigned to the targets</param>
        /// <returns>A generated DLR expression for strand assignment</returns>
        private static DLR.Expression GenerateStrandAssign(AplusScope scope, Strand targets, DLR.Expression value)
        {
            AbstractMonadicFunction disclose = MonadicFunctionInstance.Disclose;
            Aplus runtime = scope.GetRuntime();
            DLR.ParameterExpression environment = scope.GetRuntimeExpression();

            DLR.ParameterExpression valuesParam = DLR.Expression.Parameter(typeof(AType), "__VALUES__");
            // for dependency evaluation
            List<string> targetVariables = new List<string>();

            // for case A) assigns
            List<DLR.Expression> strand2StrandAssigns = new List<DLR.Expression>();
            // for case B) assigns
            List<DLR.Expression> strand2ValueAssigns = new List<DLR.Expression>()
            {
                DLR.Expression.Assign(
                    valuesParam,
                    DLR.Expression.Call(
                        DLR.Expression.Constant(disclose),
                        disclose.GetType().GetMethod("Execute"),
                        valuesParam,
                        environment
                    )
                )
            };

            int indexCounter = 0;

            foreach (Node node in targets.Items)
            {
                if (!(node is Identifier))
                {
                    throw new ParseException("assign lhs");
                }
                Identifier id = (Identifier)node;

                DLR.Expression strandValue =
                    DLR.Expression.Call(
                        DLR.Expression.Constant(disclose),
                        disclose.GetType().GetMethod("Execute"),
                        DLR.Expression.MakeIndex(
                            valuesParam,
                            typeof(AType).GetIndexerProperty(typeof(int)), // The indexer property which will be used
                            new DLR.Expression[] { DLR.Expression.Constant(indexCounter, typeof(int)) }
                        ),
                        environment
                );

                // case A) $TARGETS[i] = disclose($VALUES[i])
                strand2StrandAssigns.Add(GenerateIdentifierAssign(scope, id, strandValue, isStrand: true));

                // case B) $TARGETS[i] = $VALUE.Clone()
                strand2ValueAssigns.Add(
                    GenerateIdentifierAssign(
                        scope,
                        id,
                        DLR.Expression.Condition(
                            DLR.Expression.Property(valuesParam, "IsMemoryMappedFile"),
                            valuesParam,
                            DLR.Expression.Call(
                                valuesParam,
                                typeof(AType).GetMethod("Clone")
                            )
                        ),
                        isStrand: true
                    )
                );

                // gather the global variables for dependency validation/invalidation. 
                if ((scope.IsMethod && id.IsEnclosed) || !scope.IsMethod)
                {
                    targetVariables.Add(id.BuildQualifiedName(runtime.CurrentContext));
                }

                indexCounter++;
            }

            // case A)
            DLR.Expression caseStrand2Strand =
                DLR.Expression.IfThenElse(
                    DLR.Expression.NotEqual(
                        DLR.Expression.PropertyOrField(valuesParam, "Length"),
                        DLR.Expression.Constant(indexCounter, typeof(int))
                    ),
                    DLR.Expression.Throw(
                        DLR.Expression.New(
                            typeof(Error.Length).GetConstructor(new Type[] { typeof(string) }),
                            DLR.Expression.Constant("assign", typeof(string))
                        )
                    ),
                    DLR.Expression.Block(strand2StrandAssigns)
            );

            DLR.Expression dependencyManager = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");

            DLR.Expression result = DLR.Expression.Block(
                new DLR.ParameterExpression[] { valuesParam },
                DLR.Expression.Assign(valuesParam, DLR.Expression.Convert(value, typeof(AType))),
                DLR.Expression.IfThenElse(
                    DLR.Expression.PropertyOrField(valuesParam, "IsArray"),
                // case A)
                    caseStrand2Strand,
                // case B)
                    DLR.Expression.Block(strand2ValueAssigns)
                ),
                DLR.Expression.Call(
                    dependencyManager,
                    typeof(DependencyManager).GetMethod("InvalidateDependencies", new Type[] { typeof(string[]) }),
                    DLR.Expression.Constant(targetVariables.ToArray())
                ),
                DLR.Expression.Call(
                    dependencyManager,
                    typeof(DependencyManager).GetMethod("ValidateDependencies"),
                    DLR.Expression.Constant(targetVariables.ToArray())
                ),
                valuesParam
            );

            return DLR.Expression.Block(result, DLR.Expression.Assign(scope.CallbackInfo.NonPresetValue, value));
        }
Esempio n. 29
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            PreprocessVariables(scope);

            DLR.Expression result  = null;
            Aplus          runtime = scope.GetRuntime();

            if (scope.IsEval && scope.IsMethod)
            {
                // override the original scope
                // create a top level scope
                scope = new AplusScope(null, "_EVAL_DEP_SCOPE_",
                                       scope.GetRuntime(),
                                       scope.GetRuntimeExpression(),
                                       scope.Parent.GetModuleExpression(),
                                       scope.ReturnTarget,
                                       isEval: true
                                       );
            }

            // 1. Create a function scope
            string     dependencyName  = this.variable.BuildQualifiedName(runtime.CurrentContext);
            string     scopename       = String.Format("__dependency_{0}_scope__", this.variable.Name);
            AplusScope dependencyScope = new AplusScope(scope, scopename,
                                                        runtimeParam: scope.GetRuntimeExpression(),
                                                        moduleParam: DLR.Expression.Parameter(typeof(DYN.ExpandoObject), scopename),
                                                        returnTarget: DLR.Expression.Label(typeof(AType), "RETURN"),
                                                        isMethod: true
                                                        );
            // 1.5. Method for registering dependencies
            MethodInfo registerMethod;

            // 2. Prepare the method arguments (RuntimeExpression)
            DLR.ParameterExpression[] methodParameters;

            if (this.IsItemwise)
            {
                DLR.ParameterExpression index =
                    DLR.Expression.Parameter(typeof(AType), string.Format("__INDEX[{0}]__", this.Indexer.Name));
                dependencyScope.Variables.Add(this.Indexer.Name, index);

                methodParameters = new DLR.ParameterExpression[] { dependencyScope.RuntimeExpression, index };
                registerMethod   = typeof(DependencyManager).GetMethod("RegisterItemwise");
            }
            else
            {
                methodParameters = new DLR.ParameterExpression[] { dependencyScope.RuntimeExpression };
                registerMethod   = typeof(DependencyManager).GetMethod("Register");
            }

            // 2.5  Create a result parameter
            DLR.ParameterExpression resultParameter = DLR.Expression.Parameter(typeof(AType), "__RESULT__");

            // 2.75 Get the dependency informations
            DLR.Expression dependencyInformation =
                DLR.Expression.Call(
                    DLR.Expression.Property(dependencyScope.GetRuntimeExpression(), "DependencyManager"),
                    typeof(DependencyManager).GetMethod("GetDependency"),
                    DLR.Expression.Constant(dependencyName)
                    );

            // 3. Build the method
            DLR.LambdaExpression method = DLR.Expression.Lambda(
                DLR.Expression.Block(
                    new DLR.ParameterExpression[] { dependencyScope.ModuleExpression, resultParameter },
                    // Add the local scope's store
                    DLR.Expression.Assign(dependencyScope.ModuleExpression, DLR.Expression.Constant(new DYN.ExpandoObject())),
                    // set AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(dependencyScope.RuntimeExpression, "FunctionScope"),
                        dependencyScope.ModuleExpression
                        ),
                    // Mark the dependency as under evaluation
                    DLR.Expression.Call(
                        dependencyInformation,
                        typeof(DependencyItem).GetMethod("Mark"),
                        DLR.Expression.Constant(DependencyState.Evaluating)
                        ),
                    // Calculate the result of the defined function
                    DLR.Expression.Assign(
                        resultParameter,
                        DLR.Expression.Label(dependencyScope.ReturnTarget, this.functionBody.Generate(dependencyScope))
                        ),
                    // Mark the dependency as valid
                    DLR.Expression.Call(
                        dependencyInformation,
                        typeof(DependencyItem).GetMethod("Mark"),
                        DLR.Expression.Constant(DependencyState.Valid)
                        ),
                    // reset  AplusEnviroment's function scope reference
                    DLR.Expression.Assign(
                        DLR.Expression.Property(dependencyScope.RuntimeExpression, "FunctionScope"),
                        DLR.Expression.Constant(null, typeof(DYN.ExpandoObject))
                        ),
                    // Return the result
                    resultParameter
                    ),
                dependencyName,
                methodParameters
                );

            DLR.Expression wrappedLambda = DLR.Expression.Call(
                typeof(AFunc).GetMethod("Create"),
                DLR.Expression.Constant(dependencyName),
                method,
                DLR.Expression.Constant(methodParameters.Length),
                DLR.Expression.Constant(this.codeText)
                );

            // 3.5 Build dependant set
            // filter out the variables from the dependant set if it is a local variable
            HashSet <string> dependents = new HashSet <string>(
                from pair in this.variables.Accessing                           // get all variables
                where !this.variables.LocalAssignment.ContainsKey(pair.Key)     // but skip the local variables
                select pair.Value[0].BuildQualifiedName(runtime.CurrentContext) // then build the correct name
                );

            // 4. Register the method for the Dependency manager
            DLR.ParameterExpression dependencyMethodParam = DLR.Expression.Parameter(typeof(AType), "__DEP._METHOD__");
            DLR.Expression          dependencyManager     = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");
            result = DLR.Expression.Block(
                new DLR.ParameterExpression[] { dependencyMethodParam },
                DLR.Expression.Assign(dependencyMethodParam, wrappedLambda),
                DLR.Expression.Call(
                    dependencyManager,
                    registerMethod,
                    DLR.Expression.Constant(dependencyName, typeof(string)),
                    DLR.Expression.Constant(dependents, typeof(HashSet <string>)),
                    dependencyMethodParam
                    ),
                dependencyMethodParam
                );

            return(result);
        }
Esempio n. 30
0
        private static DLR.Expression BuildGlobalAssignment(
            AplusScope scope,
            Aplus runtime,
            DLR.Expression variableContainer,
            Identifier target,
            DLR.Expression value,
            bool isStrand = false,
            bool needCallback = true
            )
        {
            // Build the ET for updating the dependency
            DLR.Expression dependencyCall;
            string qualifiedName = target.BuildQualifiedName(runtime.CurrentContext);

            if (isStrand)
            {
                // We are inside a starnd assignment, do nothing.
                // Dependency update will be handled later.
                dependencyCall = DLR.Expression.Empty();
            }
            else
            {
                // Otherwise build the dependency invalidation call.
                DLR.Expression dependencyManager = DLR.Expression.Property(scope.GetRuntimeExpression(), "DependencyManager");
                dependencyCall = DLR.Expression.Call(
                    dependencyManager,
                    typeof(DependencyManager).GetMethod("InvalidateDependencies", new Type[] { typeof(string) }),
                    DLR.Expression.Constant(qualifiedName)
                );
            }

            DLR.ParameterExpression errorParam = DLR.Expression.Parameter(typeof(Error.Signal), "__ERRORPARAM__");
            DLR.ParameterExpression valueParam = DLR.Expression.Parameter(typeof(AType), "__VALUEPARAMETER__");
            scope.AssignDone = (scope.AssignDone == null) ? DLR.Expression.Parameter(typeof(bool), "__ASSIGNDONE__") : scope.AssignDone;

            DLR.Expression presetCallback = BuildPresetCallbackCall(scope, valueParam);
            DLR.Expression callback = BuildCallbackCall(scope, valueParam);

            DLR.Expression variableSet =
                DLR.Expression.Block(
                        VariableHelper.SetVariable(
                            runtime,
                            variableContainer,
                            target.CreateContextNames(runtime.CurrentContext),
                            valueParam
                        ),
                        dependencyCall
                    );

            DLR.Expression codebody;

            if (needCallback)
            {
                codebody =
                    DLR.Expression.Block(
                        typeof(void),
                        DLR.Expression.Assign(scope.CallbackInfo.QualifiedName, DLR.Expression.Constant(qualifiedName)),
                        DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(false)),
                        DLR.Expression.TryCatch(
                            DLR.Expression.Block(
                                typeof(void),
                                DLR.Expression.Assign(valueParam, presetCallback),
                                DLR.Expression.Assign(scope.AssignDone, DLR.Expression.Constant(true))
                            ),
                            DLR.Expression.Catch(
                                errorParam,
                                DLR.Expression.Empty()
                            )
                        ),
                        DLR.Expression.IfThen(
                            scope.AssignDone,
                            DLR.Expression.Block(
                                variableSet,
                                callback
                            )
                        )
                    );
            }
            else
            {
                codebody = variableSet;
            }

            // value assignment
            DLR.Expression result = DLR.Expression.Block(
                new DLR.ParameterExpression[] { valueParam },
                DLR.Expression.Assign(valueParam, value),
                codebody,
                valueParam
            );

            return result;
        }
Esempio n. 31
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression func, result;

            if (this.function is Token)
            {
                Node wrappedFunction = new BuiltInFunction((Token)this.function);
                func = wrappedFunction.Generate(scope);
            }
            else
            {
                func = this.function.Generate(scope);
            }

            DLR.ParameterExpression environment = scope.GetRuntimeExpression();

            DLR.ParameterExpression functionParam = DLR.Expression.Variable(typeof(AType), "$$functionParam");
            DLR.ParameterExpression rightParam = DLR.Expression.Variable(typeof(AType), "$$rightParam");
            DLR.ParameterExpression valueParam = DLR.Expression.Variable(typeof(AType), "$$valueParam");

            // TODO: rewrite this
            if (this.IsGeneralApply)
            {
                ExpressionList argumnets = (ExpressionList)this.rightarg;
                LinkedList<DLR.Expression> callArguments = new LinkedList<DLR.Expression>();

                // 2. Add the parameters in !reverse! order
                foreach (Node item in argumnets.Items)
                {
                    callArguments.AddFirst(item.Generate(scope));
                }

                // 0. Add A+ environment as first argument for user defined functions
                callArguments.AddFirst(environment);

                // 1. Construct the method body
                callArguments.AddFirst(functionParam.Property("NestedItem"));

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { functionParam, valueParam },
                    DLR.Expression.Assign(functionParam, func),
                    DLR.Expression.IfThenElse(
                            DLR.Expression.PropertyOrField(functionParam, "IsFunctionScalar"),
                            DLR.Expression.Assign(
                                valueParam,
                                AST.UserDefInvoke.BuildInvoke(scope.GetRuntime(), callArguments)
                            ),
                            DLR.Expression.Throw(
                                DLR.Expression.New(
                                    typeof(Error.Valence).GetConstructor(new Type[] { typeof(string) }),
                                    DLR.Expression.Constant("apply")
                                )
                            )
                        ),
                    valueParam
                    );
            }
            else if (isDyadic)
            {
                DLR.Expression right = this.rightarg.Generate(scope);
                DLR.Expression left = this.leftarg.Generate(scope);
                DLR.ParameterExpression leftParam = DLR.Expression.Variable(typeof(AType), "$$leftParam");

                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { functionParam, rightParam, leftParam, valueParam },
                    DLR.Expression.Assign(functionParam, func),
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.Assign(leftParam, left),
                    DLR.Expression.IfThenElse(
                        DLR.Expression.IsTrue(
                            DLR.Expression.PropertyOrField(functionParam, "IsFunctionScalar")
                        ),
                         DLR.Expression.Assign(
                            valueParam,
                             DLR.Expression.Call(
                                 DLR.Expression.Constant(DyadicOperatorInstance.Apply),
                                 DyadicOperatorInstance.Apply.GetType().GetMethod("Execute"),
                                 functionParam, rightParam, leftParam, environment
                             )
                         ),
                         DLR.Expression.Assign(
                            valueParam,
                             DLR.Expression.Call(
                                 DLR.Expression.Constant(DyadicOperatorInstance.Each),
                                 DyadicOperatorInstance.Each.GetType().GetMethod("Execute"),
                                 functionParam, rightParam, leftParam, environment
                             )
                        )
                    ),
                    valueParam
                 );
            }
            else
            {
                DLR.Expression right = this.rightarg.Generate(scope);
                result = DLR.Expression.Block(
                    new DLR.ParameterExpression[] { functionParam, rightParam, valueParam },
                    DLR.Expression.Assign(functionParam, func),
                    DLR.Expression.Assign(rightParam, right),
                    DLR.Expression.IfThenElse(
                        DLR.Expression.IsTrue(
                            DLR.Expression.PropertyOrField(functionParam, "IsFunctionScalar")
                        ),
                         DLR.Expression.Assign(
                            valueParam,
                             DLR.Expression.Call(
                                 DLR.Expression.Constant(MonadicOperatorInstance.Apply),
                                 MonadicOperatorInstance.Apply.GetType().GetMethod("Execute"),
                                 functionParam, rightParam, environment
                             )
                         ),
                         DLR.Expression.Assign(
                            valueParam,
                             DLR.Expression.Call(
                                 DLR.Expression.Constant(MonadicOperatorInstance.Each),
                                 MonadicOperatorInstance.Each.GetType().GetMethod("Execute"),
                                 functionParam, rightParam, environment
                             )
                        )
                    ),
                    valueParam
                 );
            }

            return result;
        }