예제 #1
0
        public static bool Compile(DMCompilerSettings settings)
        {
            Settings = settings;
            if (Settings.Files == null)
            {
                return(false);
            }

            //TODO: Only use InvariantCulture where necessary instead of it being the default
            CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

            _compileStartTime = DateTime.Now;

            if (settings.SuppressUnimplementedWarnings)
            {
                Warning(new CompilerWarning(Location.Unknown, "Unimplemented proc & var warnings are currently suppressed"));
            }

            DMPreprocessor preprocessor = Preprocess(settings.Files);

            if (settings.DumpPreprocessor)
            {
                StringBuilder result = new();
                foreach (Token t in preprocessor.GetResult())
                {
                    result.Append(t.Text);
                }

                string output = Path.Join(Path.GetDirectoryName(settings.Files?[0]) ?? AppDomain.CurrentDomain.BaseDirectory, "preprocessor_dump.dm");
                File.WriteAllText(output, result.ToString());
                Console.WriteLine($"Preprocessor output dumped to {output}");
            }

            bool successfulCompile = preprocessor is not null && Compile(preprocessor.GetResult());

            if (successfulCompile)
            {
                Console.WriteLine($"Compilation succeeded with {WarningCount} warnings");

                //Output file is the first file with the extension changed to .json
                string outputFile        = Path.ChangeExtension(settings.Files[0], "json");
                List <DreamMapJson> maps = ConvertMaps(preprocessor.IncludedMaps);

                SaveJson(maps, preprocessor.IncludedInterface, outputFile);
            }
            else
            {
                Console.WriteLine($"Compilation failed with {ErrorCount} errors and {WarningCount} warnings");
            }

            TimeSpan duration = DateTime.Now - _compileStartTime;

            Console.WriteLine($"Total time: {duration.ToString(@"mm\:ss")}");

            return(successfulCompile);
        }
예제 #2
0
        private static List <DreamMapJson> ConvertMaps(List <string> mapPaths)
        {
            List <DreamMapJson> maps = new();

            foreach (string mapPath in mapPaths)
            {
                VerbosePrint($"Converting map {mapPath}");

                DMPreprocessor preprocessor = new DMPreprocessor(false, !Settings.SuppressUnimplementedWarnings);
                preprocessor.IncludeFile(Path.GetDirectoryName(mapPath), Path.GetFileName(mapPath));

                DMLexer      lexer  = new DMLexer(mapPath, preprocessor.GetResult());
                DMMParser    parser = new DMMParser(lexer);
                DreamMapJson map    = parser.ParseMap();

                if (parser.Errors.Count > 0)
                {
                    foreach (CompilerError error in parser.Errors)
                    {
                        Error(error);
                    }

                    continue;
                }

                if (parser.Warnings.Count > 0)
                {
                    foreach (CompilerWarning warning in parser.Warnings)
                    {
                        Warning(warning);
                    }
                }

                maps.Add(map);
            }

            return(maps);
        }