Exemplo n.º 1
0
        private BlockSyntax GenBlock(bool root, int numStatements = -1)
        {
            if (numStatements == -1)
            {
                numStatements = Options.BlockStatementCountDist.Sample(Random.Rng);
            }

            _level++;

            ScopeFrame scope = new ScopeFrame();

            _scope.Add(scope);

            if (root)
            {
                scope.Variables.AddRange(Parameters);
            }

            BlockSyntax block = Block(GenStatements());

            _scope.RemoveAt(_scope.Count - 1);
            _level--;

            return(block);

            IEnumerable <StatementSyntax> GenStatements()
            {
                StatementSyntax retStmt = null;

                for (int i = 0; i < numStatements; i++)
                {
                    StatementSyntax stmt = GenStatement();
                    if (stmt is ReturnStatementSyntax)
                    {
                        retStmt = stmt;
                        break;
                    }

                    yield return(stmt);
                }

                if (root && retStmt == null)
                {
                    retStmt = GenReturn();
                }

                if (Options.EnableChecksumming)
                {
                    foreach (StatementSyntax stmt in GenChecksumming(scope.Variables, _genChecksumSiteId))
                    {
                        yield return(stmt);
                    }
                }

                if (retStmt != null)
                {
                    yield return(retStmt);
                }
            }
        }
Exemplo n.º 2
0
        private BlockSyntax GenBlock(bool root, int numStatements = -1)
        {
            if (numStatements == -1)
            {
                numStatements = Options.BlockStatementCountDist.Sample(Random.Rng);
            }

            _level++;

            ScopeFrame scope = new ScopeFrame();

            _scope.Add(scope);

            if (root)
            {
                scope.Variables.AddRange(Parameters);
            }

            BlockSyntax block = Block(GenStatements());

            _scope.RemoveAt(_scope.Count - 1);

            _level--;

            return(block);

            IEnumerable <StatementSyntax> GenStatements()
            {
                StatementSyntax retStmt      = null;
                int             numGenerated = 0;

                while (true)
                {
                    StatementSyntax stmt = GenStatement(allowReturn: !root);

                    if (stmt is ReturnStatementSyntax)
                    {
                        retStmt = stmt;
                        break;
                    }

                    NumStatements++;
                    yield return(stmt);

                    numGenerated++;
                    if (numGenerated < numStatements)
                    {
                        continue;
                    }

                    // For first block we ensure we get a minimum amount of statements
                    if (root && _funcIndex == 0)
                    {
                        if (_funcs.Sum(f => f.NumStatements) < Options.ProgramMinStatements)
                        {
                            continue;
                        }
                    }

                    break;
                }

                if (Options.EnableChecksumming)
                {
                    foreach (StatementSyntax stmt in GenChecksumming(scope.Variables, _genChecksumSiteId))
                    {
                        yield return(stmt);
                    }
                }

                if (root && retStmt == null && ReturnType != null)
                {
                    retStmt = GenReturn();
                }

                if (retStmt != null)
                {
                    NumStatements++;
                    yield return(retStmt);
                }
            }
        }