Exemple #1
0
        private byte[] PrepareExpandVector(AType left)
        {
            // if the left side is User defined function, we throw Valence error.
            // this part belongs to Scan.
            if (left.Type == ATypes.AFunc)
            {
                throw new Error.Valence(ValenceErrorText);
            }

            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            //int element;
            AType scalar;

            byte[] expandVector;

            if (left.TryFirstScalar(out scalar, true))
            {
                expandVector = new byte[] { ExtractExpandArgument(scalar) };
            }
            else
            {
                expandVector = left.Select(item => ExtractExpandArgument(item)).ToArray();
            }

            return(expandVector);
        }
Exemple #2
0
        private byte[] PrepareExpandVector(AType left)
        {
            // if the left side is User defined function, we throw Valence error.
            // this part belongs to Scan.
            if (left.Type == ATypes.AFunc)
            {
                throw new Error.Valence(ValenceErrorText);
            }

            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            //int element;
            AType scalar;

            byte[] expandVector;

            if (left.TryFirstScalar(out scalar, true))
            {
                expandVector = new byte[] { ExtractExpandArgument(scalar) };
            }
            else
            {
                expandVector = left.Select(item => ExtractExpandArgument(item)).ToArray();
            }

            return expandVector;
        }
Exemple #3
0
        private static int GetLeadingAxesLength(AType argument)
        {
            if (!argument.IsNumber)
            {
                throw new Error.Type("Items");
            }

            AType result;

            if (!argument.TryFirstScalar(out result, true))
            {
                throw new Error.Length("Items");
            }


            int number;

            if (!result.ConvertToRestrictedWholeNumber(out number))
            {
                throw new Error.Type("Items");
            }

            if (number != -1 && number < 0)
            {
                throw new Error.Domain("Items");
            }

            return(number);
        }
        private int GetCutNumber(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(this.TypeErrorText);
            }
            
            AType scalar;
            int cutValue;

            // get the first scalar value with length check on
            if (!left.TryFirstScalar(out scalar, true))
            {
                throw new Error.Nonce(this.NonceErrorText);
            }

            // check if the scalar is a whole number and set the desired count of items
            if (!left.ConvertToRestrictedWholeNumber(out cutValue))
            {
                throw new Error.Type(this.TypeErrorText);
            }

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

            if (right.Rank == 0 && cutValue != 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            return cutValue;
        }
Exemple #5
0
        private static int GetLeadingAxesLength(AType argument)
        {
            if (!argument.IsNumber)
            {
                throw new Error.Type("Items");
            }

            AType result;
            if (!argument.TryFirstScalar(out result, true))
            {
                throw new Error.Length("Items");
            }


            int number;
            if (!result.ConvertToRestrictedWholeNumber(out number))
            {
                throw new Error.Type("Items");
            }

            if (number != -1 && number < 0)
            {
                throw new Error.Domain("Items");
            }

            return number;
        }
Exemple #6
0
        private int GetCutNumber(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(this.TypeErrorText);
            }

            AType scalar;
            int   cutValue;

            // get the first scalar value with length check on
            if (!left.TryFirstScalar(out scalar, true))
            {
                throw new Error.Nonce(this.NonceErrorText);
            }

            // check if the scalar is a whole number and set the desired count of items
            if (!left.ConvertToRestrictedWholeNumber(out cutValue))
            {
                throw new Error.Type(this.TypeErrorText);
            }

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

            if (right.Rank == 0 && cutValue != 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            return(cutValue);
        }
Exemple #7
0
        private int GetDropCounter(AType element)
        {
            if (element.Type == ATypes.AFloat || element.Type == ATypes.AInteger)
            {
                AType scalar;

                // Get the first scalar value with length check on
                if (!element.TryFirstScalar(out scalar, true))
                {
                    throw new Error.Nonce(this.NonceErrorText);
                }

                int dropCounter;
                // Check if the scalar is a whole number and set the drop counter
                if (!scalar.ConvertToRestrictedWholeNumber(out dropCounter))
                {
                    throw new Error.Type(this.TypeErrorText);
                }

                return dropCounter;
            }
            else if (element.Type == ATypes.ANull)
            {
                throw new Error.Nonce(this.NonceErrorText);
            }
            else
            {
                throw new Error.Type(this.TypeErrorText);
            }
        }
Exemple #8
0
        private int PrepareDesiredCount(AType element)
        {
            if (element.Type == ATypes.AFloat || element.Type == ATypes.AInteger)
            {
                AType scalar;

                // Get the first scalar value with length check on
                if (!element.TryFirstScalar(out scalar, true))
                {
                    throw new Error.Nonce(this.NonceErrorText);
                }

                int desiredCount;
                // Check if the scalar is a whole number and set the desired count of items
                if (!scalar.ConvertToRestrictedWholeNumber(out desiredCount))
                {
                    throw new Error.Type(this.TypeErrorText);
                }

                return(desiredCount);
            }
            else if (element.Type == ATypes.ANull)
            {
                throw new Error.Nonce(this.NonceErrorText);
            }
            else
            {
                throw new Error.Type(this.TypeErrorText);
            }
        }
