Example #1
0
        /// <summary>
        /// Name:Compile
        /// Description:Compiles the expression
        /// </summary>
        /// <param name="expression">inputed expression</param>
        /// <returns>returns the basenode of the expression tree</returns>
        private static BasicNode Compile(string expression)
        {
            if (expression.Length != 0)
            {
                int operatorIndex = expression.Length - 1;
                while (operatorIndex > 0 && !ExpressionTreeFactory.IsValidOperator(expression[operatorIndex]))
                {
                    operatorIndex--;
                }

                if (ExpressionTreeFactory.IsValidOperator(expression[operatorIndex]))
                {
                    OperatorNode newNode = ExpressionTreeFactory.CreateOperatorNode(expression[operatorIndex]);
                    newNode.Left  = Compile(expression.Substring(0, operatorIndex));
                    newNode.Right = Compile(expression.Substring(operatorIndex + 1));

                    return(newNode);
                }

                double number;
                if (double.TryParse(expression, out number))
                {
                    ConstantNode newNode = new ConstantNode(number);

                    return(newNode);
                }
                else
                {
                    VariableNode newNode = new VariableNode(expression);
                    return(newNode);
                }
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Name:Compile
        /// Description:Compiles the expression
        /// </summary>
        /// <param name="expression">inputed expression</param>
        /// <returns>returns the basenode of the expression tree</returns>
        private BasicNode Compile(string expression)
        {
            expression = expression.Replace(" ", string.Empty);
            int counter = 1, i = 0;

            if (expression[i] == '(')
            {
                for (i = 1; expression.Length > i; i++)
                {
                    if (expression[i] == '(')
                    {
                        counter++;
                    }
                    else if (expression[i] == ')')
                    {
                        counter--;
                        if (counter == 0)
                        {
                            if (i == expression.Length - 1)
                            {
                                return(this.Compile(expression.Substring(1, expression.Length - 2)));
                            }

                            break;
                        }
                    }
                }

                if (counter != 0)
                {
                    throw new System.ArgumentException("Too many or too few parentheses", "Invalid Expression");
                }
            }

            int index = GetLowestOperatorInheritanceIndex(expression);

            if (index != -1)
            {
                OperatorNode node = ExpressionTreeFactory.CreateOperatorNode(expression[index]);
                node.Right = this.Compile(expression.Substring(index + 1));
                node.Left  = this.Compile(expression.Substring(0, index));
                return(node);
            }
            else if (index == -2)
            {
                throw new System.ArgumentException("Too many or too few parentheses", "Invalid Expression");
            }

            return(this.BuildVNode(expression));
        }