⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cs

📁 charp compiler
💻 CS
📖 第 1 页 / 共 3 页
字号:
            return 0;
        }
        
//-----------------------------------------------------------------------------
// Main wrapper. 
// This doesn't worry about startup/shutdown.
// It wraps the worker, protects for exceptions, and handles errors at the
// top level.
//-----------------------------------------------------------------------------       
        static int MainProxy(string[] arstArgs)
        {            
            // Signify error. Clear error if we make it all the way through
            int iErrorValue = 1;
            
            
            
            // Process the command line. This will call all Option Handlers and
            // produce an array of files to compile. It may also produce errors
            string[] arstSourceFiles = null;
            bool f = m_OptionMananager.ProcessCommandLine(arstArgs, out arstSourceFiles);
            if (!f)
                return 0;
                    
            try
            {                
                // If the options are bad, then print errors & quit now.
                if (StdErrorLog.HasErrors())
                    return 1;
            
                        
                // This may throw an exception. If it does, it's an internal
                // error in Blue.
                iErrorValue = MainWorker(arstSourceFiles);
            }
            catch(Exception e)
            {
                // Catch the exception before we print the error log so
                // that the log includes it.
                PrintError_InternalError(e);
                iErrorValue = 8;
            }
            finally
            {
                m_ErrorLog.PrintFinal();                    
            }
                        
            return iErrorValue;
        } // end Driver.Main


//-----------------------------------------------------------------------------
// Parse multiple source files into a single program.
// The Parser only understands Lexers (as opposed to filenames), so we can't
// give the parser the string array of source files. But we don't want to 
// create the lexers all at once and pass them to the parser either.
// Return null on errors.
//-----------------------------------------------------------------------------
        private static AST.ProgramDecl ParseAllFiles(
            string [] arstSourceFiles
        )
        {
            //AST.ProgramDecl root = null;
            AST.NamespaceDecl [] arFiles = new AST.NamespaceDecl[arstSourceFiles.Length];
            
            bool fHasErrors = false;

            int iFile = 0;
            foreach(string stSourceFile in arstSourceFiles)
            {                        
                string stShort    = IO.Path.GetFileNameWithoutExtension(stSourceFile);

                // Get a lexer for this file                
                System.IO.StreamReader reader = null;
                
                try{
                    reader = new System.IO.StreamReader(stSourceFile);
                } catch(System.Exception e)
                {
                    reader = null;
                    PrintError_CantFindSourceFile(stSourceFile, e);
                    fHasErrors = true;
                }
                
                if (reader == null)
                {                    
                    arFiles[iFile] = null;                    
                } else {
                    ILexer lex = new ManualParser.Lexer(stSourceFile, reader, m_defines);

                    // Parse this file
                    IParser p = new ManualParser.Parser(lex);
                    Debug.Assert(p != null);
                                          
                    // Return null on errors. Continue to try and parse the other source
                    // files so that we can catch as many errors as possible, but 
                    // we won't resolve / codegen anything if it has parse errors.
                    Log.WriteLine(Log.LF.Parser, "Parsing source file:" + stSourceFile);
                    AST.NamespaceDecl nodeFile = p.ParseSourceFile();
                        
                    if (nodeFile == null) 
                    {
                        fHasErrors = true;
                    }

                    // Spit this out even if we have errors; it may be partially helpful.                        
                    if (s_fGenerateXML)
                    {
                        System.Xml.XmlWriter oParse = new System.Xml.XmlTextWriter(stShort + "_parse.xml", null);
                        AST.Node.DumpTree(nodeFile, oParse);
                    }
                    
                    arFiles[iFile] = nodeFile;
                }

            // Add to program                
                iFile++;
            }

            if (fHasErrors)
                return null;
                
            return new AST.ProgramDecl(arFiles);
        }

#region Option Handlers
//-----------------------------------------------------------------------------
// Handle -X option: Show XML
//-----------------------------------------------------------------------------
        private static void 
        OptionX(
            string stOption)
        {
            s_fGenerateXML = true;
            
            if (stOption != "")
                throw new Blue.Utilities.OptionErrorException("No parameter expected");
        }

        private static bool s_fGenerateXML = false;
        
