Ejemplo n.º 1
0
        public static List <string> getReversePolishNotation(string input)
        {
            Stack <string> operatorsStack = new Stack <string>();
            List <string>  output         = new List <string>();

            string bufferWord = "";
            int    opens = 0;
            bool   bOper = false, bClose = false, bClosed = false;
            string operation = "", value = "";

            foreach (char c in input)
            {
                if (opens > 0)
                {
                    bufferWord += c;
                    if (c == '(' || c == '{')
                    {
                        opens++;
                    }
                    else if (c == ')' || c == '}')
                    {
                        opens--;
                    }

                    if (opens == 0)
                    {
                        if (bClosed)
                        {
                            bClosed = false;
                            output.RemoveAt(output.Count - 1);
                        }
                        //output.Add(bufferWord);
                        //bufferWord = "";
                    }
                    continue;
                }
                if (bOper)
                {
                    if (instance.isOperation(operation + c))
                    {
                        operation += c;
                    }
                    else
                    {
                        if (instance.isIgnoreOperator(operation))
                        {
                            bufferWord += operation;
                        }
                        else
                        {
                            if (bufferWord.Length > 0)
                            {
                                if (bClosed)
                                {
                                    bClosed = false;
                                    output.RemoveAt(output.Count - 1);
                                }
                                output.Add(bufferWord);
                                bufferWord = "";
                            }
                            else if ((operation == "+" || operation == "-") && !bClose)
                            {
                                bufferWord = operation;
                                operation  = "";
                            }

                            if (operation.Length > 0)
                            {
                                while (operatorsStack.Count > 0 && AppParams.getOperationRatio(operatorsStack.Peek()) <= AppParams.getOperationRatio(operation) && operatorsStack.Peek() != "(")
                                {
                                    output.Add(operatorsStack.Pop());
                                }
                                operatorsStack.Push(operation);
                                bClosed = false;
                                bClose  = false;
                            }
                        }
                        if (c == '(')
                        {
                            operatorsStack.Push("(");
                        }
                        else if (c != ' ')
                        {
                            bufferWord += c;
                        }
                        if (c == '{')
                        {
                            opens++;
                        }
                        operation = "";
                        bOper     = false;
                    }
                    continue;
                }
                else if (AppParams.Instance.isOperation("" + c))
                {
                    if (bufferWord.Length > 0 || (c != '*' && c != '&') || bClose)
                    {
                        bOper     = true;
                        operation = "" + c;
                    }
                    else
                    {
                        bufferWord += c;
                    }
                    continue;
                }
                else if (c == '(' && bufferWord.Length == 0)
                {
                    operatorsStack.Push("(");
                    continue;
                }
                else if (c == ')')
                {
                    bClose = true;
                    if (bufferWord.Length > 0)
                    {
                        output.Add(bufferWord);
                        bufferWord = "";
                    }
                    string oper;
                    int    count = 0;
                    while ((oper = operatorsStack.Pop()) != "(")
                    {
                        output.Add(oper);
                        count++;
                    }
                    if (count == 0)
                    {
                        bClosed = true;
                    }
                    continue;
                }

                switch (c)
                {
                case '{':
                    bufferWord += c;
                    opens++;
                    break;

                case '(':
                    if (Instance.isOperator(bufferWord))
                    {
                        output.Add(bufferWord);
                        operatorsStack.Push("(");
                        bufferWord = "";
                    }
                    else
                    {
                        bufferWord += c;
                        opens++;
                    }
                    break;

                case ' ':
                    if (Instance.isOperator(bufferWord))
                    {
                        output.Add(bufferWord);
                        bufferWord = "";
                    }
                    break;

                default:
                    bufferWord += c;
                    break;
                }
            }

            if (bufferWord.Length > 0)
            {
                if (bClosed)
                {
                    bClosed = false;
                    output.RemoveAt(output.Count - 1);
                }
                output.Add(bufferWord);
            }

            if (operation.Length > 0)
            {
                while (operatorsStack.Count > 0 && AppParams.getOperationRatio(operatorsStack.Peek()) <= AppParams.getOperationRatio(operation))
                {
                    output.Add(operatorsStack.Pop());
                }
                operatorsStack.Push(operation);
            }

            while (operatorsStack.Count > 0)
            {
                output.Add(operatorsStack.Pop());
            }

            return(output);
        }
Ejemplo n.º 2
0
        protected void parseValue(string body, int offset)
        {
            List <string>  polishNotation = AppParams.getReversePolishNotation(body);
            Stack <string> bufferStack    = new Stack <string>();

            if (polishNotation.Count == 1 && polishNotation[0].ToList().FindAll(x => x == '(').Count > 0)
            {
                string bufferWord = "", value = "";
                int    opens = 0;
                foreach (char c in polishNotation[0])
                {
                    if (opens > 0)
                    {
                        if (c == '(')
                        {
                            opens++;
                        }
                        else if (c == ')')
                        {
                            opens--;
                        }
                        if (opens != 0)
                        {
                            value += c;
                        }
                        continue;
                    }
                    switch (c)
                    {
                    case '(':
                        opens++;
                        break;

                    case ' ':
                        break;

                    default:
                        bufferWord += c;
                        break;
                    }
                }
                Action meth = new Action(polishNotation[0], value, Position, Position, Length);
                Children.Add(meth);
                return;
            }

            foreach (string str in polishNotation)
            {
                if (AppParams.Instance.isOperation(str))
                {
                    string left, right;
                    right = bufferStack.Pop();
                    left  = bufferStack.Pop();

                    string val = "";
                    if (left.Length > 1)
                    {
                        val += "(" + left + ")";
                    }
                    else
                    {
                        val += left;
                    }

                    val += str;

                    if (right.Length > 1)
                    {
                        val += "(" + right + ")";
                    }
                    else
                    {
                        val += right;
                    }

                    if (left.ToList().FindAll(x => x == '(').Count == 1)
                    {
                        string bufferWord = "", value = "";
                        int    opens = 0;
                        foreach (char c in left)
                        {
                            if (opens > 0)
                            {
                                if (c == '(')
                                {
                                    opens++;
                                }
                                else if (c == ')')
                                {
                                    opens--;
                                }
                                if (opens != 0)
                                {
                                    value += c;
                                }
                            }
                            switch (c)
                            {
                            case '(':
                                opens++;
                                break;

                            case ' ':
                                break;

                            default:
                                bufferWord += c;
                                break;
                            }
                        }
                        Action meth = new Action(left, value, Position, Position, Length);
                        Children.Add(meth);
                    }

                    if (right.ToList().FindAll(x => x == '(').Count == 1)
                    {
                        string bufferWord = "", value = "";
                        int    opens = 0;
                        foreach (char c in right)
                        {
                            if (opens > 0)
                            {
                                if (c == '(')
                                {
                                    opens++;
                                }
                                else if (c == ')')
                                {
                                    opens--;
                                }
                                if (opens != 0)
                                {
                                    value += c;
                                }
                            }
                            switch (c)
                            {
                            case '(':
                                opens++;
                                break;

                            case ' ':
                                break;

                            default:
                                bufferWord += c;
                                break;
                            }
                        }
                        Action meth = new Action(right, value, Position, Position, Length);
                        Children.Add(meth);
                    }

                    Action var = new Action(val, "", Position, Position, Length, str);
                    Children.Add(var);
                    bufferStack.Push(val);
                }
                else
                {
                    bufferStack.Push(str);
                }
            }
        }