// Copyright (c) Ivan Bondarev, Stanislav Mihalkovich (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using PascalABCCompiler.Errors; using System.IO; using System.Linq; using System.Collections.Generic; namespace PascalABCCompiler { /*class mc { public static int meth() { return 3; } }*/ class ConsoleCompiler { private static string StringsPrefix = "PABCNETC_"; public static PascalABCCompiler.Compiler Compiler=null; public static string FileName = ""; public static CompilerOptions.OutputType outputType; public static DateTime StartTime; private static string StringResourcesGet(string Key) { return StringResources.Get(StringsPrefix + Key); } public static void ShowConnectedParsers() { if (Compiler.SupportedSourceFiles.Length > 0) { Console.Write(StringResourcesGet("CONNECTED_PARSERS")); foreach (PascalABCCompiler.SupportedSourceFile ssf in Compiler.SupportedSourceFiles) Console.Write(ssf+"; "); Console.WriteLine(); } } public static bool CheckAndSplitDirective(string directive, out string name, out string value) { System.Diagnostics.Debug.Assert(directive[0] == '/'); directive = directive.Remove(0, 1); name = null; value = null; var ss = directive.Split(':'); if (ss.Length>2) { Console.WriteLine("Directive can contain only one ':' sign"); return false; } name = ss[0].ToLower(); if (ss.Length > 1) value = ss[1].ToLower(); return true; } public static bool ApplyDirective(string name, string value, CompilerOptions co) { switch (name) { case "debug": switch (value) { case "0": co.Debug = false; co.Optimise = true; return true; case "1": co.Debug = true; return true; default: Console.WriteLine("Bad value in 'Debug' directive '{0}'. Acceptable values are 0 or 1", value); return false; } default: Console.WriteLine("No such directive name: '{0}'", name); return false; } } public static bool CheckAndApplyDirective(string directive, CompilerOptions co) { string name, value; var b = CheckAndSplitDirective(directive,out name, out value); if (!b) return false; b = ApplyDirective(name, value, co); return b; } public static void OutputHelp() { Console.WriteLine("Command line: "); Console.WriteLine("pabcnetcclear /directive1:value1 /directive2:value2 ... [inputfile]\n"); Console.WriteLine("Available directives:\n /Help /H /?\n /Debug:0(1)\n"); Console.WriteLine("/Debug:0 generates code with all .NET optimizations!"); } public static int Main(string[] args) { // SSM 18.03.19 Делаю параметры командной строки. Формат: pabcnetc.exe /Dir1=value1 /Dir2=value2 ... fname dir // dir - это пережиток старого - так можно было задавать каталог раньше. Пока - увы - пусть останется // Пока сделаю только директивы /Help /H /? и /Debug=0(1) // Имя директивы - это одно слово. Равенства может не быть - тогда value директивы равно null // Вычленяем первое равенство и делим директиву: до него - name, после него - value. Если name или value - пустые строки, то ошибка DateTime ldt = DateTime.Now; PascalABCCompiler.StringResourcesLanguage.LoadDefaultConfig(); Compiler = new PascalABCCompiler.Compiler(null, null); StringResourcesLanguage.CurrentLanguageName = StringResourcesLanguage.AccessibleLanguages[0]; //Console.WriteLine("OK {0}ms", (DateTime.Now - ldt).TotalMilliseconds); ldt = DateTime.Now; //ShowConnectedParsers(); var n = args.Length; if (n == 0) { //Console.WriteLine(StringResourcesGet("COMMANDLINEISABSENT")); OutputHelp(); return 2; } var n1 = args.TakeWhile(a => a[0] == '/').Count(); // количество директив if (n1