//-----------------------------------------------------------------------------
// Test bed for the lexer
//-----------------------------------------------------------------------------        
        private static int TestLexer(ILexer lex)
        {
            ManualParser.Token t;
            
            Console.WriteLine("Lexer test:");
            do
            {
                t = lex.GetNextToken();
                Console.WriteLine(t.ToString());                
            } while (t.TokenType != ManualParser.Token.Type.cEOF);
            
            return 0;
        }

//-----------------------------------------------------------------------------
// Define symbols for preprocessor
// This may occur multiple times.
//-----------------------------------------------------------------------------
        private static void 
        OptionDefineSymbol(
            string stSymbols
        )
        {
            // stSymbols is a comma separated list of strings
            string [] stArgs = stSymbols.Trim(' ').Split(' ');
            
            if (m_defines == null)
            {
                m_defines = stArgs;
                return;
            } else {
                string [] stTemp = new string[stArgs.Length + m_defines.Length];                
                m_defines.CopyTo(stTemp, 0);
                stArgs.CopyTo(stTemp, m_defines.Length);
                
                m_defines = stTemp;
            }
        }            

//-----------------------------------------------------------------------------
// Add an assembly reference
//-----------------------------------------------------------------------------
        private static void 
        OptionAddReference(
            string stOption
        )
        {
            m_stAsmRefs.Add(stOption);
        }
        
        private static System.Collections.Specialized.StringCollection m_stAsmRefs = 
            new System.Collections.Specialized.StringCollection();
         
        // Return an array of assembly references. 
        // Goes through references set by cmd line and loads them to get real Assemblies
        // Throw an error if there's a problem   
        //
        // We need to be able to handle:
        // /r:System.dll
        private static System.Reflection.Assembly [] LoadImportedAssemblies()
        {
            if (m_AsmRefs != null && m_AsmRefs.Length == m_stAsmRefs.Count)
                return m_AsmRefs;
                
            string stRuntimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();                
                
            m_AsmRefs = new System.Reflection.Assembly[m_stAsmRefs.Count];
            for(int i = 0; i < m_AsmRefs.Length; i++)
            {
                string stFilename = m_stAsmRefs[i];
                
                // We have 3 load functions:
                // LoadFrom(string stFilename) - load the file
                // Load(string stAssemblyName) - load the assembly given the _fullname_,checks the gac
                // LoadWithPartialName(string stName) - checks gac. Load's best match

                System.Reflection.Assembly a = null;
                
                // Check a local dir. Ex: /r:MyThing.dll
                try{                    
                    a = System.Reflection.Assembly.LoadFrom(stFilename);                    
                } 
                catch(System.IO.FileNotFoundException)
                {
                    a = null;
                }
                
                // Checks partial name (includes gac), ex: /r:System
                if (a == null)
                {   
                    try
                    {
                        a = System.Reflection.Assembly.LoadWithPartialName(stFilename);                        
                    }
                    catch(System.IO.FileNotFoundException)
                    {
                        a = null;
                    }
                }
                
                // Check runtime dir, ex: /r:System.dll
                if (a == null)
                {
                    try {
                        a = System.Reflection.Assembly.LoadFrom(stRuntimeDir + "\\" + stFilename);
                    }
                    catch(System.IO.FileNotFoundException)
                    {                
                        a = null;                
                    }
                }

                // @todo - include fusion log?
                if (a == null)
                {
                    PrintError_CantFindAssembly(stFilename);
                }
                                
                m_AsmRefs[i] = a;
            }
            
            return m_AsmRefs;
        }
        
        private static System.Reflection.Assembly [] m_AsmRefs;

//-----------------------------------------------------------------------------
// Option to allow us to quit after certain stages
// (used for debugging purposes)
//-----------------------------------------------------------------------------
        private static void 
            OptionDbgQuit(
            string stOption)
        {
            s_fIsQuitAfterStage = true;
            
            if (stOption == "Lexer")
            {   
                s_eQuitAfterStage = EStage.cLexer;
            } else if (stOption == "Parser")
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -