コード例 #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);
            }
        }
コード例 #2
0
 internal void GetTokensOfFile(TFile file)
 {
     InitializeLexerForNewFile();
     PreProcess();
     TypeSymbol temp = LexicalUnit();
     while (LexicalUnit() != TypeSymbol.U_EOF)
     {
         token.Add(temp);
         temp = LexicalUnit();
     }
 }
コード例 #3
0
        internal void PreProcess()
        {
            if (!ReadNewLine())
            {
                GetWordError(WordMessagesError.FileEmpty, currFile.Name);
            }

            if (!SkipSpacesAndComment())
            {
                GetWordError(WordMessagesError.LackFileCode, currFile.Name);
            }

            while (CC == '#')
            {
                if ((NextCCInLine) && (char.ToLower(NextCC) == 'i' || char.ToLower(NextCC) == 'd'))
                {
                    CI++;
                    UL = LexicalUnit();
                    if (UL == TypeSymbol.U_Include)
                    {
                        SkipSpaces();
                        if (!(CCInLine && CC == '\''))
                        {
                            GetMacroError(WordMessagesError.NotFound, "string");
                        }
                        UL = LexicalUnit();
                        if (UL != TypeSymbol.U_Cst_Str)
                        {
                            GetMacroError(WordMessagesError.NotFound, "string");
                        }

                        TFile.AddFile(G_curr_Str, gFile);
                    }
                    else if (UL == TypeSymbol.U_Define)
                    {
                        SkipSpaces();

                        if (!((CCInLine) && (char.IsLetter(CC) || CC == '_')))
                        {
                            GetMacroError(WordMessagesError.NotFound, "Identifier");
                        }

                        UL = LexicalUnit();

                        if (UL != TypeSymbol.U_UnKown && UL != TypeSymbol.U_VarProcedure)
                        {
                            GetMacroError(WordMessagesError.IdKnown);
                        }

                        if (UL == TypeSymbol.U_UnKown)
                        {
                            IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gdefine);
                        }
                        if (UL == TypeSymbol.U_VarProcedure)
                        {
                            IdentifierInstruction.AddIdentifier(id.ToUpper(), ref Locals.gdefine);
                        }

                        DefineInstruction defAux = gDefine;
                        SkipSpaces();

                        if (!(CCInLine && (CC == '.' || char.IsNumber(CC) || CC == '\'')))
                        {
                            GetMacroError(WordMessagesError.NotFound, "number or string");
                        }

                        UL = LexicalUnit();
                        if (UL == TypeSymbol.U_Cst_Int || UL == TypeSymbol.U_Cst_Real)
                        {
                            defAux.ValNB = G_curr_Num;
                        }
                        else if (UL == TypeSymbol.U_Cst_Str)
                        {
                            defAux.ValStr = G_curr_Str;
                        }
                        else
                        {
                            GetMacroError(WordMessagesError.NotValidChar, "number or string");
                        }

                        defAux.UL = UL;
                    }
                    else
                    {
                        GetMacroError(WordMessagesError.NotFound, "Include or Define");
                    }
                }
                else
                {
                    GetMacroError(WordMessagesError.NoSpace);
                }
            }

            //return UL;
        }