示例#1
0
        /// <summary>
        /// Perform type and length check for the arguments
        /// </summary>
        /// <param name="left">Format info.</param>
        /// <param name="right">Items to format.</param>
        private void ValidateInput(AType left, AType right)
        {
            bool typeOK = (left.IsNumber || left.Type == ATypes.ANull)
                && (right.IsNumber || right.Type == ATypes.ASymbol || right.Type == ATypes.ANull);

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

            if (right.Type == ATypes.ASymbol && !right.SimpleSymbolArray())
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.IsArray)
            {
                int length = left.Shape.Product();

                if (right.Shape.Count == 0 || length != right.Shape[right.Rank - 1])
                {
                    throw new Error.Length(LengthErrorText);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Perform type and length check for the arguments
        /// </summary>
        /// <param name="left">Format info.</param>
        /// <param name="right">Items to format.</param>
        private void ValidateInput(AType left, AType right)
        {
            bool typeOK = (left.IsNumber || left.Type == ATypes.ANull) &&
                          (right.IsNumber || right.Type == ATypes.ASymbol || right.Type == ATypes.ANull);

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

            if (right.Type == ATypes.ASymbol && !right.SimpleSymbolArray())
            {
                throw new Error.Type(TypeErrorText);
            }

            if (left.IsArray)
            {
                int length = left.Shape.Product();

                if (right.Shape.Count == 0 || length != right.Shape[right.Rank - 1])
                {
                    throw new Error.Length(LengthErrorText);
                }
            }
        }
示例#3
0
        internal static void CheckArgument <T>(AType argument) where T : class
        {
            if (!argument.SimpleSymbolArray())
            {
                throw new Error.Type(typeof(T).Name);
            }

            if (argument.Rank != 0)
            {
                throw new Error.Rank(typeof(T).Name);
            }
        }
示例#4
0
        /// <summary>
        /// Checks and call convert algorithm.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        private AType Compute(AType argument)
        {
            if (!argument.SimpleSymbolArray())
            {
                throw new Error.Type(TypeErrorText);
            }

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

            int lastDimension = DetermineLastDimension(argument);

            return CreateCharArray(argument, lastDimension);
        }
示例#5
0
        /// <summary>
        /// Checks and call convert algorithm.
        /// </summary>
        /// <param name="argument"></param>
        /// <returns></returns>
        private AType Compute(AType argument)
        {
            if (!argument.SimpleSymbolArray())
            {
                throw new Error.Type(TypeErrorText);
            }

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

            int lastDimension = DetermineLastDimension(argument);

            return(CreateCharArray(argument, lastDimension));
        }
示例#6
0
 /// <summary>
 /// If argument made up by function scalar and symbol the result is true.
 /// </summary>
 /// <param name="argument"></param>
 /// <returns></returns>
 public static bool MixedSimpleArray(this AType argument)
 {
     if (argument.IsArray)
     {
         foreach (AType item in argument)
         {
             if (!MixedSimpleArray(item))
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         return(argument.IsFunctionScalar || argument.SimpleSymbolArray());
     }
 }
示例#7
0
        public static AType Assign(AType target, AType value, Aplus environment)
        {
            // Environment is required!
            Assert.NotNull(environment);

            if ((!target.SimpleSymbolArray()) || (target.Rank != 0))
            {
                throw new Error.Domain("assign");
            }

            // Get the context parts, (context, variablename) string pairs
            string[] contextParts = VariableHelper.CreateContextParts(environment.CurrentContext, target.asString);

            // Build the method
            Func <AType> method = VariableHelper.BuildVariableAssignMethod(environment, contextParts, value).Compile();

            return(method());
        }
示例#8
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);
        }
示例#10
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);
        }
示例#11
0
        public static bool IsSlotFiller(this AType vector, bool extended = false)
        {
            // Vector must be box array.
            if (!vector.IsArray)
            {
                return(false);
            }

            AType ravelVector = vector.Rank > 1 ? MonadicFunctionInstance.Ravel.Execute(vector) : vector;

            // Form (symbol;value)
            if (vector.Length != 2)
            {
                return(false);
            }

            // Symbol and value must be box.
            if (!ravelVector[0].IsBox || !ravelVector[0].IsBox)
            {
                return(false);
            }

            AType symbol = MonadicFunctionInstance.Disclose.Execute(ravelVector[0]);
            AType value  = MonadicFunctionInstance.Disclose.Execute(ravelVector[1]);

            // calculate symbols' size
            int symbolLength = CalculateLength(symbol, extended);

            // calculate values' size
            int valueLength = CalculateLength(value, extended);

            // symbol is not a Symbol or symbol and value has same length
            if (symbol.Type != ATypes.ASymbol || symbolLength != valueLength)
            {
                return(false);
            }

            if (!extended)
            {
                if (!symbol.SimpleSymbolArray())
                {
                    return(false);
                }

                if (symbol.IsArray)
                {
                    // If symbol is an array, it must contain distinct symbols.
                    for (int i = 0; i < symbol.Length; i++)
                    {
                        for (int j = 0; j < symbol.Length; j++)
                        {
                            if (i != j && symbol[i].CompareTo(symbol[j]) == 0)
                            {
                                // Found a duplicate symbol
                                return(false);
                            }
                        }
                    }
                }
            }

            if (extended)
            {
                return(value.Type == ATypes.ABox || value.Type == ATypes.AFunc);
            }
            else
            {
                if (!value.IsBox)
                {
                    return(false);
                }

                if (value.IsArray)
                {
                    // Value is a vector.
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (value[i].IsPrimitiveFunction())
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
                else
                {
                    return(!value.IsPrimitiveFunction());
                }
            }
        }
示例#12
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;
        }