Beispiel #1
0
 public void Push(Token p_token)
 {
     //push in operator/ operand stack
     if (p_token is Operator)
     {
         //operatorStack.
         if (operatorStack.Count == 0 || (operatorStack.Count > 0 && operatorStack.Peek().Priority < (p_token as Operator).Priority))
         {
             //do remaining operation
             operatorStack.Push(p_token as Operator);
         }
         else
         {
             List <Token> args = new List <Token>();
             for (int i = 0; i < 2; i++) //as these all are binary operators
             {
                 args.Insert(0, LiteralStack.Pop());
             }
             Token tkn = OperatorStack.Pop().Evalute(args);
             LiteralStack.Push(tkn as Literal);
             OperatorStack.Push(p_token as Operator);
         }
     }
     else if (p_token is Literal)
     {
         LiteralStack.Push(p_token as Literal);
     }
 }
Beispiel #2
0
        private void BuildTree()
        {
            while (OperatorStack.Count != 0)
            {
                TreeNode <string> temp_root = OperatorStack.Pop();

                temp_root.Right = NumberStack.Pop();
                temp_root.Left  = NumberStack.Pop();

                temp_root = CheckPrecedence(temp_root);

                NumberStack.Push(temp_root);
            }
        }
Beispiel #3
0
 private void BuildNodes(string[] expressionArray)
 {
     foreach (string item in expressionArray)
     {
         double tempNumber;
         if (double.TryParse(item, out tempNumber))
         {
             TreeNode <string> number_node = new TreeNode <string> {
                 Data = tempNumber.ToString()
             };
             NumberStack.Push(number_node);
         }
         else
         {
             TreeNode <string> operator_node = new TreeNode <string> {
                 Data = item
             };
             OperatorStack.Push(operator_node);
         }
     }
 }
Beispiel #4
0
        internal string Summerize()
        {
            Token tkn = null;

            Operator     opt  = null;
            List <Token> args = null;

            for (int i = 0; OperatorStack.Count > 0; i++)
            {
                opt  = OperatorStack.Pop();
                args = new List <Token>();
                for (int j = 0; j < 2; j++) //as these all are binary operators
                {
                    args.Insert(0, LiteralStack.Pop());
                }
                tkn = opt.Evalute(args);
                LiteralStack.Push(tkn as Literal);
            }
            //opt = OperatorStack.Pop();

            return((tkn as Literal).Value.ToString());
        }
Beispiel #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:ConstraintBuilder"/> class.
 /// </summary>
 public ConstraintBuilder()
 {
     this.ops         = new OperatorStack(this);
     this.constraints = new ConstraintStack(this);
 }
        /// <summary>
        /// Performs the Shunting-Yard Algorithm
        /// </summary>
        public void Shunt()
        {
            // O(n)

            if (!ShuntValid)
            {
                throw new Exception("Shunt() was called without proper initialisation. Check ShuntValid first!");
            }

            var working = new List <Symbol>();

            foreach (var symbol in Input)
            {
                if (symbol.GetType() == typeof(Operand))
                {
                    working.Add(symbol);                                      // Add any numbers straight to output
                }
                else if (symbol.ToString() != "(" && symbol.ToString() != ")")
                { // Operators
                    var op = (Operator)symbol;

                    // If function, Push() to stack and continue;
                    if (op.Op == Operators.Function)
                    {
                        OperatorStack.Push(op);
                        continue;
                    }

                    while ((OperatorStack.Count != 0 && // Stack not empty!
                            (OperatorStack.Peek().Precedence >
                             op.Precedence || // Top operator has higher precedence than current one
                             (OperatorStack.Peek().Precedence == op.Precedence && !OperatorStack.Peek().IsRightAssociative))
                            ) && // Top op has equal precendence and is left associative
                           OperatorStack.Peek().Op != Operators.OpenBracket)    // Top op is not opening bracket
                    {
                        working.Add(OperatorStack.Pop());
                    }

                    OperatorStack.Push(op); // Add operator to stack
                }
                else
                {
                    switch (symbol.ToString())
                    {
                    case "(": // Opening Bracket
                        OperatorStack.Push((Operator)symbol);
                        break;

                    case ")": // Closing Bracket
                    {
                        while (OperatorStack.Peek().Op != Operators.OpenBracket)
                        {
                            if (OperatorStack.Count == 0)
                            {
                                throw new Exception("Ran out of stack while looking for left bracket: input had mismatched brackets.");
                            }
                            working.Add(OperatorStack.Pop());
                        }

                        if (OperatorStack.Peek().Op == Operators.OpenBracket)
                        {
                            OperatorStack.Pop();                                                   // Discard extra brackets
                        }
                        break;
                    }
                    }
                }
            }

            working.AddRange(OperatorStack); // Final step: pop out entire stack to output
            OperatorStack.Clear();           // Apparently AddRange() will Peek() instead of Pop() if the stack only has one element, failing to clear it

            Output = working.ToArray();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:ConstraintBuilder"/> class.
 /// </summary>
 public ConstraintBuilder()
 {
     this.ops = new OperatorStack(this);
     this.constraints = new ConstraintStack(this);
 }
Beispiel #8
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="T:ConstraintBuilder" /> class.
 /// </summary>
 internal ConstraintBuilder()
 {
     _ops = new OperatorStack(this);
     _constraints = new ConstraintStack(this);
 }