Example #1
0
        public static void Reconstruct(String path, String output = "stdout")
        {
            if (!File.Exists(path))
            {
                Error("File doesn't exists.");
                return;
            }

            var code        = File.ReadAllText(path);
            var environment = new LuaEnvironment( );

            environment.AddAnalyser("fixparents", new Analysis.FixParentsAnalyser( ));
            environment.AddAnalyser("gotocheck", new Analysis.GotoTargetCheckAnalyser( ));
            environment.AddFolder("ConstantFolder 1st pass", new Folder.ConstantASTFolder( ));
            environment.AddAnalyser("AssignmentAnalyser", new Analysis.AssignmentAnalyser( ));
            environment.AddFolder("AssignmentFolder", new Folder.AssignmentFolder( ));
            //environment.AddFolder ( "ConstantFolder 2nd pass", new Folder.ConstantASTFolder ( ) );

            var sw1 = Stopwatch.StartNew( );

            Env.EnvFile file = environment.ProcessFile(path, code);
            sw1.Stop( );
            foreach (Error err in file.Errors)
            {
                Console.WriteLine($"{err.Location} [{err.Type}] {err.Message}");
            }

            var sw2 = Stopwatch.StartNew( );

            using (var mem = new MemoryStream( ))
                using (Stream fileStream = output == "stdout" ? Console.OpenStandardOutput( ) : File.OpenWrite(output))
                    using (var writer = new StreamWriter(fileStream, Encoding.UTF8))
                    {
                        var reconstructor = new FormattedLuaReconstructor( );
                        reconstructor.Construct(file.AST, mem);
                        writer.Write(Encoding.UTF8.GetString(mem.ToArray( )));
                    }
            sw2.Stop( );
            Console.WriteLine($"Time elapsed on parsing + analysing + folding: {HumanTime ( sw1 )}");
            Console.WriteLine($"Time elapsed on serializing code: {HumanTime ( sw2 )}");
        }