Ejemplo n.º 1
0
        private SLabeledStatement ParseWhileStatement(Queue <PMatchedData> matchedData)
        {
            var currTokens = matchedData.Dequeue().GetTokens();

            int i;

            if (currTokens[0].TokenType != PTokenType.While)
            {
                throw new ArgumentException("matchedData should start with a while statement!", nameof(matchedData));
            }

            var conditionExpression = new SExpression(GetText(currTokens, 2, currTokens.Length - 1));
            var loopStatement       = ParseStatement(matchedData);
            var nextStatement       = loopStatement.NextStatement;

            var breakTarget = new SLabeledStatement("while-loop: break target", null, nextStatement)
            {
                Visible = false
            };

            var continueLoop = new SJumpStatement("continue", null);

            loopStatement.SetNextStatement(continueLoop);
            continueLoop.Visible = false;

            var continueTarget = new SLabeledStatement("while-loop: continue target", null, new SConditionalStatement(
                                                           conditionExpression,
                                                           new SCompoundStatement(new[] { loopStatement, continueLoop }, null),
                                                           breakTarget
                                                           ))
            {
                Visible = false
            };

            continueLoop.SetTargetStatement(continueTarget);

            for (i = 0; i < _breakJumps.Count; i++)
            {
                if (continueTarget.Contains(_breakJumps[i]))
                {
                    _breakJumps[i].SetTargetStatement(breakTarget);
                    _breakJumps.RemoveAt(i);
                    i--;
                }
            }

            for (i = 0; i < _continueJumps.Count; i++)
            {
                if (continueTarget.Contains(_continueJumps[i]))
                {
                    _continueJumps[i].SetTargetStatement(continueTarget);
                    _continueJumps.RemoveAt(i);
                    i--;
                }
            }

            return(continueTarget);
        }
Ejemplo n.º 2
0
        private SLabeledStatement ParseLabeledStatement(Queue <PMatchedData> matchedData)
        {
            var         currTokens     = matchedData.Dequeue().GetTokens();
            var         labelName      = currTokens[0].TokenCode;
            SExpression caseExpression = null;

            var labeled = ParseStatement(matchedData);

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

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

            var result = new SLabeledStatement(labelName, caseExpression, labeled);

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

                for (i = 0; i < _gotoJumps.Count; i++)
                {
                    if (_gotoJumps[i].TargetIdentifier == labelName)
                    {
                        _gotoJumps[i].SetTargetStatement(result);
                        _gotoJumps.RemoveAt(i);

                        return(result);
                    }
                }

                _labels.Add(result);
            }

            return(result);
        }
Ejemplo n.º 3
0
        private SCompoundStatement ParseForStatement(Queue <PMatchedData> matchedData)
        {
            var currTokens = matchedData.Dequeue().GetTokens();

            int i, j;

            if (currTokens[0].TokenType != PTokenType.For)
            {
                throw new ArgumentException("matchedData should start with a for statement!", nameof(matchedData));
            }

            for (i = 0; i < currTokens.Length && currTokens[i].TokenCode != ";"; i++)
            {
                ;
            }
            var startExpression = new SExpression(GetText(currTokens, 2, i));

            for (j = i + 1; j < currTokens.Length && currTokens[j].TokenCode != ";"; j++)
            {
                ;
            }
            var conditionExpression = new SExpression(GetText(currTokens, i + 1, j));
            var iterationExpression = new SExpression(GetText(currTokens, j + 1, currTokens.Length - 1));
            var loopStatement       = ParseStatement(matchedData);
            var nextStatement       = loopStatement.NextStatement;

            var breakTarget = new SLabeledStatement("for-loop: break target", null, nextStatement)
            {
                Visible = false
            };
            var continueLoop = new SJumpStatement("for-loop: next iteration", null)
            {
                Visible = false
            };
            var iteration = new SExpressionStatement(iterationExpression, continueLoop)
            {
                Visible = false
            };
            var continueTarget = new SLabeledStatement("for-loop: continue target", null, iteration)
            {
                Visible = false
            };

            loopStatement.SetNextStatement(continueTarget);


            var iterationStart = new SLabeledStatement("for-loop: iteration start", null, new SConditionalStatement(
                                                           conditionExpression,
                                                           new SCompoundStatement(new[]
            {
                loopStatement,
                continueTarget,
                continueLoop
            }, null),
                                                           null
                                                           ))
            {
                Visible = false
            };

            continueLoop.SetTargetStatement(iterationStart);

            for (i = 0; i < _breakJumps.Count; i++)
            {
                if (loopStatement.Contains(_breakJumps[i]))
                {
                    _breakJumps[i].SetTargetStatement(breakTarget);
                    _breakJumps.RemoveAt(i);
                    i--;
                }
            }

            for (i = 0; i < _continueJumps.Count; i++)
            {
                if (loopStatement.Contains(_continueJumps[i]))
                {
                    _continueJumps[i].SetTargetStatement(continueTarget);
                    _continueJumps.RemoveAt(i);
                    i--;
                }
            }

            var cExpression = new SExpressionStatement(startExpression, iterationStart)
            {
                Visible = false
            };

            return(new SCompoundStatement(
                       new SStatement[]
            {
                cExpression,
                iterationStart
            },
                       breakTarget));
        }
Ejemplo n.º 4
0
        private SCompoundStatement ParseDoWhileStatement(Queue <PMatchedData> matchedData)
        {
            var        currTokens = matchedData.Dequeue().GetTokens();
            SStatement nextStatement;
            int        i;

            if (currTokens[0].TokenType != PTokenType.Do)
            {
                throw new ArgumentException("matchedData should start with a do-while statement!", nameof(matchedData));
            }

            var loopStatement = ParseStatement(matchedData);

            loopStatement.Visible = false;

            if (loopStatement.NextStatement is SLabeledStatement &&
                loopStatement.NextStatement.NextStatement is SLabeledStatement)
            {
                nextStatement = ((SLabeledStatement)loopStatement.NextStatement.NextStatement).LabeledStatement;
            }
            else
            {
                throw new ArgumentException("incorrect do-while statement!", nameof(matchedData));
            }

            var breakTarget =
                new SLabeledStatement("do-while-loop: break target", null, nextStatement)
            {
                Visible = false
            };
            var continueLoop = new SJumpStatement("continue", null)
            {
                Visible = false
            };
            var continueTarget =
                new SLabeledStatement("do-while-loop: continue target", null, loopStatement)
            {
                Visible = false
            };

            continueLoop.SetTargetStatement(continueTarget);

            var statement = new SConditionalStatement(
                ((SConditionalStatement)((SLabeledStatement)loopStatement.NextStatement).LabeledStatement).Condition,
                continueLoop,
                null
                );

            loopStatement.SetNextStatement(
                statement
                );

            for (i = 0; i < _breakJumps.Count; i++)
            {
                if (continueTarget.Contains(_breakJumps[i]))
                {
                    _breakJumps[i].SetTargetStatement(breakTarget);
                    _breakJumps.RemoveAt(i);
                    i--;
                }
            }

            for (i = 0; i < _continueJumps.Count; i++)
            {
                if (continueTarget.Contains(_continueJumps[i]))
                {
                    _continueJumps[i].SetTargetStatement(continueTarget);
                    _continueJumps.RemoveAt(i);
                    i--;
                }
            }

            var statements = new[] { continueTarget, loopStatement.NextStatement };

            return(new SCompoundStatement(
                       statements, breakTarget
                       ));
        }