Ejemplo n.º 1
0
        internal void ProcessWhile()
        {
            int startWhileCondition = m_currentChar;

            // A heuristic check against an infinite loop.
            int cycles = 0;
            int START_CHECK_INF_LOOP = CHECK_AFTER_LOOPS / 2;

            Parser.Result argCache1 = null;
            Parser.Result argCache2 = null;

            bool stillValid = true;

            while (stillValid)
            {
                m_currentChar = startWhileCondition;

                Parser.Result arg1       = GetNextIfToken();
                string        comparison = Utils.GetComparison(m_data, ref m_currentChar);
                Parser.Result arg2       = GetNextIfToken();

                stillValid = EvalCondition(arg1, comparison, arg2);
                int startSkipOnBreakChar = m_currentChar;

                if (!stillValid)
                {
                    break;
                }

                // Check for an infinite loop if we are comparing same values:
                if (++cycles % START_CHECK_INF_LOOP == 0)
                {
                    if (cycles >= MAX_LOOPS || (arg1.IsEqual(argCache1) && arg2.IsEqual(argCache2)))
                    {
                        throw new ArgumentException("Looks like an infinite loop after " +
                                                    cycles + " cycles.");
                    }
                    argCache1 = arg1;
                    argCache2 = arg2;
                }

                Parser.Result result = ProcessBlock();
                if (result is Break)
                {
                    m_currentChar = startSkipOnBreakChar;
                    break;
                }
            }

            // The while condition is not true anymore: must skip the whole while
            // block before continuing with next statements.
            SkipBlock();
        }
Ejemplo n.º 2
0
        internal Parser.Result ProcessIf()
        {
            int startIfCondition = m_currentChar;

            Parser.Result result = null;

            Parser.Result arg1       = GetNextIfToken();
            string        comparison = Utils.GetComparison(m_data, ref m_currentChar);

            Parser.Result arg2 = GetNextIfToken();

            bool isTrue = EvalCondition(arg1, comparison, arg2);

            if (isTrue)
            {
                result = ProcessBlock();

                if (result is Continue || result is Break)
                {
                    // Got here from the middle of the if-block. Skip it.
                    m_currentChar = startIfCondition;
                    SkipBlock();
                }
                SkipRestBlocks();

                return(result);
            }

            // We are in Else. Skip everything in the If statement.
            SkipBlock();

            int    endOfToken = m_currentChar;
            string nextToken  = Utils.GetNextToken(m_data, ref endOfToken);

            if (ELSE_IF_LIST.Contains(nextToken))
            {
                m_currentChar = endOfToken + 1;
                result        = ProcessIf();
            }
            else if (ELSE_LIST.Contains(nextToken))
            {
                m_currentChar = endOfToken + 1;
                result        = ProcessBlock();
            }

            return(new Parser.Result());
        }