Danmaku_no_Kyojin.BulletEngine.Equationator.BaseNode.ParseOperNode C# (CSharp) Method

ParseOperNode() protected static method

Given a list of tokens and the index, get an operator node based on whatever is at that index.
protected static ParseOperNode ( List tokenList, int &curIndex, Equation owner ) : BaseNode
tokenList List Token list.
curIndex int Current index.
owner Equation the equation that this node is part of. required to pull function delegates out of the dictionary
return BaseNode
        protected static BaseNode ParseOperNode(List<Token> tokenList, ref int curIndex, Equation owner)
        {
            Debug.Assert(null != tokenList);
            Debug.Assert(null != owner);
            Debug.Assert(curIndex < tokenList.Count);

            //what kind of token do I have at that index?
            switch (tokenList[curIndex].TypeOfToken)
            {
                case TokenType.Operator:
                {
                    //ok create an operator node
                    OperatorNode operNode = new OperatorNode();

                    //parse into that node
                    operNode.ParseToken(tokenList, ref curIndex, owner);

                    //return the thing
                    return operNode;
                }

                case TokenType.CloseParen:
                {
                    //close paren, just eat it and return null.  It means this equation is finished parsing
                    curIndex++;
                    return null;
                }

                default:
                {
                    //should just be close paren nodes in here, which we should never get
                    throw new FormatException("Expected a \"operator\" token, but got a " + tokenList[curIndex].TypeOfToken.ToString());
                }
            }
        }

Usage Example

Example #1
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>
        static public BaseNode Parse(List <Token> tokenList, ref int curIndex, Equation owner)
        {
            Debug.Assert(null != tokenList);
            Debug.Assert(null != owner);
            Debug.Assert(curIndex < tokenList.Count);

            //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);

            //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);

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

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