Пример #1
0
        internal IMathMeta GET_INSTANCE_FOR(string keyword)
        {
            var hasOperatorforKeyword = SupportedOperators?.FirstOrDefault(c => c.ToString() == keyword);

            if (hasOperatorforKeyword != null)
            {
                return(hasOperatorforKeyword);
            }
            var hasFunction = SupportedFunctions?.FirstOrDefault(c => c.ToString() == keyword);

            if (hasFunction != null)
            {
                return(hasFunction);
            }
            var hasConstant = SupportedConstants?.FirstOrDefault(c => c.ToString() == keyword);

            if (hasConstant != null)
            {
                return(hasConstant);
            }
            if (keyword[0] != BaseInterpreter.NumberMarker ||
                (
                    !char.IsDigit(keyword[0]) &&
                    keyword[0] != BaseInterpreter.NumberMarker))
            {
                throw new Exception("Argument is not understood! Define " + keyword + "!");
            }
            return(NumberResult.Create(keyword));
        }
Пример #2
0
        public IMathMeta LexicalAnalysisInFixNotation(string expression, ref int position)
        {
            // Receive first char
            StringBuilder token = new StringBuilder();

            token.Append(expression[position]);

            IMathOperator @operator;

            if (CHECK_IF_OPERATOR(expression, ref position, out @operator))
            {
                return(@operator);
            }

            if (Char.IsLetter(expression[position]))
            {
                while (++position < expression.Length && Char.IsLetter(expression[position]))
                {
                    token.Append(expression[position]);
                }
            }

            if (char.IsLetter(token[0]) &&// if it is a function
                SupportedFunctions.Any(c => c.Keyword.StartsWith(token.ToString()))
                )
            {
                if (SupportedFunctions.Any(c => c.Keyword == token.ToString()))
                {
                    return(SupportedFunctions.FirstOrDefault(c => c.Keyword == token.ToString()));
                }
            }

            if (char.IsLetter(token[0]) &&
                SupportedConstants.Any(c => c.Keyword.StartsWith(token.ToString())))
            // if it is a constant
            {
                return(SupportedConstants.FirstOrDefault(c => c.Keyword == token.ToString()));
            }

            if (Char.IsDigit(token[0]) || token[0].ToString() == DecimalSeperatorAccordingToCurrentCulture)
            {
                // Read number
                // Read the whole part of number
                if (Char.IsDigit(token[0]))
                {
                    while (++position < expression.Length &&
                           Char.IsDigit(expression[position]))
                    {
                        token.Append(expression[position]);
                    }
                }
                else
                {
                    // Because system decimal separator
                    // will be added below
                    token.Clear();
                }

                // Read the fractional part of number
                if (position < expression.Length &&
                    expression[position].ToString() == DecimalSeperatorAccordingToCurrentCulture)
                {
                    // Add current system specific decimal separator
                    token.Append(DecimalSeperatorAccordingToCurrentCulture);

                    while (++position < expression.Length &&
                           Char.IsDigit(expression[position]))
                    {
                        token.Append(expression[position]);
                    }
                }

                // Read scientific notation (suffix)
                if (position + 1 < expression.Length && expression[position] == 'e' &&
                    (Char.IsDigit(expression[position + 1]) ||
                     (position + 2 < expression.Length &&
                      (expression[position + 1] == '+' ||
                       expression[position + 1] == '-') &&
                      Char.IsDigit(expression[position + 2]))))
                {
                    token.Append(expression[position++]); // e

                    if (expression[position] == '+' || expression[position] == '-')
                    {
                        token.Append(expression[position++]); // sign
                    }
                    while (position < expression.Length &&
                           Char.IsDigit(expression[position]))
                    {
                        token.Append(expression[position++]); // power
                    }

                    // Convert number from scientific notation to decimal notation
                }

                return(NumberResult.Create(token.ToString()));
            }
            else
            {
                throw new ArgumentException("Unknown token in expression");
            }
        }