Exemplo n.º 1
0
        /// <summary>
        /// Given a list of tokens and the index, get a node based on whatever is at that index
        /// </summary>
        /// <returns>The value node, will be a number, function, param, or equation node</returns>
        /// <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 static BaseNode ParseValueNode(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.Number:
                {
                    //awesome, that's nice and easy... just shove the text into a node as a number

                    //create the number node
                    NumberNode valueNode = new NumberNode();

                    //parse the text into the number node
                    valueNode.ParseToken(tokenList, ref curIndex, owner);

                    //return the number node as our result
                    return valueNode;
                }

                case TokenType.Param:
                {
                    //also not bad, grab the text as a parameter index and put in a node

                    //create the param node
                    ParamNode valueNode = new ParamNode();

                    //parse the parameter index into the node
                    valueNode.ParseToken(tokenList, ref curIndex, owner);

                    //return it as our result
                    return valueNode;
                }

                case TokenType.Function:
                {
                    //hmmm... need to get the delegate and put in a node?

                    //create the function node
                    FunctionNode valueNode = new FunctionNode();

                    //parse the function delegate into the node
                    valueNode.ParseToken(tokenList, ref curIndex, owner);

                    //return it as our result
                    return valueNode;
                }

                case TokenType.OpenParen:
                {
                    //ok don't panic...

                    //verify that this is not the last token
                    if (curIndex >= (tokenList.Count - 1))
                    {
                        throw new FormatException("Can't end an equation with an open paranthesis");
                    }

                    //move past this token, cuz nothing else to do with it
                    curIndex++;

                    //starting at the next token, start an equation node
                    EquationNode valueNode = new EquationNode();

                    //start parsing into the equation node
                    valueNode.ParseToken(tokenList, ref curIndex, owner);

                    //return it as the result
                    return valueNode;
                }

                case TokenType.Operator:
                {
                    //whoa, how did an operator get in here?  it better be a minus sign
                    return EquationNode.ParseNegativeToken(tokenList, ref curIndex, owner);
                }

                default:
                {
                    //should just be close paren nodes in here, which we should never get
                    throw new FormatException("Expected a \"value\" token, but got a " + tokenList[curIndex].TypeOfToken.ToString());
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Given a list of tokens and the index, get a node based on whatever is at that index
        /// </summary>
        /// <returns>The value node, will be a number, function, param, or equation node</returns>
        /// <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>
        static protected BaseNode ParseValueNode(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.Number:
            {
                //awesome, that's nice and easy... just shove the text into a node as a number

                //create the number node
                NumberNode valueNode = new NumberNode();

                //parse the text into the number node
                valueNode.ParseToken(tokenList, ref curIndex, owner);

                //return the number node as our result
                return(valueNode);
            }

            case TokenType.Param:
            {
                //also not bad, grab the text as a parameter index and put in a node

                //create the param node
                ParamNode valueNode = new ParamNode();

                //parse the parameter index into the node
                valueNode.ParseToken(tokenList, ref curIndex, owner);

                //return it as our result
                return(valueNode);
            }

            case TokenType.Function:
            {
                //hmmm... need to get the delegate and put in a node?

                //create the function node
                FunctionNode valueNode = new FunctionNode();

                //parse the function delegate into the node
                valueNode.ParseToken(tokenList, ref curIndex, owner);

                //return it as our result
                return(valueNode);
            }

            case TokenType.OpenParen:
            {
                //ok don't panic...

                //verify that this is not the last token
                if (curIndex >= (tokenList.Count - 1))
                {
                    throw new FormatException("Can't end an equation with an open paranthesis");
                }

                //move past this token, cuz nothing else to do with it
                curIndex++;

                //starting at the next token, start an equation node
                EquationNode valueNode = new EquationNode();

                //start parsing into the equation node
                valueNode.ParseToken(tokenList, ref curIndex, owner);

                //return it as the result
                return(valueNode);
            }

            case TokenType.Operator:
            {
                //whoa, how did an operator get in here?  it better be a minus sign
                return(EquationNode.ParseNegativeToken(tokenList, ref curIndex, owner));
            }

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