Exemple #9
0
        private int[] PrepareRotateVector(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                // Allowed types are: AFloat, AInteger and ANull
                // otherwise throw Type error
                throw new Error.Type(TypeErrorText);
            }

            AType      scalar;
            List <int> rotateVector = new List <int>();

            if (left.TryFirstScalar(out scalar, true))
            {
                int result;

                if (!scalar.ConvertToRestrictedWholeNumber(out result))
                {
                    throw new Error.Type(TypeErrorText);
                }

                rotateVector.Add(result);
            }
            else
            {
                if (right.Rank > 0)
                {
                    if (left.Rank != right.Rank - 1)
                    {
                        throw new Error.Rank(RankErrorText);
                    }

                    if (!left.Shape.SequenceEqual(right.Shape.GetRange(1, right.Shape.Count - 1)))
                    {
                        throw new Error.Length(LengthErrorText);
                    }
                }

                // if the left is AArray and rank is bigger than 1, Ravel it
                AType leftvector = left.Rank > 1 ? MonadicFunctionInstance.Ravel.Execute(left) : left;

                int element;

                foreach (AType item in leftvector)
                {
                    if (!item.ConvertToRestrictedWholeNumber(out element))
                    {
                        throw new Error.Type(TypeErrorText);
                    }

                    rotateVector.Add(element);
                }
            }

            // if the right argument is scalar, we clone it!
            return(rotateVector.ToArray());
        }
Exemple #10
0
        private int[] PrepareRotateVector(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                // Allowed types are: AFloat, AInteger and ANull
                // otherwise throw Type error
                throw new Error.Type(TypeErrorText);
            }

            AType scalar;
            List<int> rotateVector = new List<int>();

            if (left.TryFirstScalar(out scalar, true))
            {
                int result;

                if (!scalar.ConvertToRestrictedWholeNumber(out result))
                {
                    throw new Error.Type(TypeErrorText);
                }

                rotateVector.Add(result);
            }
            else
            {
                if (right.Rank > 0)
                {
                    if (left.Rank != right.Rank - 1)
                    {
                        throw new Error.Rank(RankErrorText);
                    }

                    if (!left.Shape.SequenceEqual(right.Shape.GetRange(1, right.Shape.Count - 1)))
                    {
                        throw new Error.Length(LengthErrorText);
                    }
                }

                // if the left is AArray and rank is bigger than 1, Ravel it
                AType leftvector = left.Rank > 1 ? MonadicFunctionInstance.Ravel.Execute(left) : left;

                int element;

                foreach (AType item in leftvector)
                {
                    if (!item.ConvertToRestrictedWholeNumber(out element))
                    {
                        throw new Error.Type(TypeErrorText);
                    }

                    rotateVector.Add(element);
                }
            }

            // if the right argument is scalar, we clone it!
            return rotateVector.ToArray();
        }
Exemple #11
0
        /// <summary>
        /// Test if the input AType is not equals to 0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool BooleanTest(AType value)
        {
            AType scalar;
            int   number;

            if (value.TryFirstScalar(out scalar, true) && scalar.ConvertToRestrictedWholeNumber(out number))
            {
                return(number != 0);
            }

            throw new Error.Domain("Condition fail");
        }
Exemple #12
0
        internal static AType ConvertATypeListToAType(List <AType> list)
        {
            AType result;
            AType indexers = AArray.Create(ATypes.AArray);

            indexers.AddRange(list);

            if (!indexers.TryFirstScalar(out result, true))
            {
                result = indexers;
            }

            return(result);
        }
Exemple #13
0
        public AType GetTimeout(AType argument)
        {
            //atotv
            TimeSpan timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1)); // time since epoch
            AType    result;
            int      seconds;
            int      microseconds;
            int      nowSeconds      = Convert.ToInt32(Math.Floor(timestamp.TotalSeconds));
            int      nowMicroSeconds = 1000 * timestamp.Milliseconds;
            int      argumentLength  = argument.Length;

            if (argument.Type == ATypes.AFloat && argument.TryFirstScalar(out result, false))
            {
                seconds      = (int)Math.Floor(argument.asFloat);
                microseconds = 1000000 * (int)(argument.asFloat - Math.Floor(argument.asFloat));

                result = AArray.Create(
                    ATypes.AInteger,
                    AInteger.Create(nowSeconds + seconds),
                    AInteger.Create(nowMicroSeconds + microseconds),
                    AInteger.Create(1)
                    );
            }
            else if (argument.Type == ATypes.AInteger && argumentLength >= 1 && argumentLength <= 3)
            {
                if (argumentLength == 3 && argument[2].asInteger == 1 &&
                    argument[1].asInteger > 0)
                {
                    result = argument;
                }
                else
                {
                    seconds      = (argument.IsArray) ? argument[0].asInteger : argument.asInteger;
                    microseconds = (argumentLength >= 2) ? argument[1].asInteger * 1000 : 0;

                    result = AArray.Create(
                        ATypes.AInteger,
                        AInteger.Create(nowSeconds + seconds),
                        AInteger.Create(nowMicroSeconds + microseconds),
                        AInteger.Create(1)
                        );
                }
            }
            else
            {
                result = Utils.ANull();
            }

            return(result);
        }
Exemple #14
0
        /// <summary>
        /// Returns the integer extracted from the argument.
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="function"></param>
        /// <exception cref="Error.Type">If the argument is not a tolerably whole number.</exception>
        /// <exception cref="Error.Length">If the argument is not a one element array or scalar</exception>
        private static int ExtractInteger(AType argument, string function)
        {
            if (!argument.IsTolerablyWholeNumber)
            {
                throw new Error.Type(function);
            }

            AType result;

            if (!argument.TryFirstScalar(out result, true))
            {
                throw new Error.Length(function);
            }

            return(result.asInteger);
        }
