Exemple #1
0
        public static CoreNode Optimize(CoreNode code)
        {
            int pass = 0;

            codes = new Dictionary <string, CoreNode>()
            {
                { "<INITIAL>", code }
            };
            dependencyGraph.Add("<INITIAL>", new HashSet <string>());

            BlockNode combined;
            bool      anyOptimized;

            do
            {
                anyOptimized    = false;
                newDependencies = false;

                combined = new BlockNode()
                {
                    Context = code.Code.Context
                };
                foreach (var c in codes)
                {
                    combined.Code.Add(c.Value.Code);
                }

                File.WriteAllText($"_optimized_pass_{pass++}.fbl", combined.ToCodeString(0));

                var oldCodes = new Dictionary <string, CoreNode>(codes);
                foreach (var c in oldCodes)
                {
                    currentCodeName = c.Key;
                    var optimized = MakePass(c.Value);
                    if (optimized != c.Value)
                    {
                        codes[c.Key] = optimized;
                        anyOptimized = true;
                    }
                }
            } while (anyOptimized || newDependencies);

            combined = new BlockNode()
            {
                Context = code.Code.Context
            };
            foreach (var ins in Sort(codes.Keys, x => dependencyGraph[x]))
            {
                var c = codes[ins].Code;
                if (c is BlockNode cb)
                {
                    combined.Code.AddRange(cb.Code);
                }
                else
                {
                    combined.Code.Add(c);
                }
            }

            var opt = new CoreNode()
            {
                Code = combined
            };

            do
            {
                anyOptimized = false;
                File.WriteAllText($"_optimized_pass_{pass++}.fbl", opt.ToCodeString(0));

                code = opt;
                opt  = MakePass(code);
            } while (opt != code);

            return(opt);
        }