Exemple #1
0
        /// <summary>
        /// </summary>
        /// <param name="right"></param>
        /// <param name="left"></param>
        private LaminateJobInfo CreateLaminateJob(AType right, AType left)
        {
            LaminateJobInfo laminateInfo =
                new LaminateJobInfo(left.Length != 0 ? left.Type : right.Type);
            
            if (left.IsArray && !right.IsArray)
            {
                laminateInfo.Left = left.Clone();
                laminateInfo.Right = DyadicFunctionInstance.Reshape.Execute(right, left.Shape.ToAArray());
            }
            else if (!left.IsArray && right.IsArray)
            {
                laminateInfo.Right = right.Clone();
                laminateInfo.Left = DyadicFunctionInstance.Reshape.Execute(left, right.Shape.ToAArray());
            }
            else
            {
                laminateInfo.Right = right.Clone();
                laminateInfo.Left = left.Clone();
            }

            // check if the types are same

            if (right.Type != left.Type)
            {
                if (left.Type == ATypes.AFloat && right.Type == ATypes.AInteger)
                {
                    right = right.ConvertToFloat();
                }
                else if (left.Type == ATypes.AInteger && right.Type == ATypes.AFloat)
                {
                    left = left.ConvertToFloat();
                }
                else if (!(Utils.IsSameGeneralType(left, right) 
                    || left.Type == ATypes.ANull || right.Type == ATypes.ANull))
                {
                    throw new Error.Type(TypeErrorText);
                }
            }

            // check the length, shape and rank
            if (laminateInfo.Left.Rank != laminateInfo.Right.Rank)
            {
                throw new Error.Rank(RankErrorText);
            }

            if (!laminateInfo.Left.Shape.SequenceEqual(laminateInfo.Right.Shape))
            {
                throw new Error.Length(LengthErrorText);
            }

            if (laminateInfo.Left.Rank >= 9 || laminateInfo.Right.Rank >= 9)
            {
                throw new Error.MaxRank(MaxRankErrorText);
            }

            return laminateInfo;
        }
Exemple #2
0
        private AType ConvertToInt(AType argument)
        {
            AType result;

            switch (argument.Type)
            {
            case ATypes.AInteger:
                result = argument.Clone();
                break;

            case ATypes.AFloat:
                result = ConvertFloatToInt(argument);
                break;

            case ATypes.AChar:
                result = ConvertCharToInt(argument, false);
                break;

            case ATypes.ASymbol:
                result = ConvertSymToInt(argument);
                break;

            default:
                throw new Error.Domain(this.DomainErrorText);
            }

            return(result);
        }
Exemple #3
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            // First we check if one side is an ANull
            if (right.Type == ATypes.ANull)
            {
                return AArray.Create(left.Type, left.Clone());
            }
            else if (left.Type == ATypes.ANull)
            {
                return AArray.Create(right.Type, right.Clone());
            }

            // Type check
            if(!Utils.IsSameGeneralType(left, right))
            {
                throw new Error.Type(this.TypeErrorText);
            }

            AType result = CreateResult(right, left);

            // Convert to float if one of the arguments is an AFloat and the other is an AInteger
            if (Utils.DifferentNumberType(left, right))
            {
                result.ConvertToFloat();
            }

            return result;
        }
        internal static AType StringSearchandReplace(Aplus environment, AType replaceWith, AType replaceWhat, AType replaceIn)
        {
            if (replaceIn.Type != ATypes.AChar || replaceWhat.Type != ATypes.AChar || replaceWith.Type != ATypes.AChar)
            {
                throw new Error.Type("_ssr");
            }

            string withString = Monadic.MonadicFunctionInstance.Ravel.Execute(replaceWith, environment).ToString();
            string whatString = Monadic.MonadicFunctionInstance.Ravel.Execute(replaceWhat, environment).ToString();
            AType argument = (withString.Length == whatString.Length)
                ? replaceIn.Clone() : Monadic.MonadicFunctionInstance.Ravel.Execute(replaceIn, environment);

            Queue<string> rows = new Queue<string>();
            ExtractRows(argument, rows);

            Queue<string> replacedRows = new Queue<string>();

            foreach (string item in rows)
            {
                string replaced = item.Replace(whatString, withString);
                replacedRows.Enqueue(replaced);
            }

            AType result = BuildAType(replacedRows, argument.Shape);
            return result;

        }
Exemple #5
0
        public override AType Execute(AType argument, Aplus environment)
        {
            AType result;

            if (argument.SimpleArray())
            {
                result = argument.IsMemoryMappedFile ? argument : argument.Clone();
            }
            else
            {
                if (!argument.NestedArray())
                {
                    throw new Error.Domain(DomainErrorText);
                }

                switch (argument.Rank)
                {
                    case 0:
                        result = MonadicFunctionInstance.Disclose.Execute(argument);
                        break;
                    case 1:
                        result = NestedVector(argument);
                        break;
                    default:
                        throw new Error.Rank(RankErrorText);
                }
            }

            return result;
        }
