public void setTargetStatement(CLabeledStatement targetStatement)
 {
     if (!this.targetStatementSet)
     {
         this.targetStatement    = targetStatement;
         this.targetStatementSet = true;
     }
 }
Example #2
0
        public CLabeledStatement parseLabeledStatement(Queue <CMatchedData> matchedData)
        {
            CToken[]    currTokens     = matchedData.Dequeue().getTokens();
            string      labelName      = currTokens[0].tokenCode;
            CExpression caseExpression = null;

            CStatement labeled = parseStatement(matchedData);

            CLabeledStatement result;

            if (labelName == "case")
            {
                caseExpression = new CExpression(getText(currTokens, 1, currTokens.Length - 1));
            }

            if (labeled == null)
            {
                throw new System.InvalidOperationException("error: label at end of compound statement!");
            }

            result = new CLabeledStatement(labelName, caseExpression, labeled);

            if (labelName == "case" || labelName == "default")
            {
                this.caseLabels.Add(result);
            }
            else
            {
                int i;

                /* Match label with goto */
                for (i = 0; i < this.gotoJumps.Count; i++)
                {
                    if (this.gotoJumps[i].targetIdentifier == labelName)
                    {
                        this.gotoJumps[i].setTargetStatement(result);
                        this.gotoJumps.RemoveAt(i);

                        return(result);
                    }
                }

                this.labels.Add(result);
            }

            return(result);
        }
Example #3
0
        public CCompoundStatement parseForStatement(Queue <CMatchedData> matchedData)
        {
            CToken[] currTokens = matchedData.Dequeue().getTokens();

            CExpression startExpression, conditionExpression, iterationExpression;

            CStatement loopStatement, nextStatement;

            CLabeledStatement continueTarget, breakTarget, iterationStart;

            CJumpStatement continueLoop;

            int i, j;

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

            for (i = 0; i < currTokens.Length && currTokens[i].tokenCode != ";"; i++)
            {
                ;
            }
            startExpression = new CExpression(getText(currTokens, 2, i));

            for (j = i + 1; j < currTokens.Length && currTokens[j].tokenCode != ";"; j++)
            {
                ;
            }
            conditionExpression = new CExpression(getText(currTokens, i + 1, j));

            iterationExpression = new CExpression(getText(currTokens, j + 1, currTokens.Length - 1));

            loopStatement = parseStatement(matchedData);


            nextStatement = loopStatement.nextStatement;

            /* Create loop */

            nextStatement = loopStatement.nextStatement;

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

            continueLoop = new CJumpStatement("for-loop: next iteration", null);

            continueTarget = new CLabeledStatement("for-loop: continue target", null,
                                                   new CExpressionStatement(iterationExpression, continueLoop));
            loopStatement.setNextStatement(continueTarget);

            iterationStart = new CLabeledStatement("for-loop: iteration start", null, new CConditionalStatement(
                                                       conditionExpression,
                                                       new CCompoundStatement(new CStatement[]
            {
                loopStatement,
                continueTarget,
                continueLoop
            }, null),
                                                       null
                                                       ));

            continueLoop.setTargetStatement(iterationStart);

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

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

            return(new CCompoundStatement(
                       new CStatement[]
            {
                new CExpressionStatement(startExpression, iterationStart),
                iterationStart
            },
                       breakTarget));
        }
Example #4
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
                       ));
        }
Example #5
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);
        }