Esempio n. 1
0
        /// <returns>true if 'o' is an identifier</returns>
        private bool IsFunctionApplication(Object o)
        {
            FunctionApplication FA = o as FunctionApplication;

            return(FA != null);
        }
Esempio n. 2
0
        private Object Parse(Object[] lexedStr, String str)
        {
            ArrayList L = new ArrayList(lexedStr.Length);
            // parse all subexpressions; replace the Object[]s with FunctionApplications
            for (int i = 0; i < lexedStr.Length; i++) {
                Object[] lexedSubExprStr = lexedStr[i] as Object[];
                if (lexedSubExprStr != null) // if subexpression, then parse that first:
                    L.Add(Parse(lexedSubExprStr, str));
                else L.Add(lexedStr[i]);
            }

            for (int o = 0; o < m_operators.Length; o++)
            {
                // go from left-to-right or from right-to-left (depending on associativity) through the entire list of lexed objects
                String currentOpSymbol = " " + m_operators[o].Symbol; // lexed operator have a ' ' in front of them

                if (m_operators[o].Symbol == " ")
                    L = InsertSpaceOperators(L);

                int startIdx = (m_operators[o].LeftAssociative ? 0 : L.Count-1);
                int direction = (m_operators[o].LeftAssociative ? +1 : -1);
                for (int l = startIdx; (l >= 0) && (l < L.Count); l += direction)
                {
                    String opSymbol = L[l] as String;
                    // check if operator matches:
                    if ((opSymbol != null) && (opSymbol == currentOpSymbol))
                    {
                        // operator matches, check each possible case
                        if (m_operators[o].Unary)
                        {
                            if (m_operators[o].Postfix)
                            {
                                // postfix unary op
                                if (l < 1) continue; // we cannot match a postfix op if nothing sits before it
                                if (IsIdentifier(L[l - 1]) || IsFunctionApplication(L[l - 1]))
                                {
                                    L[l - 1] = new FunctionApplication(m_operators[o].FunctionName, L[l - 1]);
                                    L.RemoveAt(l);
                                    l -= direction;
                                }
                                else continue;
                            }
                            else
                            {
                                // prefix unary op
                                if (l > L.Count-2) continue; // we cannot match a postfix op if nothing sits after it
                                if (IsIdentifier(L[l + 1]) || IsFunctionApplication(L[l + 1]))
                                {
                                    L[l] = new FunctionApplication(m_operators[o].FunctionName, L[l + 1]);
                                    L.RemoveAt(l+1);
                                    l -= direction;
                                }
                                else continue;
                            }
                        }
                        else
                        {
                            // binary op
                            if ((l < 1) || (l > L.Count - 2)) continue; // we cannot match a binary op if nothing sits left or right
                            if ((IsIdentifier(L[l - 1]) || IsFunctionApplication(L[l - 1])) &&
                                (IsIdentifier(L[l + 1]) || IsFunctionApplication(L[l + 1])))
                            {
                                L[l-1] = new FunctionApplication(m_operators[o].FunctionName, L[l - 1], L[l + 1]);
                                L.RemoveRange(l, 2);

                                if (direction>0)
                                    l -= direction;
                            }
                            else continue;
                        }
                    }
                }
            }

            if (L.Count == 0) return null;
            if (L.Count == 1)
                return L[0];
            else throw new Exception("Error parsing '" + str + "'");
        }
Esempio n. 3
0
        private Object Parse(Object[] lexedStr, String str)
        {
            ArrayList L = new ArrayList(lexedStr.Length);

            // parse all subexpressions; replace the Object[]s with FunctionApplications
            for (int i = 0; i < lexedStr.Length; i++)
            {
                Object[] lexedSubExprStr = lexedStr[i] as Object[];
                if (lexedSubExprStr != null) // if subexpression, then parse that first:
                {
                    L.Add(Parse(lexedSubExprStr, str));
                }
                else
                {
                    L.Add(lexedStr[i]);
                }
            }


            for (int o = 0; o < m_operators.Length; o++)
            {
                // go from left-to-right or from right-to-left (depending on associativity) through the entire list of lexed objects
                String currentOpSymbol = " " + m_operators[o].Symbol; // lexed operator have a ' ' in front of them

                if (m_operators[o].Symbol == " ")
                {
                    L = InsertSpaceOperators(L);
                }

                int startIdx  = (m_operators[o].LeftAssociative ? 0 : L.Count - 1);
                int direction = (m_operators[o].LeftAssociative ? +1 : -1);
                for (int l = startIdx; (l >= 0) && (l < L.Count); l += direction)
                {
                    String opSymbol = L[l] as String;
                    // check if operator matches:
                    if ((opSymbol != null) && (opSymbol == currentOpSymbol))
                    {
                        // operator matches, check each possible case
                        if (m_operators[o].Unary)
                        {
                            if (m_operators[o].Postfix)
                            {
                                // postfix unary op
                                if (l < 1)
                                {
                                    continue;        // we cannot match a postfix op if nothing sits before it
                                }
                                if (IsIdentifier(L[l - 1]) || IsFunctionApplication(L[l - 1]))
                                {
                                    L[l - 1] = new FunctionApplication(m_operators[o].FunctionName, L[l - 1]);
                                    L.RemoveAt(l);
                                    l -= direction;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                // prefix unary op
                                if (l > L.Count - 2)
                                {
                                    continue;                // we cannot match a postfix op if nothing sits after it
                                }
                                if (IsIdentifier(L[l + 1]) || IsFunctionApplication(L[l + 1]))
                                {
                                    L[l] = new FunctionApplication(m_operators[o].FunctionName, L[l + 1]);
                                    L.RemoveAt(l + 1);
                                    l -= direction;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            // binary op
                            if ((l < 1) || (l > L.Count - 2))
                            {
                                continue;                               // we cannot match a binary op if nothing sits left or right
                            }
                            if ((IsIdentifier(L[l - 1]) || IsFunctionApplication(L[l - 1])) &&
                                (IsIdentifier(L[l + 1]) || IsFunctionApplication(L[l + 1])))
                            {
                                L[l - 1] = new FunctionApplication(m_operators[o].FunctionName, L[l - 1], L[l + 1]);
                                L.RemoveRange(l, 2);

                                if (direction > 0)
                                {
                                    l -= direction;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
            }

            if (L.Count == 0)
            {
                return(null);
            }
            if (L.Count == 1)
            {
                return(L[0]);
            }
            else
            {
                throw new Exception("Error parsing '" + str + "'");
            }
        }