Exemple #6
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            SetVariables(argument);

            AType result;

            //First dimension equal with zero case (as Null).
            if (argument.Length == 0)
            {
                result = argument.Clone();
                result.Type = this.type;
            }
            else
            {
                //Accepted types are float and integer.
                if (argument.Type != ATypes.AFloat && argument.Type != ATypes.AInteger)
                {
                    throw new Error.Type(TypeErrorText);
                }

                result = argument.IsArray ? ScanAlgorithm(argument) : PreProcess(argument);
            }

            return result;
        }
Exemple #7
0
        private AType Compute(AType right, int dropCounter)
        {
            if (right.IsArray && dropCounter == 0)
            {
                return right.Clone();
            }

            AType result = AArray.Create(ATypes.AType);

            if (right.IsArray)
            {
                if (right.Length - Math.Abs(dropCounter) > 0)
                {
                    if (dropCounter > 0)
                    {
                        for (int i = dropCounter; i < right.Length; i++)
                        {
                            result.AddWithNoUpdate(right[i].Clone());
                        }
                    }
                    else
                    {
                        for (int i = 0; i < right.Length + dropCounter; i++)
                        {
                            result.AddWithNoUpdate(right[i].Clone());
                        }
                    }
                    result.Length = right.Length - Math.Abs(dropCounter);
                    result.Shape = new List<int>() { result.Length };
                    
                    if (right.Rank > 1)
                    {
                        result.Shape.AddRange(right.Shape.GetRange(1, right.Shape.Count - 1));
                    }

                    result.Rank = right.Rank;
                    result.Type = result[0].Type;
                }
                else
                {
                    result.Type = right.MixedType() ? ATypes.ANull : right.Type;
                }
            }
            else
            {
                if (dropCounter == 0)
                {
                    result.Add(right.Clone());
                    result.Length = 1;
                    result.Shape = new List<int>() { 1 };
                    result.Type = right.Type;
                }
                else
                {
                    result.Type = right.MixedType() ? ATypes.ANull : right.Type;
                }
            }
            return result;
        }
 /// <summary>
 /// Transpose reduction to Transpose Axes.
 /// As follows: (rot iota rho rho x) flip x
 /// </summary>
 /// <param name="argument"></param>
 /// <returns></returns>
 public override AType Execute(AType argument, Aplus environment)
 {
     if (argument.IsArray)
     {
         AType transposeVector = Enumerable.Range(0, argument.Rank).Reverse().ToAArray();
         return DyadicFunctionInstance.TransposeAxis.Execute(argument, transposeVector);
     }
     else
     {
         return argument.Clone();
     }
 }
Exemple #9
0
 /// <summary>
 /// Transpose reduction to Transpose Axes.
 /// As follows: (rot iota rho rho x) flip x
 /// </summary>
 /// <param name="argument"></param>
 /// <returns></returns>
 public override AType Execute(AType argument, Aplus environment)
 {
     if (argument.IsArray)
     {
         AType transposeVector = Enumerable.Range(0, argument.Rank).Reverse().ToAArray();
         return(DyadicFunctionInstance.TransposeAxis.Execute(argument, transposeVector));
     }
     else
     {
         return(argument.Clone());
     }
 }
Exemple #10
0
        /// <summary>
        /// Select item from items array by pathVector.
        /// </summary>
        /// <param name="pathVector"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        private static AType GetItem(AType items, List <int> pathVector, int[] transposeVector)
        {
            int[] originalPathVector = GetOriginalIndex(pathVector, transposeVector);

            AType result = items;

            foreach (int pathElement in originalPathVector)
            {
                result = result[pathElement];
            }

            return(result.Clone());
        }
Exemple #11
0
        internal static AType Alsf(Aplus environment, AType input)
        {
            if (input.Rank > 1)
            {
                throw new Error.Rank("_alsf");
            }

            if (input.IsSlotFiller())
            {
                return(input.Clone());
            }

            return((input.IsArray) ? ArrayInput(input) : NotArrayInput(input));
        }
Exemple #12
0
        internal static AType Alsf(Aplus environment, AType input)
        {
            if (input.Rank > 1)
            {
                throw new Error.Rank("_alsf");
            }

            if (input.IsSlotFiller())
            {
                return input.Clone();
            }

            return (input.IsArray) ? ArrayInput(input) : NotArrayInput(input);
        }
