public static Builder_Locales FromFile(string path) { if (!File.Exists(path)) { throw new SALO_Exception("LocaleStrings file not found", new FileNotFoundException()); } string res = File.ReadAllText(path); Builder_Locales result = null; try { result = JsonConvert.DeserializeObject <Builder_Locales>(res); } catch (Exception e) { throw new SALO_Exception("Error parsing locale settings", e); } return(result); }
public Builder_Preprocessor(string mainFilePath, string input, Builder_Locales locales) { utf8 = Encoding.GetEncoding("UTF-8"); this.locales = locales; if (!IsFullPath(mainFilePath)) { string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string absolute_path = Path.Combine(dir, mainFilePath); mainFilePath = Path.GetFullPath((new Uri(absolute_path)).LocalPath); } this.mainFilePath = mainFilePath; if (!File.Exists(mainFilePath)) { throw new SALO_Exception("Main code file was not found", new FileNotFoundException(mainFilePath)); } //Get input text InputText = input; string outText = input; //Remove comments outText = RemoveComments(outText); //Replace backslashed outText = ReplaceBackslash(outText); //Include files outText = IncludeCode(outText, mainFilePath); //Perform macros bool processed = true; while (processed) { outText = ProcessFile(outText, out processed); } OutputText = outText; }
public Builder_Translation(string input, Builder_Locales locales) { localeStrings = locales; original = input; translated = input; //TODO - cut out comments foreach (NamesTranslated nt in locales.names_translated) { if (!string.IsNullOrWhiteSpace(nt.locale) && !string.IsNullOrWhiteSpace(nt.translated)) { //int ind = translated.IndexOf(nt.locale); translated = translated.Replace(nt.locale, nt.translated); } } //Parse letter by letter, skip strings int quoteIndex = 0; string output = ""; while (quoteIndex < translated.Length) { bool breakLoop = false; if (translated[quoteIndex] == '\"') { if (quoteIndex == 0 || translated[quoteIndex - 1] != '\\') { //We have a quote start, find end int quoteStart = quoteIndex; ++quoteIndex; while (!(translated[quoteIndex] == '\"' && translated[quoteIndex - 1] != '\\')) { quoteIndex++; if (quoteIndex >= translated.Length) { //We reached the end output += translated.Substring(quoteStart); breakLoop = true; break; } } if (breakLoop) { break; } //quoteIndex is pointing at the closing quote output += translated.Substring(quoteStart, quoteIndex - quoteStart + 1); } else { output += '\"'; } } else { string s = ""; s += translated[quoteIndex]; output += Translitor.Translit(s); } ++quoteIndex; } translated = output; }
public Builder_Global(string input, string inputPath, string settingsPath, Language lang, bool build_eng = true, bool build_ast = true, bool build_lang = true) { try { if (input == null) { if (inputPath == null) { throw new NullReferenceException("Both provided input text and file are empty"); } } Encoding utf8 = Encoding.GetEncoding("UTF-8"); //TODO - check if file exists, if arguments are correct, etc. input = File.ReadAllText(inputPath, utf8); } catch (Exception e) { throw new SALO_Exception("Unhandled exception caught while loading main code file", e); } try { LocaleStrings = Builder_Locales.FromFile(settingsPath); } catch (SALO_Exception e) { throw new SALO_Exception("Failed to create LocaleStrings builder", e); } catch (Exception e) { throw new SALO_Exception("Unhandled exception caught while creating LocaleStrings builder", e); } if (build_eng) { try { Translator = new Builder_Translation(input, LocaleStrings); #if DEBUG Console.WriteLine("Translated:"); Console.WriteLine(Translator.Translated); #endif } catch (SALO_Exception e) { throw new SALO_Exception("Failed to create Translation builder", e); } catch (Exception e) { throw new SALO_Exception("Unhandled exception caught while creating Translation builder", e); } try { Preprocessor = new Builder_Preprocessor(inputPath, Translator.Translated, LocaleStrings); #if DEBUG Console.WriteLine("Preprocessed:"); Console.WriteLine(Preprocessor.OutputText); #endif } catch (SALO_Exception e) { throw new SALO_Exception("Failed to create Preprocessor builder", e); } catch (Exception e) { throw new SALO_Exception("Unhandled exception caught while creating Preprocessor builder", e); } if (build_ast) { try { AST = new Builder_AST(Preprocessor.OutputText); #if DEBUG string ast = ""; AST.Print(ref ast); Console.WriteLine("AST:"); Console.WriteLine(ast); #endif } catch (SALO_Exception e) { //ExceptionHandler.ShowException(e, Translator.Translated); throw new SALO_Exception("Failed to create an AST", e); } catch (Exception e) { throw new SALO_Exception("Unhandled exception caught while creating an AST", e); } if (build_lang) { try { Libraries = new Builder_Libraries(settingsPath); Compiler = new Builder_Compile(lang, AST, Libraries); #if DEBUG Console.WriteLine("Assembler:"); Console.WriteLine(Compiler.Result); #endif } catch (SALO_Exception e) { throw new SALO_Exception("Failed to create an AST", e); } catch (Exception e) { throw new SALO_Exception("Unhandled exception caught while compiling into " + lang.ToString(), e); } } } } Initialized = true; }