Exemple #15
0
        private static bool ExtractString(AType argument, out string message)
        {
            AType symbol;
            if (argument.Type == ATypes.ASymbol && argument.TryFirstScalar(out symbol))
            {
                message = symbol.asString;
                return true;
            }
            else if (argument.Type == ATypes.AChar)
            {
                message = MonadicFunctionInstance.Ravel.Execute(argument).ToString();
                return true;
            }

            message = "";
            return false;
        }
Exemple #16
0
        private ReplicateJobInfo CreateReplicateJobInfo(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            int[] replicateVector;
            AType scalar;

            if (left.TryFirstScalar(out scalar, true))
            {
                replicateVector = new int[] { ExtractInteger(scalar) };
            }
            else
            {
                if (left.Length > 0)
                {
                    replicateVector = left.Select(item => ExtractInteger(item)).ToArray();
                }
                else
                {
                    replicateVector = new int[] { 0 };
                }

                // lenght check should be the first than parse the left side,
                // but the A+ follow that order.
                if (right.Length != 1 && left.Length != right.Length)
                {
                    throw new Error.Length(LengthErrorText);
                }
            }

            ReplicateJobInfo info = new ReplicateJobInfo(
                replicateVector,
                right.IsArray ? right : AArray.Create(right.Type, right)
            );

            return info;
        }
Exemple #17
0
        private ReplicateJobInfo CreateReplicateJobInfo(AType right, AType left)
        {
            if (!(left.Type == ATypes.AFloat || left.Type == ATypes.AInteger || left.Type == ATypes.ANull))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            int[] replicateVector;
            AType scalar;

            if (left.TryFirstScalar(out scalar, true))
            {
                replicateVector = new int[] { ExtractInteger(scalar) };
            }
            else
            {
                if (left.Length > 0)
                {
                    replicateVector = left.Select(item => ExtractInteger(item)).ToArray();
                }
                else
                {
                    replicateVector = new int[] { 0 };
                }

                // lenght check should be the first than parse the left side,
                // but the A+ follow that order.
                if (right.Length != 1 && left.Length != right.Length)
                {
                    throw new Error.Length(LengthErrorText);
                }
            }

            ReplicateJobInfo info = new ReplicateJobInfo(
                replicateVector,
                right.IsArray ? right : AArray.Create(right.Type, right)
                );

            return(info);
        }
Exemple #18
0
        private static bool ExtractString(AType argument, out string message)
        {
            AType symbol;

            if (argument.Type == ATypes.ASymbol && argument.TryFirstScalar(out symbol))
            {
                message = symbol.asString;
                return(true);
            }
            else if (argument.Type == ATypes.AChar)
            {
                message = MonadicFunctionInstance.Ravel.Execute(argument).ToString();
                return(true);
            }

            message = "";
            return(false);
        }
Exemple #19
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            // type check
            if (left.Type != ATypes.ASymbol)
            {
                throw new Error.Domain(DomainErrorText);
            }

            AType scalar;

            // get the first scalar value with length check on
            if (!left.TryFirstScalar(out scalar, true))
            {
                throw new Error.Domain(DomainErrorText);
            }

            AType result;

            // select the correspond function for convert
            switch (scalar.asString)
            {
            case "int":
                result = IntegerCase(right);
                break;

            case "float":
                result = FloatCase(right);
                break;

            case "sym":
                result = SymbolCase(right);
                break;

            case "char":
                result = CharCase(right);
                break;

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

            return(result);
        }
        public override AType Execute(AType right, AType left, Aplus environment)
        {
            // Environment is required!
            Assert.NotNull(environment);

            if (right.Type != ATypes.AChar)
            {
                throw new Error.Type(this.TypeErrorText);
            }

            if (right.Rank > 1)
            {
                throw new Error.Rank(this.RankErrorText);
            }

            string sourceCode = right.ToString();
            AType result;

            if(left.Type == ATypes.ASymbol)
            {
                AType symbol;
                if(left.TryFirstScalar(out symbol))
                {
                    result = ExecuteWithContextSwitch(environment, sourceCode, symbol.asString);
                }
                else
                {
                    result = ProtectedExecute(environment, sourceCode);
                }
            }
            else if (left.Type == ATypes.AInteger || left.Type == ATypes.ANull)
            {
                result = ProtectedExecute(environment, sourceCode);
            }
            else
            {
                throw new Error.Type(this.TypeErrorText);
            }

            return result;
        }
Exemple #21
0
        public override AType Execute(AType right, AType left, Aplus environment)
        {
            // Environment is required!
            Assert.NotNull(environment);

            if (right.Type != ATypes.AChar)
            {
                throw new Error.Type(this.TypeErrorText);
            }

            if (right.Rank > 1)
            {
                throw new Error.Rank(this.RankErrorText);
            }

            string sourceCode = right.ToString();
            AType  result;

            if (left.Type == ATypes.ASymbol)
            {
                AType symbol;
                if (left.TryFirstScalar(out symbol))
                {
                    result = ExecuteWithContextSwitch(environment, sourceCode, symbol.asString);
                }
                else
                {
                    result = ProtectedExecute(environment, sourceCode);
                }
            }
            else if (left.Type == ATypes.AInteger || left.Type == ATypes.ANull)
            {
                result = ProtectedExecute(environment, sourceCode);
            }
            else
            {
                throw new Error.Type(this.TypeErrorText);
            }

            return(result);
        }