Exemple #13
0
        public static AType SimpleIndex(this AType input, List <AType> indexers, int indexpos, AType currentIdx, bool isAssign, bool isMemoryMapped)
        {
            // Get the current item, specified by the currentIdx
            AType item = input[currentIdx];

            if (item.IsArray && indexpos < indexers.Count - 1)
            {
                // If it is an array and we can index further, then do so
                return(item.Indexing(indexers, indexpos + 1, isAssign, isMemoryMapped));
            }
            else
            {
                return(!isAssign && isMemoryMapped?item.Clone() : item);
            }
        }
Exemple #14
0
        public AType Compute(Aplus environment, AType right, AType left, out bool resultFromBox)
        {
            //Type Error!
            if (left.Type == ATypes.AChar || (left.Type == ATypes.AFloat && left.asInteger != left.asFloat))
            {
                throw new Error.Type(TypeErrorText);
            }


            // Case 2: If the right argument is scalar than we wrap it to one-element vector.
            AType rightArray;

            if (right.IsArray)
            {
                rightArray = right;
            }
            else
            {
                rightArray =
                    AArray.Create(right.Type, right.IsMemoryMappedFile ? right.Clone() : right);
            }

            AType result;

            if (left.IsBox)                         // Case 6
            {
                resultFromBox = true;
                result        =
                    ItemSelectWalker(NestedPathNumber2NestedArrayDelegate, left, rightArray, environment);
            }
            else if (left.Type == ATypes.ASymbol)   // Case 3
            {
                resultFromBox = true;
                result        =
                    ItemSelectWalker(SymbolConstant2SlotFillerDelegate, left, rightArray, environment);
            }
            else if (right.IsBox)                   // Case 5
            {
                result = PathVector2NestedVector(left, rightArray, out resultFromBox);
            }
            else                                    // Case 1-2-4
            {
                resultFromBox = false;
                result        = SimpleNumeric2SimpleRight(left, rightArray);
            }

            return(result);
        }
Exemple #15
0
        private static void PerformIndexAssign(AType target, AType value)
        {
            if (target.Rank > 0)
            {
                for (int i = 0; i < target.Length; i++)
                {
                    PerformIndexAssign(target[i], value.IsArray ? value[i] : value);
                }
            }
            else
            {
                AValue result;

                if (target.Type == value.Type)
                {
                    result = value.Clone().Data;
                }
                else if (target.Type == ATypes.AInteger && value.Type == ATypes.AFloat)
                {
                    int number;
                    if (!value.ConvertToRestrictedWholeNumber(out number))
                    {
                        throw new Error.Type("assign");
                    }

                    result = AInteger.Create(number).Data;
                }
                else if (target.Type == ATypes.AFloat && value.Type == ATypes.AInteger)
                {
                    result = AFloat.Create(value.asInteger).Data;
                }
                else
                {
                    throw new Error.Type("Assign");
                }

                if (target.IsMemoryMappedFile)
                {
                    ((IMapped)target.Data).Update(result);
                }
                else
                {
                    target.Data = result;
                }
            }
        }
Exemple #16
0
        /// <summary>
        /// If right side length equal zero, then the result
        /// is (1 drop x) rho identity, where identity is scalar
        /// that depends on function.
        /// </summary>
        /// <param name="shape"></param>
        /// <returns></returns>
        private AType Fill(List <int> shape)
        {
            if (shape.Count > 0)
            {
                AType result = AArray.Create(this.type);

                for (int i = 0; i < shape[0]; i++)
                {
                    result.Add(Fill(shape.GetRange(1, shape.Count - 1)));
                }

                return(result);
            }
            else
            {
                return(fillerElement.Clone());
            }
        }
Exemple #17
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.IsFunctionScalar)
            {
                return(GetFunction(argument));
            }
            else if (argument.IsBox)
            {
                if (argument.IsArray)
                {
                    throw new Error.Rank(RankErrorText);
                }
                else
                {
                    throw new Error.Type(TypeErrorText);
                }
            }
            else if (argument.Type == ATypes.ANull)
            {
                throw new Error.Rank(RankErrorText);
            }
            else if (argument.Type == ATypes.AChar)
            {
                return(argument.Clone());
            }

            // use Printing precision system variable
            int printingPrecision = (environment != null) ? environment.SystemVariables["pp"].asInteger : -1;

            switch (printingPrecision)
            {
            case -1:
                // system variable doesn't exist, so we write whole float number.
                printingPrecision = 0;
                break;

            case 0:
                // if `pp is 0, then it is reated as if it were one.
                printingPrecision = 1;
                break;
            }

            return(Compute(argument, printingPrecision));
        }
Exemple #18
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.SimpleArray())
            {
                //The argument is simple array/scalar and not mapped we clone it!
                return(argument.IsMemoryMappedFile ?
                       argument :
                       argument.Clone());
            }
            else
            {
                if (!argument.NestedArray())
                {
                    throw new Error.Domain(DomainErrorText);
                }

                return(DiscloseNestedArray(argument, environment));
            }
        }
