Beispiel #1
0
        /// <summary>
        /// Traverses the list to perform operations on items according to operator precedence
        /// </summary>
        /// <returns>final evaluated expression of Expression string</returns>
        private object EvaluateList()
        {
            ArrayList list = (ArrayList)_expressionlist.Clone();

            //Do the unary operators first
            for (int x = 0; x < list.Count; x++)
            {
                if (list[x] is UnaryOp)
                {
                    list[x] = PerformUnaryOp(
                        (UnaryOp)list[x],
                        list[x + 1]
                    );
                    list.RemoveAt(x + 1);
                }
            }

            //Get the queued binary operations
            BinaryOpQueue opqueue = new BinaryOpQueue(list);

            string msg = "";
            for (int x = 1; x < list.Count; x += 2)
            {
                bool bError = false;
                if (list[x] is BinaryOp)
                {
                    if (x + 1 == list.Count)
                    {
                        throw new ArgumentException(
                            "Expression cannot end in a binary operation: [" + list[x].ToString() + "]"
                        );
                    }
                }
                else
                {
                    msg += string.Format(
                        "\n{0} [?] {1}",
                        (list[x - 1] is string) ? "\"" + list[x - 1] + "\"" : list[x - 1],
                        (list[x] is string) ? "\"" + list[x] + "\"" : list[x]
                    );
                    x--;
                }
            }
            if (msg != "")
                throw new ArgumentException("Missing binary operator: " + msg);

            BinaryOp op = opqueue.Dequeue();
            while (op != null)
            {
                int nIdx = list.IndexOf(op);
                list[nIdx - 1] = PerformBinaryOp(
                    (BinaryOp)list[nIdx],
                    list[nIdx - 1],
                    list[nIdx + 1]
                );
                list.RemoveAt(nIdx);
                list.RemoveAt(nIdx);
                op = opqueue.Dequeue();
            }

            object ret = null;
            if (list[0] is IExpression)
                ret = ((IExpression)list[0]).Evaluate();
            else
                ret = list[0];
            return ret;
        }
Beispiel #2
0
        /// <summary>
        /// Traverses the list to perform operations on items according to operator precedence
        /// </summary>
        /// <returns>final evaluated expression of Expression string</returns>
        private object EvaluateList()
        {
            ArrayList list = (ArrayList)_expressionlist.Clone();

            //Do the unary operators first
            for (int x = 0; x < list.Count; x++)
            {
                if (list[x] is UnaryOp)
                {
                    list[x] = PerformUnaryOp(
                        (UnaryOp)list[x],
                        list[x + 1]
                        );
                    list.RemoveAt(x + 1);
                }
            }

            //Get the queued binary operations
            BinaryOpQueue opqueue = new BinaryOpQueue(list);

            string msg = "";

            for (int x = 1; x < list.Count; x += 2)
            {
                if (list[x] is BinaryOp)
                {
                    if (x + 1 == list.Count)
                    {
                        throw new ArgumentException(
                                  "Expression cannot end in a binary operation: [" + list[x].ToString() + "]"
                                  );
                    }
                }
                else
                {
                    msg += string.Format(
                        "\n{0} [?] {1}",
                        (list[x - 1] is string) ? "\"" + list[x - 1] + "\"" : list[x - 1],
                        (list[x] is string) ? "\"" + list[x] + "\"" : list[x]
                        );
                    x--;
                }
            }
            if (msg != "")
            {
                throw new ArgumentException("Missing binary operator: " + msg);
            }

            BinaryOp op = opqueue.Dequeue();

            while (op != null)
            {
                int nIdx = list.IndexOf(op);
                list[nIdx - 1] = PerformBinaryOp(
                    (BinaryOp)list[nIdx],
                    list[nIdx - 1],
                    list[nIdx + 1]
                    );
                list.RemoveAt(nIdx);
                list.RemoveAt(nIdx);
                op = opqueue.Dequeue();
            }

            object ret = null;

            if (list[0] is IExpression)
            {
                ret = ((IExpression)list[0]).Evaluate();
            }
            else
            {
                ret = list[0];
            }
            return(ret);
        }