Exemplo n.º 1
0
        private bool IsUnaryOperator(string current, string?prev, string?next)
        {
            IMathContext context = Context;

            if (!context.TryGetUnaryOperator(current, out var op))
            {
                return(false);
            }

            if (op.Notation == OperatorNotation.Prefix)
            {
                if (prev != null && (prev == ")" ||
                                     IsNumber(prev) ||
                                     context.IsVariableOrConstant(prev) ||
                                     (context.IsUnaryOperator(prev) && !context.IsBinaryOperator(prev))))
                {
                    return(false);
                }

                return(next != null && !IsValidOperator(next[0]) && (next == "(" || char.IsLetterOrDigit(next[0])));
            }
            else if (op.Notation == OperatorNotation.Postfix)
            {
                return(prev != null && (prev == ")" || IsNumber(prev) || context.IsVariableOrConstant(prev)));
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        private Token[] GetTokensInternal(string[] arr)
        {
            if (arr.Length == 0)
            {
                return(Array.Empty <Token>());
            }

            List <Token> tokens  = new List <Token>(arr.Length);
            IMathContext context = Context;
            int          length  = arr.Length;

            for (int i = 0; i < length; i++)
            {
                string current = arr[i];

                if (IsNumber(current))
                {
                    tokens.Add(new Token(current, TokenType.Number));
                }
                else if (context.IsVariableOrConstant(current))
                {
                    tokens.Add(new Token(current, TokenType.VariableOrConstant));
                }
                else if (context.IsFunction(current))
                {
                    tokens.Add(new Token(current, TokenType.Function));
                }
                else if (context.IsBinaryOperator(current) || context.IsUnaryOperator(current))
                {
                    string?prev = i > 0 ? arr[i - 1] : null;
                    string?next = i < length - 1 ? arr[i + 1] : null;

                    if (IsUnaryOperator(current, prev, next))
                    {
                        tokens.Add(new Token(current, TokenType.UnaryOperator));
                    }
                    else
                    {
                        tokens.Add(new Token(current, TokenType.BinaryOperator));
                    }
                }
                else if (current == "(" || current == ")")
                {
                    tokens.Add(Token.FromParenthesis(current[0]));
                }
                else if (current == ",")
                {
                    tokens.Add(Token.Comma);
                }
                else
                {
                    tokens.Add(new Token(current, TokenType.Unknown));
                }
            }

            return(tokens.ToArray());
        }