Example #1
0
        /// <summary>
        /// Parse the specified equationText.
        /// </summary>
        /// <param name="equationText">Equation text.</param>
        public void Parse(string equationText)
        {
            try
            {
                //grab the equation text
                TextEquation = equationText;

                //straight up tokenize the equation: operators, numbers, parens, functions, params
                List <Token> tokenList = Tokenize(equationText);

                //check if an empty equation was passed into the equationator
                if (0 == tokenList.Count)
                {
                    RootNode = null;
                }
                else
                {
                    //sort out those tokens into a linked list of equation nodes
                    int      index        = 0;
                    BaseNode listRootNode = BaseNode.Parse(tokenList, ref index, this);

                    //take that linked list and bend it into a binary tree.  Grab the root node
                    RootNode = listRootNode.Treeify();
                }
            }
            catch (Exception)
            {
                //Clean up everything
                TextEquation = null;
                RootNode     = null;

                //rethrow the exception
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// Parse the specified tokenList and curIndex.
        /// overloaded by child types to do there own specific parsing.
        /// </summary>
        /// <param name="tokenList">Token list.</param>
        /// <param name="curIndex">Current index.</param>
        /// <param name="owner">the equation that this node is part of.  required to pull function delegates out of the dictionary</param>
        protected override void ParseToken(List <Token> tokenList, ref int curIndex, Equation owner)
        {
            //check arguments
            if (null == tokenList)
            {
                throw new ArgumentNullException("tokenList");
            }
            if (null == owner)
            {
                throw new ArgumentNullException("owner");
            }
            Debug.Assert(curIndex < tokenList.Count);  //TODO: throw exceptions

            //parse the equation into our subnode
            SubEquation = BaseNode.Parse(tokenList, ref curIndex, owner);

            //if some smart ass types in () the parse method wouldve returned null.
            //it should evaluate to 0 in that case, so create a number node and set the value.
            if (null == SubEquation)
            {
                NumberNode fakeNode = new NumberNode();
                fakeNode.NumberValue = 0.0f;
                SubEquation          = fakeNode;
            }
            Debug.Assert(null != SubEquation); //TODO: throw exceptions

            //treeify the subequation so we can solve it
            SubEquation = SubEquation.Treeify();
            Debug.Assert(null != SubEquation); //TODO: throw exceptions
        }
Example #3
0
        /// <summary>
        /// Parse a list of tokens into a linked list of equation nodes.
        /// This will sort it out into a flat equation
        /// </summary>
        /// <param name="tokenList">Token list.</param>
        /// <param name="curIndex">Current index. When this function exits, will be incremented to the past any tokens consumed by this method</param>
        /// <param name="owner">the equation that this node is part of.  required to pull function delegates out of the dictionary</param>
        /// <returns>A basenode pointing at the head of a linked list parsed by this method</returns>
        public static BaseNode Parse(List <Token> tokenList, ref int curIndex, Equation owner)
        {
            if (null == tokenList)
            {
                throw new ArgumentNullException(nameof(tokenList));
            }
            if (null == owner)
            {
                throw new ArgumentNullException(nameof(owner));
            }

            Debug.Assert(curIndex < tokenList.Count); //TODO: throw exceptions

            //first get a value, which will be a number, function, param, or equation node
            BaseNode myNumNode = BaseNode.ParseValueNode(tokenList, ref curIndex, owner);

            Debug.Assert(null != myNumNode); //TODO: throw exceptions

            //if there are any tokens left, get an operator
            if (curIndex < tokenList.Count)
            {
                BaseNode myOperNode = BaseNode.ParseOperNode(tokenList, ref curIndex, owner);

                if (null != myOperNode)
                {
                    //add that node to the end of the list
                    myNumNode.AppendNextNode(myOperNode);

                    //If it was able to pull an operator out, there has to be a number after it.
                    if (curIndex >= tokenList.Count)
                    {
                        throw new FormatException("Can't end an equation with an operator.");
                    }

                    //Recurse into the parse function and sort out the rest of the tokens
                    BaseNode nextNode = BaseNode.Parse(tokenList, ref curIndex, owner);
                    Debug.Assert(null != nextNode); //TODO: throw exceptions

                    //add that node to the end of the list
                    myOperNode.AppendNextNode(nextNode);
                }
            }

            //return the head node that I found
            return(myNumNode);
        }