Exemple #22
0
        internal static AType Exit(Aplus environment, AType argument)
        {
            AType result;
            bool isFirstScalar = argument.TryFirstScalar(out result, true);

            if (isFirstScalar && !result.IsTolerablyWholeNumber || 
                !(argument.Type == ATypes.AInteger || argument.Type == ATypes.AFloat))
            {
                throw new Error.Type("_exit");
            }

            if (!isFirstScalar)
            {
                throw new Error.Length("_exit");
            }

            Environment.Exit(result.asInteger);

            // unreachable code
            return argument;
        }
Exemple #23
0
        /// <summary>
        /// Get integer from argument.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        private int ExtractInteger(AType argument)
        {
            AType item;
            if (!argument.TryFirstScalar(out item, true))
            {
                throw new Error.Domain(DomainErrorText);
            }

            int number;
            if (!item.ConvertToRestrictedWholeNumber(out number))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (number < 0)
            {
                throw new Error.Domain(DomainErrorText);
            }

            return number;
        }
Exemple #24
0
        internal static AType Exit(Aplus environment, AType argument)
        {
            AType result;
            bool  isFirstScalar = argument.TryFirstScalar(out result, true);

            if (isFirstScalar && !result.IsTolerablyWholeNumber ||
                !(argument.Type == ATypes.AInteger || argument.Type == ATypes.AFloat))
            {
                throw new Error.Type("_exit");
            }

            if (!isFirstScalar)
            {
                throw new Error.Length("_exit");
            }

            Environment.Exit(result.asInteger);

            // unreachable code
            return(argument);
        }
Exemple #25
0
        internal static void PerformAssign(AType target, AType value)
        {
            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");
            }

            AType result;

            if (value.Length == 1 && value.TryFirstScalar(out result, true))
            {
                if (target.Length == 1)
                {
                    PerformIndexAssign(target, result);
                }
                else
                {
                    foreach (AType item in target)
                    {
                        PerformIndexAssign(item, result);
                    }
                }
            }
            else if (target.Length == value.Length)
            {
                for (int i = 0; i < target.Length; i++)
                {
                    PerformIndexAssign(target[i], value[i]);
                }
            }
            else if (target.Rank < value.Rank)
            {
                throw new Error.Rank("assign");
            }
            else
            {
                throw new Error.Length("assign");
            }
        }
Exemple #26
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            // type check
            if (left.Type != ATypes.ASymbol)
            {
                throw new Error.Domain(DomainErrorText);
            }

            AType scalar;

            // get the first scalar value with length check on
            if (!left.TryFirstScalar(out scalar, true))
            {
                throw new Error.Domain(DomainErrorText);
            }

            AType result;

            // select the correspond function for convert
            switch (scalar.asString)
            {
                case "int":
                    result = IntegerCase(right);
                    break;
                case "float":
                    result = FloatCase(right);
                    break;
                case "sym":
                    result = SymbolCase(right);
                    break;
                case "char":
                    result = CharCase(right);
                    break;
                default:
                    throw new Error.Domain(DomainErrorText);
            }

            return result;
        }
