Beispiel #1
0
    /// <summary>
    /// Paren() : This method evaluates Parenthesis in the equation and
    ///		insures they are handled properly according to the Order of Operations. Because this is last in the chain,
    ///		it also evaluates Variable and Function names.
    /// </summary>
    /// <returns>CValue object that holds an operation.</returns>
    private CValue Paren()
    {
        bool   bFunc  = false;
        CValue oValue = null;

        if (m_currentToken.ToString() == "(")
        {
            PositionNextToken();


            oValue = Relational();

            if (m_currentToken.ToString() == ",")
            {
                return(oValue);
            }

            if (m_currentToken.ToString() != ")")
            {
                throw new ApplicationException("Unmatched parenthesis in equation.");
            }
        }
        else
        {
            switch (m_currentToken.TokenType)
            {
            case Parser.CharType.CT_NUMBER:
                oValue = new CNumber(m_currentToken.ToString());
                break;

            case Parser.CharType.CT_LETTER:
            {
                if (m_nextToken.ToString() == "(")
                {
                    int iIdx = m_slFunctions.IndexOfKey(m_currentToken.ToString());

                    if (iIdx < 0)
                    {
                        throw new ApplicationException("Function not found - " + m_currentToken.ToString());
                    }

                    CFunction oFunc = (CFunction)m_slFunctions.GetByIndex(iIdx);

                    ArrayList alValues = new ArrayList();

                    do
                    {
                        PositionNextToken();
                        oValue = Paren();

                        alValues.Add(oValue);
                    } while (m_currentToken.ToString() == ",");
                    if (m_currentToken.ToString() == ")")
                    {
                        PositionNextToken();
                    }


                    bFunc = true;


                    oValue = oFunc.CreateInstance(alValues);
                }
                else
                {
                    oValue = GetVariableByName(m_currentToken.ToString());
                }

                break;
            }
            }
        }

        if (!bFunc)
        {
            PositionNextToken();
        }

        return(oValue);
    }