コード例 #1
0
        public Grammar CombineDataTypes(Options options)
        {
            // parse grammars again to make sure they have not been changed in the meantime
            Dictionary <string, Grammar> pythonGrammars = GrammarParser.ParseGrammarsByEmbededArchive(ArchiveName);
            var grammar = options.Tree ? pythonGrammars[StructureTree] : pythonGrammars[Structure];

            var combinations = GetCombinations(options.Datatypes);

            foreach (var grammarName in combinations)
            {
                var cur = pythonGrammars[grammarName];
                grammar.Combine(cur);
            }

            var tempVariables   = GetTempVariables(options.Datatypes, options.NumberOfInputVariables);
            var inputVariables  = GetVariables(options.Input, InputPrefix);
            var outputVariables = GetVariables(options.Output, OutputPrefix);

            // add initialisation to variables
            foreach (var p in grammar.Rules[grammar.Root].Productions)
            {
                p.Parts[0] = tempVariables.Item1 + outputVariables.Item1 + p.Parts[0];
            }

            // add temp variables
            var tempVariableNames = tempVariables.Item2;

            foreach (var item in tempVariableNames)
            {
                var p = grammar.Rules[dataTypeMapping[item.Key].VariableRule].Productions;
                foreach (var v in item.Value)
                {
                    p.Add(new Production()
                    {
                        Parts = new List <string>()
                        {
                            v
                        }
                    });
                }
            }

            // add output variables
            var outputVariableNames = outputVariables.Item2;

            foreach (var item in outputVariableNames)
            {
                var p = grammar.Rules[dataTypeMapping[item.Key].VariableRule].Productions;
                foreach (var v in item.Value)
                {
                    p.Add(new Production()
                    {
                        Parts = new List <string>()
                        {
                            v
                        }
                    });
                }
            }

            // add input variables
            var inputVariableNames = inputVariables.Item2;

            foreach (var item in inputVariableNames)
            {
                var p = grammar.Rules[dataTypeMapping[item.Key].VariableRule].Productions;
                foreach (var v in item.Value)
                {
                    p.Add(new Production()
                    {
                        Parts = new List <string>()
                        {
                            v
                        }
                    });
                }
            }

            if (options.Recursion)
            {
                var pre = grammar.Rules[RootRule].Productions;
                if (pre.Count == 1)
                {
                    // should only have one rule
                    pre.First().Parts[0] = pre.First().Parts[0] + RecursionPredifined;
                }
                else
                {
                    throw new ArgumentException("<predefined> should only have one production!?!");
                }

                //add recursion
                var p      = grammar.Rules[CallRule].Productions;
                var input  = options.Input.Select(x => dataTypeMapping[x].Rule).ToList();
                var output = options.Output.Select(x => dataTypeMapping[x].VariableRule).ToList();

                var parts = new List <string>()
                {
                    RecursionBefore
                };
                parts.AddRange(Enumerable.Repeat(", ", output.Count - 1));
                parts.Add(String.Format(" = {0}(", EvolvedProgramName));
                parts.AddRange(Enumerable.Repeat(", ", input.Count() - 1));
                parts.Add(")" + RecursionAfter);
                var rules = output;
                rules.AddRange(input);
                p.Add(new Production()
                {
                    Parts = parts, RuleDependency = rules
                });

                //add return statement
                var returnStatement = String.Format("return {0}", String.Join(", ", outputVariables.Item3));
                p.Add(new Production()
                {
                    Parts = new List <string>()
                    {
                        returnStatement
                    }
                });
            }

            // Remove types that might have been added, but that are not set as data types
            // will be completely removed after trimming
            foreach (var dataType in Enum.GetValues(typeof(DataType)).Cast <DataType>().Where(x => x != DataType.Char).Except(options.Datatypes))
            {
                if (grammar.Rules.ContainsKey(dataTypeMapping[dataType].Rule))
                {
                    grammar.Rules.Remove(dataTypeMapping[dataType].Rule);
                }
            }

            return(grammar);
        }
コード例 #2
0
        public Grammar CombineDataTypes(Options options)
        {
            // parse grammars again to make sure they have not been changed in the meantime
            Dictionary <string, Grammar> pythonGrammars = GrammarParser.ParseGrammarsByEmbededArchive(ArchiveName);
            var grammar = pythonGrammars[Structure];

            //var combinations = GetCombinations(options.Datatypes);
            //foreach (var grammarName in combinations) {
            //  var cur = pythonGrammars[grammarName];
            //  grammar.Combine(cur);
            //}

            var tempVariables   = GetTempVariables(options.Datatypes, options.NumberOfInputVariables);
            var inputVariables  = GetVariables(options.Input, InputPrefix);
            var outputVariables = GetVariables(options.Output, OutputPrefix);

            // add initialisation to variables
            foreach (var p in grammar.Rules[grammar.Root].Productions)
            {
                p.Parts[0] = tempVariables.Item1 + outputVariables.Item1 + p.Parts[0];
            }

            // add temp variables
            var tempVariableNames = tempVariables.Item2;

            foreach (var item in tempVariableNames)
            {
                var p = grammar.Rules[dataTypeMapping[item.Key].VariableRule].Productions;
                foreach (var v in item.Value)
                {
                    p.Add(new Production()
                    {
                        Parts = new List <string>()
                        {
                            v
                        }
                    });
                }
            }

            // add output variables
            var outputVariableNames = outputVariables.Item2;

            foreach (var item in outputVariableNames)
            {
                var p = grammar.Rules[dataTypeMapping[item.Key].VariableRule].Productions;
                foreach (var v in item.Value)
                {
                    p.Add(new Production()
                    {
                        Parts = new List <string>()
                        {
                            v
                        }
                    });
                }
            }

            // add input variables
            var inputVariableNames = inputVariables.Item2;

            foreach (var item in inputVariableNames)
            {
                var p = grammar.Rules[dataTypeMapping[item.Key].VariableRule].Productions;
                foreach (var v in item.Value)
                {
                    p.Add(new Production()
                    {
                        Parts = new List <string>()
                        {
                            v
                        }
                    });
                }
            }

            // Probably not necessary for DeepCoderDSLConstructor
            // Remove types that might have been added, but that are not set as data types
            // will be completely removed after trimming
            //foreach (var dataType in Enum.GetValues(typeof(DataType)).Cast<DataType>().Except(options.Datatypes).Except(new List<DataType>() { DataType.Boolean, DataType.Float, DataType.String, DataType.ListBoolean, DataType.ListFloat, DataType.ListString })) {
            //  if (grammar.Rules.ContainsKey(dataTypeMapping[dataType].Rule)) grammar.Rules.Remove(dataTypeMapping[dataType].Rule);
            //}

            return(grammar);
        }