예제 #1
0
        internal string ToCode(GrammaticalObject go)
        {
            int moddedCodon = 0;

            if (go[NewNonTerminalName] == null || go[NewNonTerminalName].Count == 0)
            {
                throw new EmptyProductionRuleCollection(NewNonTerminalName, go);
            }

            if (go[NewNonTerminalName].Count > 1)
            {
                byte codon = go.instance.NextCodon();

                if (go.FirstUsedIndex == -1)
                {
                    go.FirstUsedIndex = go.instance.codonPosition;
                }
                go.LastUsedIndex = go.instance.codonPosition;

                moddedCodon = (int)codon % go[NewNonTerminalName].Count;
            }

            ProductionRule chosenProductionRule = go[NewNonTerminalName][moddedCodon];

            return(go.ConvertToCode(chosenProductionRule));
        }
예제 #2
0
        private static ProductionRule CreateForLoop(OneMaxGO_NoImprovements go)
        {
            if (go["unusedVariables"].Count == 0)
            {
                return(new ProductionRule((NonTerminal)"loop"));
            }
            string chosenVariable         = go.ConvertToCode(new ProductionRule((NonTerminal)"unusedVariables"));
            OneMaxGO_NoImprovements newGO = go.CreateChild <OneMaxGO_NoImprovements>();
            ProductionRule          variableProductionRule = newGO["unusedVariables"].Find(x => ((Terminal)x[0]).Code == chosenVariable);

            newGO["unusedVariables"].Remove(variableProductionRule);
            newGO["variable"].Add(variableProductionRule);

            return(new ProductionRule(new Terminal("for(int " + chosenVariable + " = 0; " + chosenVariable + " < "), (NonTerminal)"value", new Terminal("; " + chosenVariable + "++)\n{\n"), new NonTerminal("code", g => newGO), (Terminal)"\n}\n"));
        }
예제 #3
0
        internal string ConvertToCode(ProductionRule productionRule, GrammaticalObject go)
        {
            string finalCode = "";

            foreach (Expression expr in productionRule)
            {
                if (expr is Terminal)
                {
                    finalCode += (Terminal)expr;
                }
                else if (expr is NonTerminal)
                {
                    NonTerminal nonTerminal = (NonTerminal)expr;

                    if (nonTerminal.NewGrammaticalObjectLambda == null)
                    {
                        finalCode += nonTerminal.ToCode(go);
                    }
                    else
                    {
                        finalCode += nonTerminal.ToCode(nonTerminal.NewGrammaticalObjectLambda(go));
                    }
                }
                else if (expr is ErrorCheck)
                {
                    ((ErrorCheck)expr).Check(go);
                    //If an exception is not thrown, the check was passed
                    _currentGenerationStats.passedErrorChecks++;
                }
                else if (expr is ObjectAction)
                {
                    (((ObjectAction)expr)).Execute(go);
                }
                else if (expr is ObjectFunction)
                {
                    finalCode += ConvertToCode(((ObjectFunction)expr).Execute(go), go);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            return(finalCode);
        }