Exemple #27
0
        /// <summary>
        /// CASE 4: Simple right argument and simple left argument.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public AType SimpleNumeric2SimpleRight(AType left, AType right)
        {
            AType item;

            if (!left.TryFirstScalar(out item, true))
            {
                if (left.Rank > 1)
                {
                    throw new Error.Rank(RankErrorText);
                }

                if (left.Length > 1)
                {
                    throw new Error.Domain(DomainErrorText);
                }

                // Case 0
                if (left.Type == ATypes.ANull || left.Length == 0)
                {
                    return(right);
                }

                throw new Error.Mismatch("Unreachable Code!");
            }

            int index = item.asInteger;

            if (right.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            if (index < 0 || index >= right.Length)
            {
                throw new Error.Index(IndexErrorText);
            }

            return(right[index]);
        }
Exemple #28
0
        /// <summary>
        /// Get integer from argument.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        private int ExtractInteger(AType argument)
        {
            AType item;

            if (!argument.TryFirstScalar(out item, true))
            {
                throw new Error.Domain(DomainErrorText);
            }

            int number;

            if (!item.ConvertToRestrictedWholeNumber(out number))
            {
                throw new Error.Type(TypeErrorText);
            }

            if (number < 0)
            {
                throw new Error.Domain(DomainErrorText);
            }

            return(number);
        }
Exemple #29
0
        /// <summary>
        /// CASE 5: Nested vector right argument and simple path vector left argument.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        private AType PathVector2NestedVector(AType path, AType items, out bool resultFromBox)
        {
            AType result;
            AType element;

            if (path.TryFirstScalar(out element, true))
            {
                result = PathNumber2NestedVector(element, items);
            }
            else
            {
                if (path.Rank > 1)
                {
                    throw new Error.Rank(RankErrorText);
                }

                result = items;

                // Case 0
                if (path.Type == ATypes.ANull || path.Length == 0)
                {
                    resultFromBox = false;
                }
                else
                {
                    resultFromBox = true;
                    foreach (AType item in path)
                    {
                        result = PathNumber2NestedVector(item, result);
                    }
                }
            }
            resultFromBox = true;

            return(result);
        }
Exemple #30
0
        /// <summary>
        /// CASE 6: Nested right argument and nested left argument.
        /// </summary>
        /// <param name="pathBox"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        private AType NestedPathNumber2NestedArray(AType pathBox, AType items, Aplus environment)
        {
            AType path = MonadicFunctionInstance.Disclose.Execute(pathBox, environment);

            if (path.IsFunctionScalar)
            {
                throw new Error.Domain(DomainErrorText);
            }

            //Nested scalar or vector whose items are simple scalar or vector of integers.
            if (path.IsBox)
            {
                throw new Error.Type(TypeErrorText);
            }

            //Right argument is ().
            if (items.Type == ATypes.ANull)
            {
                throw new Error.Index(IndexErrorText);
            }

            if (path.Type == ATypes.ANull)
            {
                if (!items.IsBox)
                {
                    throw new Error.Domain(DomainErrorText);
                }

                AType result;

                if (!items.TryFirstScalar(out result, true))
                {
                    throw new Error.Domain(DomainErrorText);
                }

                return(MonadicFunctionInstance.Disclose.Execute(result, environment));
            }
            else if (path.Type == ATypes.AInteger)
            {
                if (path.Rank > 1)
                {
                    throw new Error.Rank(RankErrorText);
                }

                if (path.Length == 1)
                {
                    // Case 5
                    return(PathNumber2NestedVector(path.IsArray ? path[0] : path, items));
                }

                // The 'path' variable must be an AArray after this point.
                // so we treat it like that

                if (!items.IsBox)
                {
                    throw new Error.Domain(DomainErrorText);
                }

                //Length: 0
                if (path.Length != items.Rank || path.Length == 0)
                {
                    throw new Error.Rank(RankErrorText);
                }

                List <int> shape = items.Shape;
                int        index;

                for (int i = 0; i < path.Length; i++)
                {
                    index = path[i].asInteger;

                    if (index < 0 || index >= shape[i])
                    {
                        throw new Error.Index(IndexErrorText);
                    }

                    items = items[index];
                }

                return(MonadicFunctionInstance.Disclose.Execute(items, environment));
            }
            else if (path.Type == ATypes.ASymbol)
            {
                // Case 3
                return(ItemSelectWalker(SymbolConstant2SlotFillerDelegate, path, items, environment));
            }
            else if (path.Type == ATypes.AChar)
            {
                throw new Error.Domain(DomainErrorText);
            }
            else
            {
                throw new Error.Type(TypeErrorText);
            }
        }
        public AType SetAttribute(AType attribute, AType value)
        {
            AType toSet;
            bool tryFirstScalar = value.TryFirstScalar(out toSet, true);


            if (attribute.asString != "clientData")
            {
                if (value.Type != ATypes.AInteger || !tryFirstScalar)
                {
                    return AInteger.Create(-1);
                }
            }

            //if (value.Type != ATypes.AInteger && attribute.asString != "clientData")
            //{
            //    return AInteger.Create(-1);
            //}

            //if (!value.TryFirstScalar(out toSet, true) && attribute.asString != "clientData")
            //{
            //    return AInteger.Create(-1);
            //}

            AType result = AInteger.Create(0);

            switch (attribute.asString)
            {
                case "noDelay":
                    noDelay = (toSet.asInteger == 1);
                    connection.SetSocket();
                    break;
                case "readPause":
                    readPause = (toSet.asInteger == 1);
                    break;
                case "writePause":
                    writePause = (toSet.asInteger == 1);
                    break;
                case "readPriority":
                    readPriority = toSet.asInteger;
                    break;
                case "writePriority":
                    writePriority = toSet.asInteger;
                    break;
                case "readBufsize":
                    readBufsize = toSet.asInteger;
                    connection.SetSocket();
                    break;
                case "writeBufsize":
                    writeBufSize = toSet.asInteger;
                    connection.SetSocket();
                    break;
                case "retry":
                    retry = (toSet.asInteger == 1);
                    break;
                case "clientData":
                    clientData = value;
                    break;
                case "debug":
                    debug = (toSet.asInteger == 1);
                    break;
                case "burstMode":
                    burstMode = (toSet.asInteger == 1);
                    break;
                default:
                    result = AInteger.Create(-1);
                    break;
            }

            return result;
        }
Exemple #32
0
        public AType SetAttribute(AType attribute, AType value)
        {
            AType toSet;
            bool  tryFirstScalar = value.TryFirstScalar(out toSet, true);


            if (attribute.asString != "clientData")
            {
                if (value.Type != ATypes.AInteger || !tryFirstScalar)
                {
                    return(AInteger.Create(-1));
                }
            }

            //if (value.Type != ATypes.AInteger && attribute.asString != "clientData")
            //{
            //    return AInteger.Create(-1);
            //}

            //if (!value.TryFirstScalar(out toSet, true) && attribute.asString != "clientData")
            //{
            //    return AInteger.Create(-1);
            //}

            AType result = AInteger.Create(0);

            switch (attribute.asString)
            {
            case "noDelay":
                noDelay = (toSet.asInteger == 1);
                connection.SetSocket();
                break;

            case "readPause":
                readPause = (toSet.asInteger == 1);
                break;

            case "writePause":
                writePause = (toSet.asInteger == 1);
                break;

            case "readPriority":
                readPriority = toSet.asInteger;
                break;

            case "writePriority":
                writePriority = toSet.asInteger;
                break;

            case "readBufsize":
                readBufsize = toSet.asInteger;
                connection.SetSocket();
                break;

            case "writeBufsize":
                writeBufSize = toSet.asInteger;
                connection.SetSocket();
                break;

            case "retry":
                retry = (toSet.asInteger == 1);
                break;

            case "clientData":
                clientData = value;
                break;

            case "debug":
                debug = (toSet.asInteger == 1);
                break;

            case "burstMode":
                burstMode = (toSet.asInteger == 1);
                break;

            default:
                result = AInteger.Create(-1);
                break;
            }

            return(result);
        }
Exemple #33
0
        /// <summary>
        /// Returns the integer extracted from the argument.
        /// </summary>
        /// <param name="argument"></param>
        /// <param name="function"></param>
        /// <exception cref="Error.Type">If the argument is not a tolerably whole number.</exception>
        /// <exception cref="Error.Length">If the argument is not a one element array or scalar</exception>
        private static int ExtractInteger(AType argument, string function)
        {
            if (!argument.IsTolerablyWholeNumber)
            {
                throw new Error.Type(function);
            }

            AType result;

            if (!argument.TryFirstScalar(out result, true))
            {
                throw new Error.Length(function);
            }

            return result.asInteger;
        }
        public AType GetTimeout(AType argument)
        {
            //atotv
            TimeSpan timestamp = (DateTime.UtcNow - new DateTime(1970, 1, 1)); // time since epoch
            AType result;
            int seconds;
            int microseconds;
            int nowSeconds = Convert.ToInt32(Math.Floor(timestamp.TotalSeconds));
            int nowMicroSeconds = 1000 * timestamp.Milliseconds;
            int argumentLength = argument.Length;

            if (argument.Type == ATypes.AFloat && argument.TryFirstScalar(out result, false))
            {
                seconds = (int)Math.Floor(argument.asFloat);
                microseconds = 1000000 * (int)(argument.asFloat - Math.Floor(argument.asFloat));

                result = AArray.Create(
                    ATypes.AInteger,
                    AInteger.Create(nowSeconds + seconds),
                    AInteger.Create(nowMicroSeconds + microseconds),
                    AInteger.Create(1)
                );
            }
            else if (argument.Type == ATypes.AInteger && argumentLength >= 1 && argumentLength <= 3)
            {
                if (argumentLength == 3 && argument[2].asInteger == 1
                    && argument[1].asInteger > 0)
                {
                    result = argument;
                }
                else
                {
                    seconds = (argument.IsArray) ? argument[0].asInteger : argument.asInteger;
                    microseconds = (argumentLength >= 2) ? argument[1].asInteger * 1000 : 0;

                    result = AArray.Create(
                        ATypes.AInteger,
                        AInteger.Create(nowSeconds + seconds),
                        AInteger.Create(nowMicroSeconds + microseconds),
                        AInteger.Create(1)
                    );
                }
            }
            else
            {
                result = Utils.ANull();
            }

            return result;
        }
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            AType typeToCast;

            if (!left.TryFirstScalar(out typeToCast, true))
            {
                throw new Error.Domain(this.DomainErrorText);
            }

            int sourceTypeSize = GetTypeSize(right.Type);

            ATypes typeToConvert;
            ConverterFunction converterFunction;
            int destinationTypeSize;

            switch (typeToCast.asString)
            {
                case "int":
                    converterFunction = ConvertToInt;
                    typeToConvert = ATypes.AInteger;
                    break;
                case "float":
                    converterFunction = ConvertToFloat;
                    typeToConvert = ATypes.AFloat;
                    break;
                case "char":
                    converterFunction = ConvertToChar;
                    typeToConvert = ATypes.AChar;
                    break;
                default:
                    throw new Error.Domain(this.DomainErrorText);
            }
            
            destinationTypeSize = GetTypeSize(typeToConvert);
            int convertSizeRatio;
            double shapeModifier;

            if (sourceTypeSize >= destinationTypeSize)
            {
                convertSizeRatio = sourceTypeSize / destinationTypeSize;
                shapeModifier = sourceTypeSize / destinationTypeSize;
            }
            else
            {
                convertSizeRatio = destinationTypeSize / sourceTypeSize;

                // check if the last dimension of the right argument is convertable to the type specified in the left argument
                if (right.Shape.Count == 0 || right.Shape[right.Shape.Count - 1] % convertSizeRatio != 0)
                {
                    throw new Error.Length(this.LengthErrorText);
                }

                shapeModifier = destinationTypeSize / sourceTypeSize;
            }

            AType result;

            if (right.Shape.Contains(0))
            {
                List<int> desiredShape = new List<int>(right.Shape);
                int length = desiredShape.Count - 1;
                desiredShape[length] = (int)(desiredShape[length] * shapeModifier);
                AType reshapeShape = desiredShape.ToAArray();

                result = Utils.FillElement(typeToConvert, reshapeShape);
            }
            else
            {
                result = converterFunction(right);
            }

            return result;
        }
