예제 #1
0
        /// <summary>
        /// Compile the primary file that has main function and the related included file with it.
        /// </summary>
        /// <param name="fileName">Represent the filename</param>
        public void CompileMainProgram(string fileName)
        {
            Warning.Clear();

            locals.initializeForRecompile();
            gFile = new TFile { Name = fileName };
            currentFile = gFile;
            bool mainFile = true;

            while (currentFile != null)
            {
                locals.initializeForNewFile();
                try
                {
                    locals.CF = File.ReadAllLines(currentFile.Name);
                }
                catch (FileNotFoundException)   // if the file that included is not found.
                {
                    MakeSyntaxError(SyntaxMessagesError.FileNotFound);
                }
                catch (DirectoryNotFoundException)
                {
                    MakeSyntaxError(SyntaxMessagesError.FileNotFound);
                }

                analyst.CompileCurrentFile();
                if (mainFile)       // if there is NO main is not in main File.
                {
                    if (IdentifierInstruction.FindIdentifer("Main", gProc) == null)
                    {
                        MakeSyntaxError(string.Format(SyntaxMessagesError.NoMainMethod, currentFile.Name));
                    }
                }

                mainFile = false;
                currentFile = currentFile.Next;
            }

            // Check if we call function or procedure that not Defined
            ProcedureInstruction tempP = gProc;
            while (tempP != null)
            {
                if (!tempP.IsDefined)
                {
                    throw new SyntaxErrorException(string.Format(SyntaxMessagesError.MethodNotDefined, tempP.Name), 0, 0, "");
                    //MakeSyntaxError(string.Format(SyntaxMessagesError.MethodNotDefined, tempP.Name));
                }
                tempP = (ProcedureInstruction)tempP.Next;
            }

            string[] dirs = gFile.Name.Split('\\');
            dir = dirs.Take(dirs.Length - 1).Select(str => str + "\\").Aggregate((one, two) => one + two) + dirs.Last().Split('.')[0] + ".obj";
            using (FileStream writer = new FileStream(dir, FileMode.Create))
            {
                formatter.Serialize(writer, gProc);
            }
        }