Пример #1
0
        public override DLR.Expression Generate(AplusScope scope)
        {
            DLR.Expression result;

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

                // i:=(iota rho x)
                DLR.Expression indices = AST.Assign.BuildIndicesList(scope, target);
                // (,x)[f{i}]
                result = AST.Assign.BuildIndexing(scope, target, GenerateMonadic(scope, indices));
            }
            else
            {
                result = GenerateMonadic(scope, this.expression.Generate(scope));
            }

            return(result);
        }
Пример #2
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);
        }
Пример #3
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);
        }