Exemple #36
0
        /// <summary>
        /// CASE 4: Simple right argument and simple left argument.
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public AType SimpleNumeric2SimpleRight(AType left, AType right)
        {
            AType item;

            if (!left.TryFirstScalar(out item, true))
            {
                if (left.Rank > 1)
                {
                    throw new Error.Rank(RankErrorText);
                }

                if (left.Length > 1)
                {
                    throw new Error.Domain(DomainErrorText);
                }

                // Case 0
                if (left.Type == ATypes.ANull || left.Length == 0)
                {
                    return right;
                }

                throw new Error.Mismatch("Unreachable Code!");
            }

            int index = item.asInteger;

            if (right.Rank > 1)
            {
                throw new Error.Rank(RankErrorText);
            }

            if (index < 0 || index >= right.Length)
            {
                throw new Error.Index(IndexErrorText);
            }

            return right[index];
        }
Exemple #37
0
        /// <summary>
        /// Test if the input AType is not equals to 0
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool BooleanTest(AType value)
        {
            AType scalar;
            int number;

            if (value.TryFirstScalar(out scalar, true) && scalar.ConvertToRestrictedWholeNumber(out number))
            {
                return (number != 0);
            }

            throw new Error.Domain("Condition fail");
        }
Exemple #38
0
        public override AType Execute(AType right, AType left, Aplus environment = null)
        {
            AType typeToCast;

            if (!left.TryFirstScalar(out typeToCast, true))
            {
                throw new Error.Domain(this.DomainErrorText);
            }

            int sourceTypeSize = GetTypeSize(right.Type);

            ATypes            typeToConvert;
            ConverterFunction converterFunction;
            int destinationTypeSize;

            switch (typeToCast.asString)
            {
            case "int":
                converterFunction = ConvertToInt;
                typeToConvert     = ATypes.AInteger;
                break;

            case "float":
                converterFunction = ConvertToFloat;
                typeToConvert     = ATypes.AFloat;
                break;

            case "char":
                converterFunction = ConvertToChar;
                typeToConvert     = ATypes.AChar;
                break;

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

            destinationTypeSize = GetTypeSize(typeToConvert);
            int    convertSizeRatio;
            double shapeModifier;

            if (sourceTypeSize >= destinationTypeSize)
            {
                convertSizeRatio = sourceTypeSize / destinationTypeSize;
                shapeModifier    = sourceTypeSize / destinationTypeSize;
            }
            else
            {
                convertSizeRatio = destinationTypeSize / sourceTypeSize;

                // check if the last dimension of the right argument is convertable to the type specified in the left argument
                if (right.Shape.Count == 0 || right.Shape[right.Shape.Count - 1] % convertSizeRatio != 0)
                {
                    throw new Error.Length(this.LengthErrorText);
                }

                shapeModifier = destinationTypeSize / sourceTypeSize;
            }

            AType result;

            if (right.Shape.Contains(0))
            {
                List <int> desiredShape = new List <int>(right.Shape);
                int        length       = desiredShape.Count - 1;
                desiredShape[length] = (int)(desiredShape[length] * shapeModifier);
                AType reshapeShape = desiredShape.ToAArray();

                result = Utils.FillElement(typeToConvert, reshapeShape);
            }
            else
            {
                result = converterFunction(right);
            }

            return(result);
        }
Exemple #39
0
        internal static void PerformAssign(AType target, AType value)
        {
            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");
            }

            AType result;

            if (value.Length == 1 && value.TryFirstScalar(out result, true))
            {
                if (target.Length == 1)
                {
                    PerformIndexAssign(target, result);
                }
                else
                {
                    foreach (AType item in target)
                    {
                        PerformIndexAssign(item, result);
                    }
                }
            }
            else if (target.Length == value.Length)
            {
                for (int i = 0; i < target.Length; i++)
                {
                    PerformIndexAssign(target[i], value[i]);
                }
            }
            else if (target.Rank < value.Rank)
            {
                throw new Error.Rank("assign");
            }
            else
            {
                throw new Error.Length("assign");
            }
        }