Exemple #19
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.SimpleArray())
            {
                //The argument is simple array/scalar and not mapped we clone it! 
                return argument.IsMemoryMappedFile ?
                    argument :
                    argument.Clone();
            }
            else
            {
                if (!argument.NestedArray())
                {
                    throw new Error.Domain(DomainErrorText);
                }

                return DiscloseNestedArray(argument, environment);
            }
        }
Exemple #20
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.Type == ATypes.ANull)
            {
                return(argument.Clone());
            }

            if (!argument.SimpleSymbolArray())
            {
                throw new Error.Type(TypeErrorText);
            }

            if (argument.Rank > 8)
            {
                throw new Error.MaxRank(MaxRankErrorText);
            }

            return(Walk(argument));
        }
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.Type == ATypes.ANull)
            {
                return argument.Clone();
            }

            if (!argument.SimpleSymbolArray())
            {
                throw new Error.Type(TypeErrorText);
            }

            if (argument.Rank > 8)
            {
                throw new Error.MaxRank(MaxRankErrorText);
            }

            return Walk(argument);
        }
Exemple #22
0
        /// <summary>
        /// If argument is:
        ///  - integer then we clone it.
        ///  - null we return integer null.
        ///  - float then we use ConvertFloatConstantToIntegerConstant function.
        ///  - char we call ConvertCharConstantToNumberConstant function.
        ///  - symbol then we use ConvertSymbolConstantToNumber function.
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        private AType IntegerCase(AType argument)
        {
            AType    result;
            CastInfo castInfo = new CastInfo(ATypes.AInteger);

            switch (argument.Type)
            {
            case ATypes.AInteger:
                result = argument.Clone();
                break;

            case ATypes.ANull:
                result = Utils.ANull(ATypes.AInteger);
                break;

            case ATypes.AFloat:
                castInfo.Converter = new Converter(ConvertToInteger);
                result             = Walker(argument, castInfo);
                break;

            case ATypes.AChar:
                castInfo.Converter = new Converter(ConvertCharacterToNumber);
                result             = Walker(argument, castInfo);
                break;

            case ATypes.ASymbol:
                if (!argument.SimpleSymbolArray())
                {
                    throw new Error.Type(TypeErrorText);
                }

                DetectLongestSymbol(argument, castInfo);
                castInfo.Converter = new Converter(ConvertSymbolConstantToNumber);
                result             = Walker(argument, castInfo);
                break;

            default:
                throw new Error.Domain(DomainErrorText);
            }

            return(result);
        }
Exemple #23
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            AType result;
            int[] rotateVector = PrepareRotateVector(right, left);
            
            if (right.Rank == 0)
            {
                // if the right argument is scalar, we clone it
                result = right.Clone();
            }
            else if (right.Rank > 2)
            {
                result = TransformAndCompute(right, rotateVector);
            }
            else
            {
                result = Compute(right, rotateVector);
            }

            return result;
        }
Exemple #24
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            AType result;

            int[] rotateVector = PrepareRotateVector(right, left);

            if (right.Rank == 0)
            {
                // if the right argument is scalar, we clone it
                result = right.Clone();
            }
            else if (right.Rank > 2)
            {
                result = TransformAndCompute(right, rotateVector);
            }
            else
            {
                result = Compute(right, rotateVector);
            }

            return(result);
        }
Exemple #25
0
        private AType Compute(AType items, byte[] expandVector)
        {
            AType result = AArray.Create(ATypes.AType);
            int   index  = 0;

            // get the filler element based on the right argument
            AType fillElementShape =
                items.Rank > 1 ? items.Shape.GetRange(1, items.Shape.Count - 1).ToAArray() : null;
            AType filler = Utils.FillElement(items.Type, fillElementShape);

            for (int i = 0; i < expandVector.Length; i++)
            {
                if (expandVector[i] == 1)
                {
                    result.AddWithNoUpdate(items[items.Length > 1 ? index++ : 0].Clone());
                }
                else
                {
                    result.AddWithNoUpdate(filler.Clone());
                }
            }

            result.Length = expandVector.Length;
            result.Shape  = new List <int>()
            {
                expandVector.Length
            };

            if (items.Rank > 1)
            {
                result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1));
            }

            result.Rank = items.Rank;
            result.Type = result.Length > 0 ? result[0].Type : (items.MixedType() ? ATypes.ANull : items.Type);

            return(result);
        }
        /// <summary>
        /// Separate symbol constant by definiton.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        private AType SeparateSymbol(AType argument)
        {
            AType result = AArray.Create(ATypes.ASymbol);
            string symbol = argument.asString;
            int index = symbol.LastIndexOf('.');

            if (index != -1)
            {
                result.AddWithNoUpdate(ASymbol.Create(symbol.Substring(0, index)));
                result.AddWithNoUpdate(ASymbol.Create(symbol.Substring(index + 1)));
            }
            else
            {
                result.AddWithNoUpdate(ASymbol.Create(""));
                result.AddWithNoUpdate(argument.Clone());
            }

            result.Length = 2;
            result.Shape = new List<int>() { 2 };
            result.Rank = 1;

            return result;
        }
