public ExpressionBase Parse(List<Token> postFix)
        {
            var stack = new ExpressionStack();
            ExpressionBase root = null;
            ExpressionBase left = null;
            ExpressionBase right = null;
            foreach (var token in postFix)
            {
                switch (token.Type)
                {
                    case TokenType.Function:
                        root = new FunctionExpression(stack.Pop(), (string)token.Data);
                        stack.Push(root);
                        break;

                    case TokenType.Delimiter:
                        switch (token.Data.ToString())
                        {
                            case "()":
                                root = new DelimiterExpression(stack.Pop());
                                stack.Push(root);
                                break;
                            case "{}":
                                root = stack.Pop();
                                stack.Push(root);
                                break;
                        }

                        break;

                    case TokenType.Number:
					root = new NumericExpression(int.Parse(token.Data.ToString()));
                        stack.Push(root);
                        break;

                    case TokenType.Constant:
                        root = new ConstantExpression((ConstantType)token.Data);
                        stack.Push(root);
                        break;

                    case TokenType.Variable:
                        root = new VariableExpression((string)token.Data);
                        stack.Push(root);
                        break;

                    case TokenType.Operator:
                        switch (token.Data.ToString())
                        {
                            case "~":
                                root = new UnaryMinusExpression(stack.Pop());
                                stack.Push(root);
                                break;

                            case "+":
                                right = stack.Pop();
                                left = stack.Pop();
                                if (left is VariadicOperatorExpression && (left as VariadicOperatorExpression).Type == OperatorType.Add)
                                {
                                    (left as VariadicOperatorExpression).Add(right);
                                    root = left;
                                    stack.Push(root);
                                }
                                else
                                {
                                    root = new VariadicOperatorExpression(OperatorType.Add, left, right);
                                    stack.Push(root);
                                }
                                break;

                            case "-":
                                if (this.TreatMinusAsUnaryMinusInVariadicPlus)
                                {
                                    right = new UnaryMinusExpression(stack.Pop());
                                    left = stack.Pop();
                                    if (left is VariadicOperatorExpression && (left as VariadicOperatorExpression).Type == OperatorType.Add)
                                    {
                                        (left as VariadicOperatorExpression).Add(right);
                                        root = left;
                                        stack.Push(root);
                                    }
                                    else
                                    {
                                        root = new VariadicOperatorExpression(OperatorType.Add, left, right);
                                        stack.Push(root);
                                    }
                                }
                                else // treat it at binary operator
                                {
                                    right = stack.Pop();
                                    left = stack.Pop();
                                    root = new BinaryOperatorExpression(left, right, OperatorType.Subtract);
                                    stack.Push(root);
                                }
                                break;

                            case "*":
                                right = stack.Pop();
                                left = stack.Pop();
                                if (left is VariadicOperatorExpression && (left as VariadicOperatorExpression).Type == OperatorType.Multiply)
                                {
                                    (left as VariadicOperatorExpression).Add(right);
                                    root = left;
                                    stack.Push(root);
                                }
                                else
                                {
                                    root = new VariadicOperatorExpression(OperatorType.Multiply, left, right);
                                    stack.Push(root);
                                }
                                break;

                            case "/":
                                right = stack.Pop();
                                left = stack.Pop();
                                root = new BinaryOperatorExpression(left, right, OperatorType.Divide);
                                stack.Push(root);
                                break;

                            case "^":
                                right = stack.Pop();
                                left = stack.Pop();
                                root = new BinaryOperatorExpression(left, right, OperatorType.Power);
                                stack.Push(root);
                                break;
                        }

                        break;
                }
            }
            return root;
        }
        //1/a^n = a^-n
        public static ExpressionBase ReverseVariableWithNegativeExponent(ExpressionBase expression, List<ExpressionBase> selection)
        {
            //If expression is a fraction. e.g. 1/a^n
            BinaryOperatorExpression binaryexpression = expression as BinaryOperatorExpression;
            if (binaryexpression != null && binaryexpression.Type == OperatorType.Divide)
            {
                //If numerator is 1 and denominator is power. e.g. 1 and a^n
                NumericExpression numericexpression = binaryexpression.Left as NumericExpression;
                BinaryExpression power = binaryexpression.Right as BinaryExpression;
                if (numericexpression != null && numericexpression.Value == "1" && power != null && power.Type == OperatorType.Power)
                {
                    //return power with minus exponent. e.g. a^-n
                    UnaryMinusExpression unaryminus = new UnaryMinusExpression(power.Right.Clone());
                    BinaryExpression mysuggestion = new BinaryOperatorExpression(power.Left.Clone(), unaryminus.Clone(), OperatorType.Power);
                    return mysuggestion;
                }
            }

            return null;
        }