Exemple #1
0
        public void ExecuteMainMethodFromObjectFile(string path)
        {
            using (FileStream stream = new FileStream(path, FileMode.Open))
            {
                ProcedureInstruction methods;
                try
                {
                    methods = (ProcedureInstruction)formatter.Deserialize(stream);
                }
                catch (SerializationException)
                {
                    GetRuntimeError(RuntimeMessagesError.ObjFile);
                    return;     // this statment is unreachable. Just for the compile Error
                }

                ProcedureInstruction main = IdentifierInstruction.FindIdentifer("Main", methods);

                if (main != null)
                {
                    ExecuteListOfInstructions(main.Linst);
                }
                else
                {
                    GetRuntimeError(RuntimeMessagesError.NoMain);
                }

                EndOfExecute?.Invoke(this, new WriteEventArgs(true, "End of Executing... Press Enter to Exit", true));
            }
        }
Exemple #2
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);
            }
        }
        private object HandleCall()
        {
            UL = LexicalUnit();
            if ((UL != TypeSymbol.U_VarProcedure) && (UL != TypeSymbol.U_UnKown))
            {
                GetSyntaxError(WordMessagesError.NotFound, "Procedure Name");
            }
            if (UL == TypeSymbol.U_UnKown)
            {
                IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gproc);
                // by default
                gProc.IsDefined = false;
                gProc.IsFunc    = false;
                gProc.Linst     = null;
                gProc.PIN       = null;
                gProc.POut      = null;
                gProc.LVar      = null;
                G_curr_ID       = gProc;
            }
            if (((ProcedureInstruction)G_curr_ID).IsFunc)
            {
                GetSyntaxError(SyntaxMessagesError.CallFunction);
            }

            ProcedureInstruction procAux = (ProcedureInstruction)G_curr_ID;

            UL = LexicalUnit();
            CallInstruction callAux = ReadCall(procAux);

            if (UL != TypeSymbol.U_SemiColon)
            {
                GetSyntaxError(SyntaxMessagesError.SemiColon);
            }
            UL = LexicalUnit();
            return(callAux);
        }
        private TExpression ReadFact(ref TExpression last)
        {
            if (UL == TypeSymbol.U_Cst_Int || UL == TypeSymbol.U_Cst_Real || UL == TypeSymbol.U_Cst_Str ||
                UL == TypeSymbol.U_True || UL == TypeSymbol.U_False)
            {
                TExpression expNew = new TExpression();
                expNew.UL = UL;
                if (UL == TypeSymbol.U_Cst_Str)
                {
                    expNew.ValStr = G_curr_Str;
                }
                else if (UL == TypeSymbol.U_Cst_Int || UL == TypeSymbol.U_Cst_Real) // the if statment i added after notce the true false.
                {
                    expNew.ValNB = G_curr_Num;
                }

                // by default
                expNew.Next = null;
                expNew.Prev = null;

                last = expNew;
                UL   = LexicalUnit();
                return(expNew);
            }
            else if (UL == TypeSymbol.U_OpenParanthese)
            {
                UL = LexicalUnit();
                TExpression expNew = ReadCondition(ref last);
                if (UL != TypeSymbol.U_ClosedParanthese)
                {
                    GetSyntaxError(WordMessagesError.NotFound, ")");
                }
                UL = LexicalUnit();
                return(expNew);
            }
            else if (UL == TypeSymbol.U_Var || UL == TypeSymbol.U_VarDefine ||
                     UL == TypeSymbol.U_VarProcedure || UL == TypeSymbol.U_UnKown)
            {
                TExpression expNew = new TExpression();

                // By Default
                expNew.Next = null;
                expNew.Prev = null;

                TVar varAux = null;
                ProcedureInstruction procAux = null;
                TypeSymbol           ulAux;

                if (UL == TypeSymbol.U_VarDefine)   // if the variable define
                {
                    DefineInstruction defAux = (DefineInstruction)G_curr_ID;
                    expNew.UL     = defAux.UL;
                    expNew.ValNB  = defAux.ValNB;
                    expNew.ValStr = defAux.ValStr;
                    ulAux         = defAux.UL;
                    UL            = LexicalUnit();
                }
                else if (UL == TypeSymbol.U_UnKown) // if the variblae unknown procedure , unknown variable
                {
                    string buffer = G_curr_Str;
                    UL = LexicalUnit();

                    if (UL == TypeSymbol.U_OpenParanthese)  // if the variblae unknown procedure
                    {
                        IdentifierInstruction.AddIdentifier(buffer, ref Locals.gproc);
                        procAux      = gProc;
                        gProc.IsFunc = true;
                        //gProc.IsDefined = false;
                        //gProc.PIN = null;
                        //gProc.POut = null;
                        //gproc.LVar = null;
                        //gProc.Linst = null;
                        ulAux = TypeSymbol.U_VarProcedure;
                    }
                    else                    // if the variblae unknown procedure , unknownvariable
                    {
                        IdentifierInstruction.AddIdentifier(buffer, ref Locals.gvar);
                        varAux = gVar;
                        ulAux  = TypeSymbol.U_Var;
                    }
                }
                else    // if the variable known
                {
                    // this statment i am not sure about it
                    ulAux = UL;
                    if (ulAux == TypeSymbol.U_Var)      // if the vairable known var
                    {
                        varAux = (TVar)G_curr_ID;
                    }
                    else                                // if the variable known procedure
                    {
                        procAux = (ProcedureInstruction)G_curr_ID;
                    }

                    UL = LexicalUnit();
                }

                expNew.UL = ulAux;
                last      = expNew;
                if (ulAux == TypeSymbol.U_VarProcedure)
                {
                    expNew.ValCall = ReadCall(procAux);
                }
                else if (ulAux == TypeSymbol.U_Var)
                {
                    expNew.ValVar = varAux;
                    if (UL == TypeSymbol.U_OpenBracket)
                    {
                        UL           = LexicalUnit();
                        expNew.Index = ReadExpression();
                        if (UL != TypeSymbol.U_ClosedBracket)
                        {
                            GetSyntaxError(WordMessagesError.NotFound, "]");
                        }

                        UL = LexicalUnit();
                    }
                    else
                    {
                        expNew.Index = null;
                    }
                }

                return(expNew);
            }
            else if (new TypeSymbol[] { TypeSymbol.U_Sin, TypeSymbol.U_Cos, TypeSymbol.U_Tg,
                                        TypeSymbol.U_Ln, TypeSymbol.U_Log, TypeSymbol.U_Exp, TypeSymbol.U_Sqr, TypeSymbol.U_Sqrt,
                                        TypeSymbol.U_Length, TypeSymbol.U_IntToStr, TypeSymbol.U_StrToInt, TypeSymbol.U_IntToHex, }.Contains(UL))
            {
                TExpression expNew = new TExpression();
                expNew.UL = UL;
                UL        = LexicalUnit();
                if (UL != TypeSymbol.U_OpenParanthese)
                {
                    GetSyntaxError(WordMessagesError.NotFound, "(");
                }

                UL = LexicalUnit();
                TExpression exp0 = ReadExpression(ref last);
                last.Next   = expNew;
                expNew.Prev = last;
                last        = expNew;

                if (UL != TypeSymbol.U_ClosedParanthese)
                {
                    GetSyntaxError(WordMessagesError.NotFound, ")");
                }

                UL = LexicalUnit();
                return(exp0);
            }
            else if (UL == TypeSymbol.U_Pluse || UL == TypeSymbol.U_Minus || UL == TypeSymbol.U_Not || UL == TypeSymbol.U_Complement)
            {
                TExpression expNew = new TExpression();

                if (UL == TypeSymbol.U_Pluse)
                {
                    expNew.UL = TypeSymbol.U_UnaryPluse;
                }
                else if (UL == TypeSymbol.U_Minus)
                {
                    expNew.UL = TypeSymbol.U_UnaryMinuse;
                }
                else if (UL == TypeSymbol.U_Not)
                {
                    expNew.UL = TypeSymbol.U_Not;
                }
                else
                {
                    expNew.UL = TypeSymbol.U_Complement;
                }

                UL = LexicalUnit();
                TExpression exp0 = ReadFact(ref last);
                last.Next   = expNew;
                expNew.Prev = last;
                last        = expNew; // this statment i added to solve the problem that unary pluse and unary minus.

                return(exp0);
            }
            else
            {
                GetSyntaxError(SyntaxMessagesError.InvalidToken, UL.ToString());
            }

            // this statment will never execute
            // just for elminate compile time error.
            return(null);
        }
        private CallInstruction ReadCall(ProcedureInstruction procAux)
        {
            CallInstruction callAux = new CallInstruction {
                P = procAux
            };

            // Code before Modify

            //callAux.P = (TProcedure)lexer.CurrID;
            //if (LexicalUnit() != TypeSymbol.U_OpenParanthese)
            //{
            //    GetSyntaxError(WordMessagesError.NotFound, "(");
            //}

            // Code after Modify
            // callAux.P = procAux;
            if (UL != TypeSymbol.U_OpenParanthese)
            {
                GetSyntaxError(WordMessagesError.NotFound, "(");
            }

            UL = LexicalUnit();
            // By Default
            callAux.Pin  = null;
            callAux.Pout = null;

            if (UL == TypeSymbol.U_Input)
            {
                UL = LexicalUnit();
                TExpression last = null;
                while (true)
                {
                    TExpression last1 = null;
                    TExpression exp1  = ReadExpression(ref last1);
                    if (callAux.Pin == null)
                    {
                        callAux.Pin = exp1;
                    }
                    else
                    {
                        last.Next = exp1;
                        exp1.Prev = last;
                    }

                    last = last1;

                    if (UL == TypeSymbol.U_ClosedParanthese || UL == TypeSymbol.U_Output)
                    {
                        break;
                    }
                    if (UL != TypeSymbol.U_Comma)
                    {
                        GetSyntaxError(WordMessagesError.NotFound, "\",\"");
                    }

                    UL = LexicalUnit();
                }   // end while
            }

            if (UL == TypeSymbol.U_Output) // this section must be Repated again ( it is not finished )
            {
                UL = LexicalUnit();

                while (true)
                {
                    if (UL == TypeSymbol.U_UnKown)
                    {
                        IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gvar);
                        G_curr_ID = gVar;
                    }
                    else if (UL != TypeSymbol.U_Var)
                    {
                        GetSyntaxError(WordMessagesError.NotFound, "Variable");
                    }

                    TListVar newlvar = new TListVar();
                    newlvar.V    = (TVar)G_curr_ID;
                    newlvar.Next = callAux.Pout;
                    callAux.Pout = newlvar;

                    UL = LexicalUnit();
                    if (UL == TypeSymbol.U_ClosedParanthese)
                    {
                        break;
                    }
                    if (UL != TypeSymbol.U_Comma)
                    {
                        GetSyntaxError(WordMessagesError.NotFound, "\",\"");
                    }
                    UL = LexicalUnit();
                }
            }

            if (UL != TypeSymbol.U_ClosedParanthese)
            {
                GetSyntaxError(WordMessagesError.NotFound, ")");
            }

            UL = LexicalUnit();
            return(callAux);
        }