public MathExpParser()
 {
     _operatorStack = countOperators(_inputEquation);
     _operatorsStackCopy = new Stack<nodeValue>(_operatorStack);
     _operatorStack = new Stack<nodeValue>(_operatorStack);
     _variable = new Stack<string>();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Function converts stack with two-address instruction commands into pbStack with one-address instruction commands.
        /// </summary>
        /// <returns>Stack with one-address instruction commands.</returns>
        public Stack<Command> populateStack()
        {
            Stack<Command> returnVal = new Stack<Command>();
            Stack<Command> tempStack ;
            Command loadCommand;
            Command OperationCommand;
            Command SaveCommand;
            foreach (Command currCommand in CommandStack)
            {
                if (currCommand.leftOperand == null)
                {
                    if (currCommand.rightOperand == null)
                    {
                        // if Right points direct to value and Left operand stores in MemoryCell

                        // Save result to Memory Cell
                        // KOP  |MEM  |M{#}|
                        SaveCommand = new Command(CommandBase.commVals.Mem, currCommand.MemoryCellUsed);
                        returnVal.Push(SaveCommand);

                        // Load Right operand and Evaluate Operation
                        // KOP  |OP   |RO|
                        OperationCommand = new Command((CommandBase.commVals)currCommand.Code, currCommand.MemoryCellNeededExtra, true);
                        returnVal.Push(OperationCommand);

                        // Load left operand
                        // KOP  |LOAD |M{#}|
                        loadCommand = new Command(CommandBase.commVals.Load, currCommand.MemoryCellNeeded);
                        returnVal.Push(loadCommand);
                    }
                    else
                    {
                        // if Right points direct to value and Left operand stores in MemoryCell

                        // Save result to Memory Cell
                        // KOP  |MEM  |M{#}|
                        SaveCommand = new Command(CommandBase.commVals.Mem, currCommand.MemoryCellUsed);
                        returnVal.Push(SaveCommand);

                        // Load Right operand and Evaluate Operation
                        // KOP  |OP   |RO|
                        OperationCommand = new Command((CommandBase.commVals)currCommand.Code, currCommand.MemoryCellNeeded);
                        returnVal.Push(OperationCommand);

                        // Load left operand
                        // KOP  |LOAD |M{#}|
                        loadCommand = new Command(CommandBase.commVals.Load, currCommand.MemoryCellNeeded);
                        returnVal.Push(loadCommand);
                    }
                }
                else
                    if (currCommand.rightOperand == null)
                    {
                        // if Left points direct to value and Right operand stores in MemoryCell

                        // Save result to Memory Cell
                        // KOP  |MEM  |M{#}|
                        SaveCommand = new Command(CommandBase.commVals.Mem, currCommand.MemoryCellUsed);
                        returnVal.Push(SaveCommand);

                        // Load Right operand and Evaluate Operation
                        // KOP  |OP   |M{#}|
                        OperationCommand = new Command((CommandBase.commVals)currCommand.Code, currCommand.MemoryCellNeeded);
                        returnVal.Push(OperationCommand);

                        // Load left operand
                        // KOP  |LOAD |LO|
                        loadCommand = new Command(CommandBase.commVals.Load, currCommand.leftOperand);
                        returnVal.Push(loadCommand);
                    }
                    else
                    {
                        // if Left and Right Operands are points direct to their values.

                        // Save result to Memory Cell
                        // KOP  |MEM  |M{#}|
                        SaveCommand = new Command(CommandBase.commVals.Mem, currCommand.MemoryCellUsed);
                        returnVal.Push(SaveCommand);

                        // Load Right operand and Evaluate Operation
                        // KOP  |OP   |RO|
                        OperationCommand = new Command((CommandBase.commVals)currCommand.Code, currCommand.rightOperand);
                        returnVal.Push(OperationCommand);

                        // Load left operand
                        // KOP  |LOAD |LO|
                        loadCommand = new Command(CommandBase.commVals.Load, currCommand.leftOperand);
                        returnVal.Push(loadCommand);
                    }

            }// foreach
            //tempStack = new Stack<Command>(returnVal);
            //return tempStack;
            return returnVal;
        }
Exemplo n.º 3
0
 public Stack()
 {
     CommandStack = new Stack<Command>();
     StackMemory = new List<MemoryCell>();
 }
        public int compile(String equation)
        {
            _inputEquation = equation;
            Console.WriteLine("Analizing... \n\r \tEquation=[" + _inputEquation + "]");

            _curStackValue = new nodeValue();

            // Creating root
            DTreeNode<String> root = new DTreeNode<String>();

            //DTreeBuilder<nodeValue> trB = new DTreeBuilder<nodeValue>();
            // Initing root
            root.Value = "Root";//_operatorStack.Peek();

            // Position to add next Node
            DTreeNode<String> curNode = null;

            // Position where were added last Node
            DTreeNode<String> lastNode = null;

            // Temporary data storage
            DTreeNode<String> tempNode = null;
            // Temporary data storage
            DTreeNode<String>[] tempNodeArray = null;
            // DEBUG Operators position and index
            if (verifyAlpha())
            {

            }
            while (_operatorStack.Count > 0)
            {
                _curStackValue.ind = _operatorStack.Peek().ind;
                _curStackValue.pos = _operatorStack.Peek().pos;
                _curStackValue.priority = _operatorStack.Pop().priority;

                Console.WriteLine("Ind= " + _curStackValue.ind + " Pos= " + _curStackValue.pos + " Prior= " + _curStackValue.priority);
            }
            Console.WriteLine("_________________________________________");

            if (verifyParentheses())
            {
                if (verifyParenthesesExp())
                {
                    // Verify all brackets

                    nodeValue[] allOperators = _operatorsStackCopy.ToArray();

                    _tempOperStack = new Stack<nodeValue>();
                    // DEBUG Operators nesting
                    for (int i = 0; i < allOperators.Length; i++)
                    {
                        allOperators[i].nested = getPriorForOperator(allOperators[i].pos);
                        _tempOperStack.Push(allOperators[i]);
                        Console.WriteLine("Nesting= " + allOperators[i].nested + "  Op" + allOperators[i].operatorSym);
                    }
                    Console.WriteLine("_________________________________________");

                    int curPos, operBeginPos, operEndPos;
                    bool foundBegin;
                    char[] sym = { '-', '/', '*', '+', ')', '(' };
                    // Setting operators operands
                    for (int i = 0; i < allOperators.Length; i++)
                    {
                        foundBegin = false;
                        // Find left operand
                        if (_inputEquation[allOperators[i].pos - 1] != ')')
                        {
                            operBeginPos = operEndPos = allOperators[i].pos - 1;
                            while (operBeginPos > 0 && !foundBegin)
                            {
                                if (_inputEquation.IndexOfAny(sym, operBeginPos - 1, 1) != -1)
                                    foundBegin = true;
                                else
                                    operBeginPos--;
                            }
                            allOperators[i].operandLeft = _inputEquation.Substring(operBeginPos, operEndPos - operBeginPos + 1);
                        }
                        else
                        {
                            // Find nested
                        }

                        foundBegin = false;
                        // Find right operand
                        if (_inputEquation[allOperators[i].pos + 1] != '(')
                        {
                            operBeginPos = operEndPos = allOperators[i].pos + 1;
                            while (operEndPos < _inputEquation.Length - 1 && !foundBegin)
                            {

                                if (_inputEquation.IndexOfAny(sym, operEndPos + 1, 1) != -1)
                                    foundBegin = true;
                                else
                                    operEndPos++;
                            }
                            allOperators[i].operandRight = _inputEquation.Substring(operBeginPos, operEndPos - operBeginPos + 1);
                        }
                    }

                    /*
                // DEBUG Tree
                MainForm testForm = new MainForm();
                TreeView testTV = new TreeView();

                testForm.Controls.Add(testTV);
                testForm.Controls[0].Width = 500;
                testForm.Controls[0].Height = 1500;

                testTV.Width = 500;
                testTV.Height = 1500;
                bool first = false;
                int lastNested = 0;
                int lastPrior = 0;
                int curNested = 0;
                int curPrior = 0;
                string tempString;

                TreeViewController<String> treeViewController = new TreeViewController<String>((TreeView)testForm.Controls[0], root);

                while (_tempOperStack.Count > 0)
                {
                    curStackValue = _tempOperStack.Pop();
                    if (!first)
                    {
                        // Lowest priority, means last in evaluation
                        if (curNode != null)
                            lastNode = curNode.Nodes.Add(curStackValue.toString());
                        else
                            curNode = lastNode = root.Nodes.Add(curStackValue.toString());
                        first = true;
                    }
                    else
                    {
                        // Lowest priority, means last in evaluation
                        // The same nesting, adding to to this leave
                        if (lastNested == curStackValue.nested)
                        {
                            if (lastPrior <= curStackValue.priority)
                            {
                                lastNode = curNode.Nodes.Add(curStackValue.toString());
                                //CopyNodeToNode(lastNode, curNode);
                                //lastNode.Remove();
                                //lastNode = curNode;
                            }
                            else
                            {
                               //curNode = root.Nodes.InsertBefore(lastNode, curStackValue.toString());

                               //CopyNodeToNode(curNode, lastNode);
                               lastNode = curNode.Nodes.Add(curStackValue.toString());//tempString
                            }
                        }
                        else
                        {
                            if (lastNested > curStackValue.nested)
                            {
                                if (curNode.Nodes.Count < 2)
                                {
                                    curNode = curNode.Nodes.Add(curStackValue.toString());
                                    curNested = curStackValue.nested;
                                    curPrior = curStackValue.priority;

                                    tempString = lastNode.Value;
                                    lastNode.Remove();
                                    curNode.Nodes.Add(tempString);
                                }
                                else
                                {
                                    if (curNode.Parent.Parent == null)
                                    {
                                        lastNode = root.Nodes.InsertBefore(curNode, curStackValue.toString());
                                        CopyNodeToNode(curNode, lastNode);

                                        curNode.Remove();
                                        curNode = lastNode;
                                    }
                                    else
                                    {
                                        lastNode = curNode.Parent.Nodes.Add(curStackValue.toString());
                                        curNode = curNode.Parent;
                                    }
                                }
                            }
                            else
                            {
                                lastNode = curNode.Nodes.Add(curStackValue.toString());
                            }
                        }
                    }
                    lastNested = curStackValue.nested;
                    lastPrior = curStackValue.priority;
                }
                testForm.ShowDialog();
                */
                }
                else
                {
                    Console.WriteLine("validation Error! Operator Missing!");
                    return -1;
                }

            }
            else
            {
                Console.WriteLine("validation Error! Parentheses Missing!");
                return -1;
            }
            return 0;
        }
 public Stack<String> getVars()
 {
     Stack<String> tempStack = new Stack<string>();
     MatchCollection mc = Regex.Matches(_inputEquation, @"[a-zA-z]");
     if (mc.Count > 0)
     {
         foreach (Match match in mc)
         {
             foreach (Capture capture in match.Captures)
             {
                 tempStack.Push(capture.Value);
             }
         }
         return tempStack;
     }
     else
         return null;
 }
        public Stack<nodeValue> countOperators(string equation)
        {
            Stack<nodeValue> count = new Stack<nodeValue>();
            char[] sym = { '-', '/', '*', '+' };
            int n = 0, ind = 0;
            int prior;
            while ((n = equation.IndexOfAny(sym, n)) != -1)
            {
                prior = equation[n] == '/' || equation[n] == '*' ? 1 : 0;
                count.Push(new nodeValue(0, n, prior, equation[n].ToString()));
                n++;
                ind++;
            }

            return count;
        }