public PostFixExpression(InfixExpression expression)
 {
     _expression = Infix2Postfix(expression.Expression);
     _tokens = _expression.Split(new[] {' '});
     CheckPostVectorForEvaluability(Tokens);
 }
        /// <summary>
        /// Convecs an infix string to a post fix string.
        /// </summary>
        /// <remarks><pre>
        /// 2004-07-19 - Jeremy Roberts
        /// </pre></remarks>
        /// <param name="func">The function to convert</param>
        /// <returns>A post fix string.</returns>
        protected string Infix2Postfix(string func)
        {
            func = new InfixExpression(func).Expression;

            string[] inFix = func.Split(new[] {' '});

            var postFix = new Stack<string>();
            var operators = new Stack<string>();

            string currOperator;

            foreach (string token in inFix) {
                if (ExpressionKeywords.IsOperand(token)) {
                    postFix.Push(token);
                }
                else {
                    if (ExpressionKeywords.OpenGroupOperators.Contains(token)) {
                        operators.Push(token);
                    }
                    else {
                        if (ExpressionKeywords.ClosingGroupOperators.Contains(token)) {
                            Grouping g = ExpressionKeywords.GetGroupingFromClose(token);
                            currOperator = operators.Pop();

                            while (currOperator != g.Open) {
                                postFix.Push(currOperator);
                                currOperator = operators.Pop();
                            }
                        }
                        else {
                            if (ExpressionKeywords.IsOperator(token)) {
                                // while precedence of the operator is <= precedence of the token
                                while (operators.Count > 0) {
                                    if (ExpressionKeywords.GetPrecedence(token)
                                        <= ExpressionKeywords.GetPrecedence(operators.Peek())) {
                                        currOperator = operators.Pop();
                                        postFix.Push(currOperator);
                                    }
                                    else {
                                        break;
                                    }
                                }

                                operators.Push(token);
                            }
                        }
                    }
                }
            }
            while (operators.Count > 0) {
                currOperator = operators.Pop();
                postFix.Push(currOperator);
            }

            // Build the post fix string.
            string psString = string.Empty;
            foreach (string item in postFix) {
                psString = item + " " + psString;
            }
            psString = psString.Trim();

            return psString;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convecs an infix string to a post fix string.
        /// </summary>
        /// <remarks><pre>
        /// 2004-07-19 - Jeremy Roberts
        /// </pre></remarks>
        /// <param name="func">The function to convert</param>
        /// <returns>A post fix string.</returns>
        protected string Infix2Postfix(string func)
        {
            func = new InfixExpression(func).Expression;

            string[] inFix = func.Split(new[] { ' ' });

            var postFix   = new Stack <string>();
            var operators = new Stack <string>();

            string currOperator;

            foreach (string token in inFix)
            {
                if (ExpressionKeywords.IsOperand(token))
                {
                    postFix.Push(token);
                }
                else if (ExpressionKeywords.OpenGroupOperators.Contains(token))
                {
                    if (operators.Count > 0)
                    {
                        var kw = ExpressionKeywords.Keywords.OfType <Procedure>()
                                 .Where(x => x.Name == operators.Peek())
                                 .Select(x => x)
                                 .SingleOrDefault();
                        if (kw != null &&
                            kw.VariableOperandsCount)
                        {
                            postFix.Push("param_terminator");
                        }
                    }
                    operators.Push(token);
                }
                else if (ExpressionKeywords.ClosingGroupOperators.Contains(token))
                {
                    Grouping g = ExpressionKeywords.GetGroupingFromClose(token);
                    currOperator = operators.Pop();

                    while (currOperator != g.Open)
                    {
                        postFix.Push(currOperator);
                        currOperator = operators.Pop();
                    }
                    if (operators.Count > 0 &&
                        ExpressionKeywords.Functions.Contains(operators.Peek()))
                    {
                        postFix.Push(operators.Pop());
                    }
                }
                else if (ExpressionKeywords.IsOperator(token))
                {
                    // while precedence of the operator is <= precedence of the token
                    while (operators.Count > 0)
                    {
                        if (ExpressionKeywords.GetPrecedence(token)
                            <= ExpressionKeywords.GetPrecedence(operators.Peek()))
                        {
                            currOperator = operators.Pop();
                            postFix.Push(currOperator);
                        }
                        else
                        {
                            break;
                        }
                    }

                    operators.Push(token);
                }
            }
            while (operators.Count > 0)
            {
                currOperator = operators.Pop();
                postFix.Push(currOperator);
            }

            // Build the post fix string.
            string psString = string.Empty;

            foreach (string item in postFix)
            {
                psString = item + " " + psString;
            }
            psString = psString.Trim();

            return(psString);
        }
Exemplo n.º 4
0
 public PostFixExpression(InfixExpression expression)
 {
     _expression = Infix2Postfix(expression.Expression);
     _tokens     = _expression.Split(new[] { ' ' });
     CheckPostVectorForEvaluability(Tokens);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Clears all information.
 /// </summary>
 /// <remarks><pre>
 /// 2004-07-19 - Jeremy Roberts
 /// </pre></remarks>
 public void Clear()
 {
     _inFunction   = null;
     _postFunction = null;
     _variables.Clear();
 }
        /// <summary>
        /// Convecs an infix string to a post fix string.
        /// </summary>
        /// <remarks><pre>
        /// 2004-07-19 - Jeremy Roberts
        /// </pre></remarks>
        /// <param name="func">The function to convert</param>
        /// <returns>A post fix string.</returns>
        protected string Infix2Postfix(string func)
        {
            func = new InfixExpression(func).Expression;

            string[] inFix = func.Split(new[] { ' ' });

            var postFix   = new Stack <string>();
            var operators = new Stack <string>();

            string currOperator;

            foreach (string token in inFix)
            {
                if (ExpressionKeywords.IsOperand(token))
                {
                    postFix.Push(token);
                }
                else if (ExpressionKeywords.OpenGroupOperators.Contains(token))
                {
                    operators.Push(token);
                }
                else if (ExpressionKeywords.ClosingGroupOperators.Contains(token))
                {
                    Grouping g = ExpressionKeywords.GetGroupingFromClose(token);
                    currOperator = operators.Pop();

                    while (currOperator != g.Open)
                    {
                        postFix.Push(currOperator);
                        currOperator = operators.Pop();
                    }
                }
                else if (ExpressionKeywords.IsOperator(token))
                {
                    // while precedence of the operator is <= precedence of the token
                    while (operators.Count > 0)
                    {
                        if (ExpressionKeywords.GetPrecedence(token) <= ExpressionKeywords.GetPrecedence(operators.Peek()))
                        {
                            currOperator = operators.Pop();
                            postFix.Push(currOperator);
                        }
                        else
                        {
                            break;
                        }
                    }

                    operators.Push(token);
                }
            }
            while (operators.Count > 0)
            {
                currOperator = operators.Pop();
                postFix.Push(currOperator);
            }

            // Build the post fix string.
            string psString = string.Empty;

            foreach (string item in postFix)
            {
                psString = item + " " + psString;
            }
            psString = psString.Trim();

            return(psString);
        }
        /// <summary>
        /// Convecs an infix string to a post fix string.
        /// </summary>
        /// <remarks><pre>
        /// 2004-07-19 - Jeremy Roberts
        /// </pre></remarks>
        /// <param name="func">The function to convert</param>
        /// <returns>A post fix string.</returns>
        protected string Infix2Postfix(string func)
        {
            func = new InfixExpression(func).Expression;

            string[] inFix = func.Split(new[] {' '});

            var postFix = new Stack<string>();
            var operators = new Stack<string>();

            string currOperator;

            foreach (string token in inFix) {
                if (ExpressionKeywords.IsOperand(token)) {
                    postFix.Push(token);
                }
                else if (ExpressionKeywords.OpenGroupOperators.Contains(token)) {
                    if (operators.Count > 0) {
                        var kw = ExpressionKeywords.Keywords.OfType<Procedure>()
                            .Where(x => x.Name == operators.Peek())
                            .Select(x => x)
                            .SingleOrDefault();
                        if (kw != null
                            && kw.VariableOperandsCount) {
                            postFix.Push("param_terminator");
                        }
                    }
                    operators.Push(token);
                }
                else if (ExpressionKeywords.ClosingGroupOperators.Contains(token)) {
                    Grouping g = ExpressionKeywords.GetGroupingFromClose(token);
                    currOperator = operators.Pop();

                    while (currOperator != g.Open) {
                        postFix.Push(currOperator);
                        currOperator = operators.Pop();
                    }
                    if (operators.Count > 0
                        && ExpressionKeywords.Functions.Contains(operators.Peek())) {
                        postFix.Push(operators.Pop());
                    }
                }
                else if (ExpressionKeywords.IsOperator(token)) {
                    // while precedence of the operator is <= precedence of the token
                    while (operators.Count > 0) {
                        if (ExpressionKeywords.GetPrecedence(token)
                            <= ExpressionKeywords.GetPrecedence(operators.Peek())) {
                            currOperator = operators.Pop();
                            postFix.Push(currOperator);
                        }
                        else {
                            break;
                        }
                    }

                    operators.Push(token);
                }
            }
            while (operators.Count > 0) {
                currOperator = operators.Pop();
                postFix.Push(currOperator);
            }

            // Build the post fix string.
            string psString = string.Empty;
            foreach (string item in postFix) {
                psString = item + " " + psString;
            }
            psString = psString.Trim();

            return psString;
        }