Exemplo n.º 1
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;
        }
Exemplo n.º 2
0
        private static string ToDot(string parent, BuiltInFunction node)
        {
            string name = String.Format("BuiltIn{0}", counter++);

            text.AppendFormat(" {0} [label={1}];\n", name, node.Function);

            return name;
        }
Exemplo n.º 3
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;
        }