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)); } }
/// <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 HandleFor() { ForInstruction forAux = new ForInstruction(); UL = LexicalUnit(); 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"); } forAux.V = (TVar)G_curr_ID; UL = LexicalUnit(); if (UL != TypeSymbol.U_Assignment) { GetSyntaxError(WordMessagesError.NotFound, ":="); } UL = LexicalUnit(); forAux.ExpBegin = ReadExpression(); if ((UL != TypeSymbol.U_To) && (UL != TypeSymbol.U_DownTo)) { GetSyntaxError(WordMessagesError.NotFound, "\"To\" or \"DownTo\" Keyword"); } forAux.IsDown = (UL == TypeSymbol.U_DownTo); UL = LexicalUnit(); forAux.ExpEnd = ReadExpression(); if (UL == TypeSymbol.U_Step) { UL = LexicalUnit(); forAux.ExpStep = ReadExpression(); } else { forAux.ExpStep = new TExpression { ValNB = 1, UL = TypeSymbol.U_Cst_Int }; } if (UL != TypeSymbol.U_Do) { GetSyntaxError(WordMessagesError.NotFound, "\"Do\" Keyword"); } forAux.Ins = ReadOneOrListOfInstruction(); return(forAux); }
private object HandleRead() { ReadInstruction readAux = new ReadInstruction(); if (LexicalUnit() != TypeSymbol.U_OpenParanthese) { GetSyntaxError(WordMessagesError.NotFound, "("); } UL = LexicalUnit(); if (UL == TypeSymbol.U_UnKown) { IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gvar); readAux.V = gVar; } else if (UL == TypeSymbol.U_Var) { readAux.V = (TVar)G_curr_ID; } else { GetSyntaxError(WordMessagesError.NotFound, "Variable"); } UL = LexicalUnit(); if (UL == TypeSymbol.U_OpenBracket) { UL = LexicalUnit(); readAux.index = ReadExpression(); if (UL != TypeSymbol.U_ClosedBracket) { GetSyntaxError(WordMessagesError.NotFound, "]"); } UL = LexicalUnit(); } if (UL != TypeSymbol.U_ClosedParanthese) { GetSyntaxError(WordMessagesError.NotFound, ")"); } UL = LexicalUnit(); if (UL != TypeSymbol.U_SemiColon) { GetSyntaxError(SyntaxMessagesError.SemiColon); } UL = LexicalUnit(); return(readAux); }
private object HandleAssign() { AssignInstruction assignAux = new AssignInstruction(); if (UL == TypeSymbol.U_UnKown) { IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gvar); G_curr_ID = gVar; } assignAux.Var = (TVar)G_curr_ID; UL = LexicalUnit(); assignAux.index = null; if (UL == TypeSymbol.U_OpenBracket) { UL = LexicalUnit(); assignAux.index = ReadExpression(); if (UL != TypeSymbol.U_ClosedBracket) { GetSyntaxError(WordMessagesError.NotFound, "]"); } UL = LexicalUnit(); } // validation. if (!(UL == TypeSymbol.U_Assignment || UL == TypeSymbol.U_PluseAssigment || UL == TypeSymbol.U_MinusAssigment || UL == TypeSymbol.U_MultiplyAssigment || UL == TypeSymbol.U_PowAssigment || UL == TypeSymbol.U_DivisionAssigment || UL == TypeSymbol.U_ModAssigment || UL == TypeSymbol.U_PlusePluse || UL == TypeSymbol.U_MinusMinus)) { GetSyntaxError(WordMessagesError.NotFound, ":= , Compound Assignmet , ++ or -- "); } assignAux.UL = UL; UL = LexicalUnit(); if (assignAux.UL != TypeSymbol.U_PlusePluse && assignAux.UL != TypeSymbol.U_MinusMinus) { assignAux.Exp = ReadExpression(); } if (UL != TypeSymbol.U_SemiColon) { GetSyntaxError(SyntaxMessagesError.SemiColon); } UL = LexicalUnit(); return(assignAux); }
private TypeSymbol HandleIdentifier() { id = CC.ToString(); CI++; while (CCInLine && (char.IsNumber(CC) || char.IsLetter(CC) || CC == '_')) { id += CC; CI++; } TSymbol symAux = TSymbol.FindSymbol(id, AubCompiler.Gsymbol); if (symAux != null) { return symAux.UL; } string idUpperCase = id.ToUpper(); G_curr_ID = IdentifierInstruction.FindIdentifer(idUpperCase, gVar); if (G_curr_ID != null) { return TypeSymbol.U_Var; } G_curr_ID = IdentifierInstruction.FindIdentifer(idUpperCase, gDefine); if (G_curr_ID != null) { return TypeSymbol.U_VarDefine; } G_curr_ID = IdentifierInstruction.FindIdentifer(idUpperCase, gProc); if (G_curr_ID != null) { return TypeSymbol.U_VarProcedure; } G_curr_Str = id.ToUpper(); return TypeSymbol.U_UnKown; }
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); }
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; }
private void ReadProcedures() { UL = LexicalUnit(); while (UL == TypeSymbol.U_Function || UL == TypeSymbol.U_Procedure) { TypeSymbol auxUl = UL; UL = LexicalUnit(); if (UL == TypeSymbol.U_UnKown) { IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gproc); currProc = gProc; currProc.IsDefined = true; } else if (UL == TypeSymbol.U_VarProcedure) { currProc = (ProcedureInstruction)G_curr_ID; if (currProc.IsDefined) { GetSyntaxError(SyntaxMessagesError.DuplicateDefination, currProc.Name); } currProc.IsDefined = true; // this want to check. if (currProc.IsFunc != (auxUl == TypeSymbol.U_Function)) { GetSyntaxError(SyntaxMessagesError.NotAsCalled, currProc.Name); } } else { GetSyntaxError(WordMessagesError.NotFound, "Name of method"); } currProc.IsFunc = (auxUl == TypeSymbol.U_Function); UL = LexicalUnit(); if (UL != TypeSymbol.U_OpenParanthese) { GetSyntaxError(WordMessagesError.NotFound, "("); } UL = LexicalUnit(); // we should be sure that Gvar is null; if (UL == TypeSymbol.U_Input) { UL = LexicalUnit(); while (true) { if (UL != TypeSymbol.U_UnKown) { GetSyntaxError(WordMessagesError.NotFound, "Undifined variable"); } IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gvar); UL = LexicalUnit(); if (UL == TypeSymbol.U_Comma) { UL = LexicalUnit(); } else { break; } } currProc.PIN = gVar; } if (UL == TypeSymbol.U_Output) { UL = LexicalUnit(); while (true) { if (UL != TypeSymbol.U_UnKown) { GetSyntaxError(WordMessagesError.NotFound, "Undifined variable"); } IdentifierInstruction.AddIdentifier(G_curr_Str, ref Locals.gvar); UL = LexicalUnit(); if (UL == TypeSymbol.U_Comma) { UL = LexicalUnit(); } else { break; } } currProc.POut = gVar; } if (UL != TypeSymbol.U_ClosedParanthese) { GetSyntaxError(WordMessagesError.NotFound, ")"); } UL = LexicalUnit(); if (UL != TypeSymbol.U_SemiColon) { GetSyntaxError(WordMessagesError.NotFound, ";"); } UL = LexicalUnit(); currProc.Linst = ReadListOfInstruction(); currProc.LVar = gVar; gVar = null; } // end while if (UL != TypeSymbol.U_EOF) { GetSyntaxError(SyntaxMessagesError.InsideMethod); } }
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); }