private string IncludeCode(string input, string path)
        {
            input = input.Replace("cdecl ", "\"" + path + "\" cdecl ");
            input = input.Replace("stdcall ", "\"" + path + "\" stdcall ");
            input = input.Replace("structure ", "\"" + path + "\" structure ");
            input = input.Replace("global ", "\"" + path + "\" global ");
            //Parse includes
            int pos = input.IndexOf("#include");

            while (pos != -1)
            {
                int includeStart = pos + "#include".Length;
                while (input[includeStart] == ' ')
                {
                    includeStart++;
                }
                if (input[includeStart] != '\"')
                {
                    throw new SALO_Exception("Include preprocessor directive is empty at " + pos);
                }
                int includeEnd = includeStart + 1;
                while (input[includeEnd] != '\"' || input[includeEnd - 1] == '\\')
                {
                    includeEnd++;
                }
                //We found include file name boundaries
                string fileName = input.Substring(includeStart + 1, includeEnd - includeStart - 1);

                if (!IsFullPath(fileName))
                {
                    string dir           = Path.GetDirectoryName(path);
                    string absolute_path = Path.Combine(dir, fileName);
                    fileName = Path.GetFullPath((new Uri(absolute_path)).LocalPath);
                }
                string fileData = File.ReadAllText(fileName, utf8);
                Builder_Translation builder_Translation = new Builder_Translation(fileData, locales);
                fileData = builder_Translation.Translated;
                fileData = RemoveComments(fileData);
                fileData = ReplaceBackslash(fileData);
                fileData = IncludeCode(fileData, fileName);

                input = input.Remove(pos, includeEnd - pos + 1);
                input = input.Insert(pos, fileData);

                pos = input.IndexOf("#include");
            }
            return(input);
        }
예제 #2
0
        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;
        }