Example #1
0
        /// <summary>
        /// This method gets called when the token parser encounters a minus sign in front of a value.
        /// If the next token is a number, it will be changed to a negative number.
        /// If the next token is a funcion, param, or equation, an equation will be generated that multiplies the result by -1
        /// </summary>
        /// <returns>The negative token.</returns>
        /// <param name="tokenList">Token list.</param>
        /// <param name="curIndex">Current index.</param>
        /// <param name="owner">Owner.</param>
        public static BaseNode ParseNegativeToken(List <Token> tokenList, ref int curIndex, Equation owner)
        {
            //verify that this is not the last token
            if (curIndex >= (tokenList.Count - 1))
            {
                throw new FormatException("Can't end an equation with an operator");
            }

            //check that the token is a minus sign
            if ("-" != tokenList[curIndex].TokenText)
            {
                throw new FormatException("Expected a value, but found an invalid operator instead: " + tokenList[curIndex].TokenText);
            }

            //skip past the minus sign so we can get to the next token
            curIndex++;

            //create a number node, parse the next token into it
            BaseNode valueNode = BaseNode.ParseValueNode(tokenList, ref curIndex, owner);

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

            //what did we get back?
            if (valueNode is NumberNode)
            {
                //the next node is a number, multiply it by minus one
                NumberNode myNumberNode = valueNode as NumberNode;
                myNumberNode.NumberValue *= -1.0f;
            }
            else
            {
                //ok the node was a function, param, or equation

                //create another equation to multiply that resdult by -1
                NumberNode negativeOne = new NumberNode();
                negativeOne.NumberValue = -1.0f;
                OperatorNode multiplyNode = new OperatorNode();
                multiplyNode.Operator = '*';

                //string it all together
                negativeOne.AppendNextNode(multiplyNode);
                multiplyNode.AppendNextNode(valueNode);

                //put that into an equation node and treeify it
                EquationNode myEquationNode = new EquationNode();
                myEquationNode.SubEquation = negativeOne.Treeify();
                Debug.Assert(null != myEquationNode.SubEquation); //TODO: throw exceptions

                //set our result to the whole equation
                valueNode = myEquationNode;
            }

            //return it as the result
            return(valueNode);
        }
Example #2
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);
        }