Exemple #27
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.IsArray)
            {
                AType result = AArray.Create(ATypes.AArray);

                for (int i = argument.Length - 1; i >= 0; i--)
                {
                    result.AddWithNoUpdate(argument[i].Clone());
                }

                result.Length = argument.Length;
                result.Shape  = new List <int>(argument.Shape);
                result.Rank   = argument.Rank;
                result.Type   = result.Length > 0 ? result[0].Type : ATypes.ANull;

                return(result);
            }
            else
            {
                return(argument.Clone());
            }
        }
Exemple #28
0
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.IsArray)
            {
                AType result = AArray.Create(ATypes.AArray);

                for (int i = argument.Length - 1;  i >= 0;  i--)
                {
                    result.AddWithNoUpdate(argument[i].Clone());
                }

                result.Length = argument.Length;
                result.Shape = new List<int>(argument.Shape);
                result.Rank = argument.Rank;
                result.Type = result.Length > 0 ? result[0].Type : ATypes.ANull;

                return result;
            }
            else
            {
                return argument.Clone();
            }
        }
Exemple #29
0
        private static void PerformIndexAssign(AType target, AType value)
        {
            if (target.Rank > 0)
            {
                for (int i = 0; i < target.Length; i++)
                {
                    PerformIndexAssign(target[i], value.IsArray ? value[i] : value);
                }
            }
            else
            {
                AValue result;

                if (target.Type == value.Type)
                {
                    result = value.Clone().Data;
                }
                else if (target.Type == ATypes.AInteger && value.Type == ATypes.AFloat)
                {
                    int number;
                    if (!value.ConvertToRestrictedWholeNumber(out number))
                    {
                        throw new Error.Type("assign");
                    }

                    result = AInteger.Create(number).Data;
                }
                else if (target.Type == ATypes.AFloat && value.Type == ATypes.AInteger)
                {
                    result = AFloat.Create(value.asInteger).Data;
                }
                else
                {
                    throw new Error.Type("Assign");
                }

                if (target.IsMemoryMappedFile)
                {
                    ((IMapped)target.Data).Update(result);
                }
                else
                {
                    target.Data = result;
                }
            }
        }
Exemple #30
0
        public AType Compute(Aplus environment, AType right, AType left, out bool resultFromBox)
        {
            //Type Error!
            if (left.Type == ATypes.AChar || (left.Type == ATypes.AFloat && left.asInteger != left.asFloat))
            {
                throw new Error.Type(TypeErrorText);
            }


            // Case 2: If the right argument is scalar than we wrap it to one-element vector.
            AType rightArray;

            if (right.IsArray)
            {
                rightArray = right;
            }
            else
            {
                rightArray = 
                    AArray.Create(right.Type, right.IsMemoryMappedFile ? right.Clone() : right);
            }

            AType result;

            if (left.IsBox)                         // Case 6
            {
                resultFromBox = true;
                result =
                    ItemSelectWalker(NestedPathNumber2NestedArrayDelegate, left, rightArray, environment);
            }
            else if (left.Type == ATypes.ASymbol)   // Case 3
            {
                resultFromBox = true;
                result =
                    ItemSelectWalker(SymbolConstant2SlotFillerDelegate, left, rightArray, environment);
            }
            else if (right.IsBox)                   // Case 5
            {
                result = PathVector2NestedVector(left, rightArray, out resultFromBox);
            }
            else                                    // Case 1-2-4
            {
                resultFromBox = false;
                result = SimpleNumeric2SimpleRight(left, rightArray);
            }

            return result;
        }
Exemple #31
0
 public override AType Execute(AType argument, Aplus environment = null)
 {
     return argument.IsMemoryMappedFile ? argument.Clone() : argument;
 }
Exemple #32
0
 protected virtual AType Process(AType argument)
 {
     return(argument.Clone());
 }