Exemple #40
0
        private AType SolveEquation(AType constants, AType equations)
        {
            AType lhs;
            AType rhs;

            if (constants.TryFirstScalar(out lhs, true) && equations.TryFirstScalar(out rhs, true))
            {
                // both left and right values are one element arrays.
                return AFloat.Create(lhs.asFloat / rhs.asFloat);
            }

            Matrix constantsArray = new SimpleMatrix(ExtractConstants(constants));
            Matrix originalEquations = new SimpleMatrix(FloatFromAType(equations));
            int[] rowsSequence;
            Matrix eliminatedConstants;
            GaussianElimination(originalEquations, constantsArray, out rowsSequence, out eliminatedConstants);

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

            if (equations.Shape[0] == equations.Shape[1])
            {
                // square equation
                if (constants.Rank > 1)
                {
                    foreach (int item in rowsSequence)
                    {
                        AType subArray = AArray.Create(ATypes.AFloat);

                        for (int i = 0; i < eliminatedConstants.Columns; i++)
                        {
                            subArray.Add(AFloat.Create(eliminatedConstants[item, i]));
                        }

                        result.Add(subArray);
                    }
                }
                else
                {
                    foreach (int item in rowsSequence)
                    {
                        result.Add(AFloat.Create(eliminatedConstants[item, 0]));
                    }
                }
            }
            else
            {
                double[][] independentConstants = BuildIndependentConstants(rowsSequence, eliminatedConstants);
                double[] beta;
                double[] actualconstants;

                if (constants.Rank == 1)
                {
                    beta = independentConstants.Select(item => item[0]).ToArray();
                    actualconstants = constants.Select(item => item.asFloat).ToArray();

                    double[] solution = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);

                    foreach (double item in solution)
                    {
                        result.Add(AFloat.Create(item));
                    }
                }
                else
                {
                    for (int objective = 0; objective < constants.Shape[1]; objective++)
                    {
                        beta = independentConstants.Select(item => item[objective]).ToArray();
                        actualconstants = constants.Select(item => item[objective].asFloat).ToArray();

                        double[] solution = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);
                        AType solutionArray = AArray.Create(ATypes.AFloat);

                        foreach (double item in solution)
                        {
                            solutionArray.Add(AFloat.Create(item));
                        }

                        result.Add(solutionArray);
                    }
                }
            }

            return result;
        }
