public PipeTypeOperatorInfo(PipeType type) : base(type)
 {
     _pipeType = type;
 }
Exemplo n.º 2
0
        void SetupScope()
        {
            if (_scopeInstance != null)
            {
                return;
            }

            _scopeInstance      = new Scope();
            _operationsInstance = new TypeOperatorInfo(this);

            Scope.AddNativeVariable(_length);
            Scope.AddNativeVariable(_last);
            Scope.AddNativeVariable(_first);

            var pipeType        = new PipeType(ArrayOfType, this);
            var functionHandler = ArrayOfType.ArrayHandler.GetFunctionHandler();

            // Filtered Array
            new GenericSortFunction()
            {
                Name                   = "FilteredArray",
                Documentation          = "A copy of the specified array with any values that do not match the specified condition removed.",
                ReturnType             = this,
                ArrayOfType            = ArrayOfType,
                FuncType               = _supplier.Boolean(),
                ParameterDocumentation = "The condition that is evaluated for each element of the copied array. If the condition is true, the element is kept in the copied array.",
                Function               = "Filtered Array",
                Executor               = functionHandler.FilteredArray()
            }.Add(Scope, _supplier);
            // Sorted Array
            new GenericSortFunction()
            {
                Name                   = "SortedArray",
                Documentation          = "A copy of the specified array with the values sorted according to the value rank that is evaluated for each element.",
                ReturnType             = this,
                ArrayOfType            = ArrayOfType,
                FuncType               = _supplier.Boolean(),
                ParameterDocumentation = "The value that is evaluated for each element of the copied array. The array is sorted by this rank in ascending order.",
                Function               = "Sorted Array",
                Executor               = functionHandler.SortedArray()
            }.Add(Scope, _supplier);
            // Is True For Any
            new GenericSortFunction()
            {
                Name                   = "IsTrueForAny",
                Documentation          = "Whether the specified condition evaluates to true for any value in the specified array.",
                ReturnType             = _supplier.Boolean(),
                ArrayOfType            = ArrayOfType,
                FuncType               = _supplier.Boolean(),
                ParameterDocumentation = "The condition that is evaluated for each element of the specified array.",
                Function               = "Is True For Any",
                Executor               = functionHandler.Any()
            }.Add(Scope, _supplier);
            // Is True For All
            new GenericSortFunction()
            {
                Name                   = "IsTrueForAll",
                Documentation          = "Whether the specified condition evaluates to true for every value in the specified array.",
                ReturnType             = _supplier.Boolean(),
                ArrayOfType            = ArrayOfType,
                FuncType               = _supplier.Boolean(),
                ParameterDocumentation = "The condition that is evaluated for each element of the specified array.",
                Function               = "Is True For All",
                Executor               = functionHandler.All()
            }.Add(Scope, _supplier);
            // Mapped
            var mapGenericParameter = new AnonymousType("T", new AnonymousTypeAttributes(false));
            var mapmethodInfo       = new MethodInfo(new[] { mapGenericParameter });

            mapGenericParameter.Context = mapmethodInfo.Tracker;
            new GenericSortFunction()
            {
                Name                   = "Map",
                Documentation          = "Whether the specified condition evaluates to true for every value in the specified array.",
                ReturnType             = new ArrayType(_supplier, mapGenericParameter),
                ArrayOfType            = ArrayOfType,
                FuncType               = mapGenericParameter,
                ParameterDocumentation = "The condition that is evaluated for each element of the specified array.",
                Function               = "Mapped Array",
                Executor               = functionHandler.Map(),
                MethodInfo             = mapmethodInfo
            }.Add(Scope, _supplier);
            // Contains
            Func(new FuncMethodBuilder()
            {
                Name          = "Contains",
                Documentation = "Whether the array contains the specified value.",
                ReturnType    = _supplier.Boolean(),
                Parameters    = new CodeParameter[] {
                    new CodeParameter("value", "The value that is being looked for in the array.", ArrayOfType)
                },
                Action = (actionSet, methodCall) => functionHandler.Contains(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Random
            if (functionHandler.AllowUnhandled)
            {
                Func(new FuncMethodBuilder()
                {
                    Name          = "Random",
                    Documentation = "Gets a random value from the array.",
                    ReturnType    = ArrayOfType,
                    Action        = (actionSet, methodCall) => Element.Part("Random Value In Array", actionSet.CurrentObject)
                });
            }
            // Randomize
            if (functionHandler.AllowUnhandled)
            {
                Func(new FuncMethodBuilder()
                {
                    Name          = "Randomize",
                    Documentation = "Returns a copy of the array that is randomized.",
                    ReturnType    = this,
                    Action        = (actionSet, methodCall) => Element.Part("Randomized Array", actionSet.CurrentObject)
                });
            }
            // Append
            if (functionHandler.AllowUnhandled)
            {
                Func(new FuncMethodBuilder()
                {
                    Name          = "Append",
                    Documentation = "A copy of the array with the specified value appended to it.",
                    ReturnType    = this,
                    Parameters    = new CodeParameter[] {
                        new CodeParameter("value", "The value that is appended to the array. If the value is an array, it will be flattened.", pipeType)
                    },
                    Action = (actionSet, methodCall) => Element.Append(actionSet.CurrentObject, methodCall.ParameterValues[0])
                });
            }
            // Remove
            if (functionHandler.AllowUnhandled)
            {
                Func(new FuncMethodBuilder()
                {
                    Name          = "Remove",
                    Documentation = "A copy of the array with the specified value removed from it.",
                    ReturnType    = this,
                    Parameters    = new CodeParameter[] {
                        new CodeParameter("value", "The value that is removed from the array.", pipeType)
                    },
                    Action = (actionSet, methodCall) => Element.Part("Remove From Array", actionSet.CurrentObject, methodCall.ParameterValues[0])
                });
            }
            // Slice
            if (functionHandler.AllowUnhandled)
            {
                Func(new FuncMethodBuilder()
                {
                    Name          = "Slice",
                    Documentation = "A copy of the array containing only values from a specified index range.",
                    ReturnType    = this,
                    Parameters    = new CodeParameter[] {
                        new CodeParameter("startIndex", "The first index of the range.", _supplier.Number()),
                        new CodeParameter("count", "The number of elements in the resulting array. The resulting array will contain fewer elements if the specified range exceeds the bounds of the array.", _supplier.Number())
                    },
                    Action = (actionSet, methodCall) => Element.Part("Array Slice", actionSet.CurrentObject, methodCall.ParameterValues[0], methodCall.ParameterValues[1])
                });
            }
            // Index Of
            if (functionHandler.AllowUnhandled)
            {
                Func(new FuncMethodBuilder()
                {
                    Name          = "IndexOf",
                    Documentation = "The index of a value within an array or -1 if no such value can be found.",
                    ReturnType    = _supplier.Number(),
                    Parameters    = new CodeParameter[] {
                        new CodeParameter("value", "The value for which to search.", ArrayOfType)
                    },
                    Action = (actionSet, methodCall) => Element.IndexOfArrayValue(actionSet.CurrentObject, methodCall.ParameterValues[0])
                });
            }
            // Modify Append
            Func(new FuncMethodBuilder()
            {
                Name          = "ModAppend",
                Documentation = "Appends a value to the array. This will modify the array directly rather than returning a copy of the array. The source expression must be a variable.",
                Parameters    = new CodeParameter[] {
                    new CodeParameter("value", "The value that is pushed to the array.", pipeType)
                },
                OnCall     = SourceVariableResolver.GetSourceVariable,
                ReturnType = _supplier.Number(),
                Action     = (actionSet, methodCall) => SourceVariableResolver.Modify(actionSet, methodCall, Operation.AppendToArray)
            });
            // Modify Remove By Value
            Func(new FuncMethodBuilder()
            {
                Name          = "ModRemoveByValue",
                Documentation = "Removes an element from the array by a value. This will modify the array directly rather than returning a copy of the array. The source expression must be a variable.",
                Parameters    = new CodeParameter[] {
                    new CodeParameter("value", "The value that is removed from the array.", ArrayOfType)
                },
                OnCall     = SourceVariableResolver.GetSourceVariable,
                ReturnType = _supplier.Number(),
                Action     = (actionSet, methodCall) => SourceVariableResolver.Modify(actionSet, methodCall, Operation.RemoveFromArrayByValue)
            });
            // Modify Remove By Index
            Func(new FuncMethodBuilder()
            {
                Name          = "ModRemoveByIndex",
                Documentation = "Removes an element from the array by the index. This will modify the array directly rather than returning a copy of the array. The source expression must be a variable.",
                Parameters    = new CodeParameter[] {
                    new CodeParameter("index", "The index of the element that is removed from the array.", _supplier.Number())
                },
                OnCall     = SourceVariableResolver.GetSourceVariable,
                ReturnType = _supplier.Number(),
                Action     = (actionSet, methodCall) => SourceVariableResolver.Modify(actionSet, methodCall, Operation.RemoveFromArrayByIndex)
            });

            // Add type operations.
            Operations.AddTypeOperation(new[] {
                // + append
                new TypeOperation(TypeOperator.Add, pipeType, this, (l, r) => Element.Append(l, r)),
                // - remove
                new TypeOperation(TypeOperator.Subtract, pipeType, this, (l, r) => Element.Remove(l, r))
            });
            Operations.AddTypeOperation(new[] {
                // += mod append
                new AssignmentOperation(AssignmentOperator.AddEqual, pipeType, info => info.Modify(Operation.AppendToArray)),
                // -= mod remove
                new AssignmentOperation(AssignmentOperator.SubtractEqual, pipeType, info => info.Modify(Operation.RemoveFromArrayByValue))
            });

            ArrayOfType.ArrayHandler.OverrideArray(this);
        }