Exemple #33
0
        /// <summary>
        /// If argument is:
        /// - char we clone it.
        /// - null then we return char null.
        /// - symbol then we use Unpack nonscalar function.
        /// - null or symbol we just clone it.
        /// - float or integer we use ConvertToCharConstant function.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        private AType CharCase(AType argument)
        {
            AType result;
            CastInfo castInfo = new CastInfo(ATypes.AChar);

            switch (argument.Type)
            {
                case ATypes.AChar:
                    result = argument.Clone();
                    break;

                case ATypes.ANull:
                    result = Utils.ANull(ATypes.AChar);
                    break;

                case ATypes.ASymbol:
                    result = MonadicFunctionInstance.Unpack.Execute(argument);
                    break;

                case ATypes.AInteger:
                case ATypes.AFloat:
                    castInfo.Converter = new Converter(ConvertNumberToCharacter);

                    result = Walker(argument, castInfo);
                    break;

                default:
                    throw new Error.Domain(DomainErrorText);
            }

            return result;
        }
Exemple #34
0
        /// <summary>
        /// Creates the resulting AArray
        /// </summary>
        /// <param name="right"></param>
        /// <param name="left"></param>
        /// <returns>AArray containing the concatenated elements.</returns>
        private AType CreateResult(AType right, AType left)
        {
            AType result = AArray.Create(left.Type);
            // Create a clone from the arguments to avoid side effects
            AType clonedLeft = left.Clone();
            AType clonedRight = right.Clone();


            if (!clonedLeft.IsArray && !clonedRight.IsArray)
            {
                // example input: 1 , 2

                result.AddWithNoUpdate(clonedLeft);
                result.Add(clonedRight);
            }
            else if (clonedLeft.IsArray && !clonedRight.IsArray)
            {
                // example input: 1 2 , 3

                result.AddRange(clonedLeft);
                AType item = PrepareItem(clonedRight, clonedLeft);

                result.Add(item);
            }
            else if (!clonedLeft.IsArray && clonedRight.IsArray)
            {
                // example input: 1 , 2 3

                result.AddWithNoUpdate(PrepareItem(clonedLeft, clonedRight));
                result.AddRange(clonedRight);
            }
            // now both left and right argument is an AArray

            else if (clonedLeft.Rank == clonedRight.Rank)
            {
                // example input: (iota 2 2) , (iota 2 2)

                CheckItemsShape(clonedLeft, clonedRight);

                if (clonedLeft.Length > 0)
                {
                    result.AddRange(clonedLeft);
                }
                if (clonedRight.Length > 0)
                {
                    result.AddRange(clonedRight);
                }
            }
            else if ((clonedLeft.Rank - clonedRight.Rank) == 1)
            {
                // example input: (iota 2 2 2) , (iota 2 2)

                CheckShape(clonedLeft.Shape.GetRange(1, clonedLeft.Shape.Count - 1), clonedRight.Shape);

                result.AddRange(clonedLeft);
                result.Add(clonedRight);
            }
            else if ((clonedLeft.Rank - clonedRight.Rank) == -1)
            {
                // example input: (iota 2 2) , (iota 2 2 2)

                CheckShape(clonedLeft.Shape, clonedRight.Shape.GetRange(1, clonedRight.Shape.Count - 1));

                result.AddWithNoUpdate(clonedLeft);
                result.AddRange(clonedRight);
            }
            else
            {
                // The rank difference was bigger than 1.
                throw new Error.Rank(this.RankErrorText);
            }

            return result;
        }
Exemple #35
0
 public override AType Execute(AType argument, Aplus environment = null)
 {
     return(argument.IsMemoryMappedFile ? argument.Clone() : argument);
 }
Exemple #36
0
 protected virtual AType Process(AType argument)
 {
     return argument.Clone();
 }
Exemple #37
0
        private AType Compute(AType right, int dropCounter)
        {
            if (right.IsArray && dropCounter == 0)
            {
                return(right.Clone());
            }

            AType result = AArray.Create(ATypes.AType);

            if (right.IsArray)
            {
                if (right.Length - Math.Abs(dropCounter) > 0)
                {
                    if (dropCounter > 0)
                    {
                        for (int i = dropCounter; i < right.Length; i++)
                        {
                            result.AddWithNoUpdate(right[i].Clone());
                        }
                    }
                    else
                    {
                        for (int i = 0; i < right.Length + dropCounter; i++)
                        {
                            result.AddWithNoUpdate(right[i].Clone());
                        }
                    }
                    result.Length = right.Length - Math.Abs(dropCounter);
                    result.Shape  = new List <int>()
                    {
                        result.Length
                    };

                    if (right.Rank > 1)
                    {
                        result.Shape.AddRange(right.Shape.GetRange(1, right.Shape.Count - 1));
                    }

                    result.Rank = right.Rank;
                    result.Type = result[0].Type;
                }
                else
                {
                    result.Type = right.MixedType() ? ATypes.ANull : right.Type;
                }
            }
            else
            {
                if (dropCounter == 0)
                {
                    result.Add(right.Clone());
                    result.Length = 1;
                    result.Shape  = new List <int>()
                    {
                        1
                    };
                    result.Type = right.Type;
                }
                else
                {
                    result.Type = right.MixedType() ? ATypes.ANull : right.Type;
                }
            }
            return(result);
        }