Exemple #41
0
        private AType SolveEquation(AType constants, AType equations)
        {
            AType lhs;
            AType rhs;

            if (constants.TryFirstScalar(out lhs, true) && equations.TryFirstScalar(out rhs, true))
            {
                // both left and right values are one element arrays.
                return(AFloat.Create(lhs.asFloat / rhs.asFloat));
            }

            Matrix constantsArray    = new SimpleMatrix(ExtractConstants(constants));
            Matrix originalEquations = new SimpleMatrix(FloatFromAType(equations));

            int[]  rowsSequence;
            Matrix eliminatedConstants;

            GaussianElimination(originalEquations, constantsArray, out rowsSequence, out eliminatedConstants);

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

            if (equations.Shape[0] == equations.Shape[1])
            {
                // square equation
                if (constants.Rank > 1)
                {
                    foreach (int item in rowsSequence)
                    {
                        AType subArray = AArray.Create(ATypes.AFloat);

                        for (int i = 0; i < eliminatedConstants.Columns; i++)
                        {
                            subArray.Add(AFloat.Create(eliminatedConstants[item, i]));
                        }

                        result.Add(subArray);
                    }
                }
                else
                {
                    foreach (int item in rowsSequence)
                    {
                        result.Add(AFloat.Create(eliminatedConstants[item, 0]));
                    }
                }
            }
            else
            {
                double[][] independentConstants = BuildIndependentConstants(rowsSequence, eliminatedConstants);
                double[]   beta;
                double[]   actualconstants;

                if (constants.Rank == 1)
                {
                    beta            = independentConstants.Select(item => item[0]).ToArray();
                    actualconstants = constants.Select(item => item.asFloat).ToArray();

                    double[] solution = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);

                    foreach (double item in solution)
                    {
                        result.Add(AFloat.Create(item));
                    }
                }
                else
                {
                    for (int objective = 0; objective < constants.Shape[1]; objective++)
                    {
                        beta            = independentConstants.Select(item => item[objective]).ToArray();
                        actualconstants = constants.Select(item => item[objective].asFloat).ToArray();

                        double[] solution      = OverDeterminedEquationSolve(beta, actualconstants, originalEquations);
                        AType    solutionArray = AArray.Create(ATypes.AFloat);

                        foreach (double item in solution)
                        {
                            solutionArray.Add(AFloat.Create(item));
                        }

                        result.Add(solutionArray);
                    }
                }
            }

            return(result);
        }
Exemple #42
0
        /// <summary>
        /// CASE 6: Nested right argument and nested left argument.
        /// </summary>
        /// <param name="pathBox"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        private AType NestedPathNumber2NestedArray(AType pathBox, AType items, Aplus environment)
        {
            AType path = MonadicFunctionInstance.Disclose.Execute(pathBox, environment);

            if (path.IsFunctionScalar)
            {
                throw new Error.Domain(DomainErrorText);
            }

            //Nested scalar or vector whose items are simple scalar or vector of integers.
            if (path.IsBox)
            {
                throw new Error.Type(TypeErrorText);
            }

            //Right argument is ().
            if (items.Type == ATypes.ANull)
            {
                throw new Error.Index(IndexErrorText);
            }

            if (path.Type == ATypes.ANull)
            {
                if (!items.IsBox)
                {
                    throw new Error.Domain(DomainErrorText);
                }

                AType result;

                if (!items.TryFirstScalar(out result, true))
                {
                    throw new Error.Domain(DomainErrorText);
                }

                return MonadicFunctionInstance.Disclose.Execute(result, environment);
            }
            else if (path.Type == ATypes.AInteger)
            {
                if (path.Rank > 1)
                {
                    throw new Error.Rank(RankErrorText);
                }

                if (path.Length == 1)
                {
                    // Case 5
                    return PathNumber2NestedVector(path.IsArray ? path[0] : path, items);
                }

                // The 'path' variable must be an AArray after this point.
                // so we treat it like that

                if (!items.IsBox)
                {
                    throw new Error.Domain(DomainErrorText);
                }

                //Length: 0
                if (path.Length != items.Rank || path.Length == 0)
                {
                    throw new Error.Rank(RankErrorText);
                }

                List<int> shape = items.Shape;
                int index;

                for (int i = 0; i < path.Length; i++)
                {
                    index = path[i].asInteger;

                    if (index < 0 || index >= shape[i])
                    {
                        throw new Error.Index(IndexErrorText);
                    }

                    items = items[index];
                }

                return MonadicFunctionInstance.Disclose.Execute(items, environment);
            }
            else if (path.Type == ATypes.ASymbol)
            {
                // Case 3
                return ItemSelectWalker(SymbolConstant2SlotFillerDelegate, path, items, environment);
            }
            else if (path.Type == ATypes.AChar)
            {
                throw new Error.Domain(DomainErrorText);
            }
            else
            {
                throw new Error.Type(TypeErrorText);
            }
        }
Exemple #43
0
        /// <summary>
        /// CASE 5: Nested vector right argument and simple path vector left argument.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="items"></param>
        /// <returns></returns>
        private AType PathVector2NestedVector(AType path, AType items, out bool resultFromBox)
        {
            AType result;
            AType element;

            if (path.TryFirstScalar(out element, true))
            {
                result = PathNumber2NestedVector(element, items);
            }
            else
            {
                if (path.Rank > 1)
                {
                    throw new Error.Rank(RankErrorText);
                }

                result = items;

                // Case 0
                if (path.Type == ATypes.ANull || path.Length == 0)
                {
                    resultFromBox = false;
                }
                else
                {    
                    resultFromBox = true;
                    foreach (AType item in path)
                    {
                        result = PathNumber2NestedVector(item, result);
                    }
                }
            }
            resultFromBox = true;

            return result;
        }