Пример #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);
        }
Пример #2
0
        private SJumpStatement ParseJumpStatement(Queue <PMatchedData> matchedData)
        {
            var            currTokens = matchedData.Dequeue().GetTokens();
            SJumpStatement result;

            if (currTokens[0].TokenType == PTokenType.Return)
            {
                if (currTokens.Length > 1)
                {
                    var returnExpression = new SExpression(GetText(currTokens, 1, currTokens.Length - 1));
                    result = new SJumpStatement(returnExpression, ParseStatement(matchedData));
                }
                else
                {
                    result = new SJumpStatement("return", ParseStatement(matchedData));
                }

                result.Visible = false;
                _returnJumps.Add(result);
            }
            else
            {
                result = new SJumpStatement(GetText(currTokens, 0, currTokens.Length - 1), ParseStatement(matchedData));

                if (currTokens[0].TokenType == PTokenType.Goto)
                {
                    foreach (var statement in _labels)
                    {
                        if (statement.CodeString == result.TargetIdentifier)
                        {
                            result.SetTargetStatement(statement);

                            return(result);
                        }
                    }

                    _gotoJumps.Add(result);
                }
                else if (currTokens[0].TokenType == PTokenType.Break)
                {
                    _breakJumps.Add(result);
                }
                else if (currTokens[0].TokenType == PTokenType.Continue)
                {
                    _continueJumps.Add(result);
                }
                else
                {
                    throw new ArgumentException("matchedData does not represent a valid jump statement!",
                                                nameof(matchedData));
                }
            }

            result.Visible = false;
            return(result);
        }
Пример #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));
        }
Пример #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
                       ));
        }