Exemple #38
0
        private AType Compute(AType items, int desiredCount)
        {
            AType result = AArray.Create(items.Type);

            if (desiredCount > 0)
            {
                // Check how many we can copy from the right argument
                int count     = (desiredCount <= items.Length) ? desiredCount : items.Length;
                int remainder = desiredCount - count;

                for (int i = 0; i < count; i++)
                {
                    result.AddWithNoUpdate(items[i].Clone());
                }

                // Check if there is some leftover we need to fill
                if (remainder > 0)
                {
                    AType filler = FillElement(items);

                    for (; remainder > 0; remainder--)
                    {
                        result.AddWithNoUpdate(filler.Clone());
                    }
                }
            }
            else
            {
                // set the start point, which is the difference between the length of the items and
                //  the count we want to take from the end of the list
                // NOTE: + is used because in this case the 'desiredCount' variable is a negative number
                int start = items.Length + desiredCount;

                if (start < 0)
                {
                    // This case we need to add fill elements to the start of the array

                    AType filler = FillElement(items);
                    for (; start < 0; start++)
                    {
                        result.AddWithNoUpdate(filler.Clone());
                    }
                    // Now the 'start' is 0
                }

                for (; start < items.Length; start++)
                {
                    result.AddWithNoUpdate(items[start].Clone());
                }
            }

            result.Length = Math.Abs(desiredCount);
            result.Shape  = new List <int>()
            {
                result.Length
            };

            if (items.Rank > 1)
            {
                result.Shape.AddRange(items.Shape.GetRange(1, items.Shape.Count - 1));
            }
            result.Rank = items.Rank;

            if (desiredCount == 0)
            {
                result.Type = items.MixedType() ? ATypes.ANull : items.Type;
            }
            else
            {
                result.Type = result[0].Type;
            }

            return(result);
        }
Exemple #39
0
        /// <summary>
        /// If argument is:
        ///  - integer then we clone it.
        ///  - null we return integer null.
        ///  - float then we use ConvertFloatConstantToIntegerConstant function.
        ///  - char we call ConvertCharConstantToNumberConstant function.
        ///  - symbol then we use ConvertSymbolConstantToNumber function.
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        private AType IntegerCase(AType argument)
        {
            AType result;
            CastInfo castInfo = new CastInfo(ATypes.AInteger);

            switch (argument.Type)
            {
                case ATypes.AInteger:
                    result = argument.Clone();
                    break;

                case ATypes.ANull:
                    result = Utils.ANull(ATypes.AInteger);
                    break;

                case ATypes.AFloat:
                    castInfo.Converter = new Converter(ConvertToInteger);
                    result = Walker(argument, castInfo);
                    break;

                case ATypes.AChar:
                    castInfo.Converter = new Converter(ConvertCharacterToNumber);
                    result = Walker(argument, castInfo);
                    break;

                case ATypes.ASymbol:
                    if (!argument.SimpleSymbolArray())
                    {
                        throw new Error.Type(TypeErrorText);
                    }

                    DetectLongestSymbol(argument, castInfo);
                    castInfo.Converter = new Converter(ConvertSymbolConstantToNumber);
                    result = Walker(argument, castInfo);
                    break;

                default:
                    throw new Error.Domain(DomainErrorText);
            }

            return result;
        }
        private AType ConvertToFloat(AType argument)
        {
            AType result;
            int typeSize = GetTypeSize(ATypes.AFloat);

            switch (argument.Type)
            {
                case ATypes.AInteger:
                    result = ConvertToFloat(argument, typeSize, false);
                    break;
                case ATypes.AFloat:
                    result = argument.Clone();
                    break;
                case ATypes.AChar:
                    result = ConvertToFloat(argument, typeSize, false);
                    break;
                case ATypes.ASymbol:
                    result = ConvertToFloat(ConvertToInt(argument));
                    break;
                default:
                    throw new Error.Domain(this.DomainErrorText);
            }

            return result;
        }
        public override AType Execute(AType argument, Aplus environment = null)
        {
            if (argument.IsFunctionScalar)
            {
                return GetFunction(argument);
            }
            else if (argument.IsBox)
            {
                if (argument.IsArray)
                {
                    throw new Error.Rank(RankErrorText);
                }
                else
                {
                    throw new Error.Type(TypeErrorText);
                }
            }
            else if (argument.Type == ATypes.ANull)
            {
                throw new Error.Rank(RankErrorText);
            }
            else if (argument.Type == ATypes.AChar)
            {
                return argument.Clone();
            }

            // use Printing precision system variable
            int printingPrecision = (environment != null) ? environment.SystemVariables["pp"].asInteger : -1;

            switch (printingPrecision)
            {
                case -1:
                    // system variable doesn't exist, so we write whole float number.
                    printingPrecision = 0;
                    break;
                case 0:
                    // if `pp is 0, then it is reated as if it were one.
                    printingPrecision = 1;
                    break;
            }

            return Compute(argument, printingPrecision);
        }
