コード例 #1
0
        public CCompoundStatement parseDoWhileStatement(Queue <CMatchedData> matchedData)
        {
            CToken[] currTokens = matchedData.Dequeue().getTokens();

            CStatement loopStatement, nextStatement;

            CLabeledStatement continueTarget, breakTarget;

            CJumpStatement continueLoop;

            int i;

            if (currTokens[0].tokenType != CTokenType.DO)
            {
                throw new System.ArgumentException("matchedData should start with a do-while statement!", "matchedData");
            }

            loopStatement = parseStatement(matchedData);

            if (loopStatement.nextStatement is CLabeledStatement &&
                loopStatement.nextStatement.nextStatement is CLabeledStatement)
            {
                nextStatement = ((CLabeledStatement)loopStatement.nextStatement.nextStatement).labeledStatement;
            }
            else
            {
                throw new System.ArgumentException("incorrect do-while statement!", "matchedData");
            }

            /* Create loop */

            breakTarget = new CLabeledStatement("do-while-loop: break target", null,
                                                nextStatement != null ? nextStatement : new CExpressionStatement(new CExpression(""), null));

            continueLoop = new CJumpStatement("continue", null);

            continueTarget = new CLabeledStatement("do-while-loop: continue target", null, loopStatement);

            continueLoop.setTargetStatement(continueTarget);

            loopStatement.setNextStatement(
                new CConditionalStatement(
                    ((CConditionalStatement)((CLabeledStatement)loopStatement.nextStatement).labeledStatement).condition,
                    continueLoop,
                    null
                    )
                );

            for (i = 0; i < this.breakJumps.Count; i++)
            {
                if (continueTarget.contains(this.breakJumps[i]))
                {
                    this.breakJumps[i].setTargetStatement(breakTarget);
                    this.breakJumps.RemoveAt(i);
                    i--;
                }
            }

            for (i = 0; i < this.continueJumps.Count; i++)
            {
                if (continueTarget.contains(this.continueJumps[i]))
                {
                    this.continueJumps[i].setTargetStatement(continueTarget);
                    this.continueJumps.RemoveAt(i);
                    i--;
                }
            }

            return(new CCompoundStatement(
                       new CStatement[] { continueTarget, loopStatement.nextStatement },
                       breakTarget
                       ));
        }
コード例 #2
0
        public CLabeledStatement parseWhileStatement(Queue <CMatchedData> matchedData)
        {
            CToken[] currTokens = matchedData.Dequeue().getTokens();

            CExpression conditionExpression;

            CStatement loopStatement, nextStatement;

            CLabeledStatement continueTarget, breakTarget;

            CJumpStatement continueLoop;

            int i;

            if (currTokens[0].tokenType != CTokenType.WHILE)
            {
                throw new System.ArgumentException("matchedData should start with a while statement!", "matchedData");
            }

            conditionExpression = new CExpression(getText(currTokens, 2, currTokens.Length - 1));

            loopStatement = parseStatement(matchedData);

            /* Create loop */

            nextStatement = loopStatement.nextStatement;

            breakTarget = new CLabeledStatement("while-loop: break target", null,
                                                nextStatement != null ? nextStatement : new CExpressionStatement(new CExpression(""), null));

            continueLoop = new CJumpStatement("continue", null);
            loopStatement.setNextStatement(continueLoop);

            continueTarget = new CLabeledStatement("while-loop: continue target", null, new CConditionalStatement(
                                                       conditionExpression,
                                                       new CCompoundStatement(new CStatement[] { loopStatement, continueLoop }, null),
                                                       breakTarget
                                                       ));

            continueLoop.setTargetStatement(continueTarget);

            for (i = 0; i < this.breakJumps.Count; i++)
            {
                if (continueTarget.contains(this.breakJumps[i]))
                {
                    this.breakJumps[i].setTargetStatement(breakTarget);
                    this.breakJumps.RemoveAt(i);
                    i--;
                }
            }

            for (i = 0; i < this.continueJumps.Count; i++)
            {
                if (continueTarget.contains(this.continueJumps[i]))
                {
                    this.continueJumps[i].setTargetStatement(continueTarget);
                    this.continueJumps.RemoveAt(i);
                    i--;
                }
            }

            return(continueTarget);
        }