Exemple #42
0
        /// <summary>
        /// Creates the resulting AArray
        /// </summary>
        /// <param name="right"></param>
        /// <param name="left"></param>
        /// <returns>AArray containing the concatenated elements.</returns>
        private AType CreateResult(AType right, AType left)
        {
            AType result = AArray.Create(left.Type);
            // Create a clone from the arguments to avoid side effects
            AType clonedLeft  = left.Clone();
            AType clonedRight = right.Clone();


            if (!clonedLeft.IsArray && !clonedRight.IsArray)
            {
                // example input: 1 , 2

                result.AddWithNoUpdate(clonedLeft);
                result.Add(clonedRight);
            }
            else if (clonedLeft.IsArray && !clonedRight.IsArray)
            {
                // example input: 1 2 , 3

                result.AddRange(clonedLeft);
                AType item = PrepareItem(clonedRight, clonedLeft);

                result.Add(item);
            }
            else if (!clonedLeft.IsArray && clonedRight.IsArray)
            {
                // example input: 1 , 2 3

                result.AddWithNoUpdate(PrepareItem(clonedLeft, clonedRight));
                result.AddRange(clonedRight);
            }
            // now both left and right argument is an AArray

            else if (clonedLeft.Rank == clonedRight.Rank)
            {
                // example input: (iota 2 2) , (iota 2 2)

                CheckItemsShape(clonedLeft, clonedRight);

                if (clonedLeft.Length > 0)
                {
                    result.AddRange(clonedLeft);
                }
                if (clonedRight.Length > 0)
                {
                    result.AddRange(clonedRight);
                }
            }
            else if ((clonedLeft.Rank - clonedRight.Rank) == 1)
            {
                // example input: (iota 2 2 2) , (iota 2 2)

                CheckShape(clonedLeft.Shape.GetRange(1, clonedLeft.Shape.Count - 1), clonedRight.Shape);

                result.AddRange(clonedLeft);
                result.Add(clonedRight);
            }
            else if ((clonedLeft.Rank - clonedRight.Rank) == -1)
            {
                // example input: (iota 2 2) , (iota 2 2 2)

                CheckShape(clonedLeft.Shape, clonedRight.Shape.GetRange(1, clonedRight.Shape.Count - 1));

                result.AddWithNoUpdate(clonedLeft);
                result.AddRange(clonedRight);
            }
            else
            {
                // The rank difference was bigger than 1.
                throw new Error.Rank(this.RankErrorText);
            }

            return(result);
        }
Exemple #43
0
        private static AType AppendItem(AType value, AType target, Aplus environment)
        {
            if (target.Rank == 0)
            {
                throw new Error.Rank("assign");
            }

            if (!Utils.DifferentNumberType(target, value) && target.Type != value.Type)
            {
                // The target and value are not numbers and they are of a different type?
                throw new Error.Type("assign");
            }

            if (target.Shape.SequenceEqual<int>(value.Shape))
            {
                for (int i = 0; i < value.Length; i++)
                {
                    target.Add(value[i].Clone());
                }
            }
            else if (target.Shape.GetRange(1, target.Shape.Count - 1).SequenceEqual<int>(value.Shape))
            {
                target.Add(value.Clone());
            }
            else if (value.Rank == 0)
            {
                AType item = DyadicFunctionInstance.Reshape.Execute(
                    value,
                    target.Shape.GetRange(1, target.Shape.Count - 1).ToAArray()
                );
                target.Add(item);
            }
            else if (value.Rank == target.Rank)
            {
                for (int i = 0; i < value.Length; i++)
                {
                    target.Add(value[i].Clone());
                }
            }
            else
            {
                throw new Error.Length("assign");
            }

            return value;
        }
        private AType ConvertToInt(AType argument)
        {
            AType result;

            switch (argument.Type)
            {
                case ATypes.AInteger:
                    result = argument.Clone();
                    break;
                case ATypes.AFloat:
                    result = ConvertFloatToInt(argument);
                    break;
                case ATypes.AChar:
                    result = ConvertCharToInt(argument, false);
                    break;
                case ATypes.ASymbol:
                    result = ConvertSymToInt(argument);
                    break;
                default:
                    throw new Error.Domain(this.DomainErrorText);
            }

            return result;
        }