////************************************************************************ ////*** FUNCTION SearchSymbol ////*** ******************************************************************** ////*** DESCRIPTION : Takes a file path to parse and validate symbol ////*** labels which are then search for in the BST and ////*** the output is dumped to the console ////*** INPUT ARGS : string filePath ////*** OUTPUT ARGS : N/A ////*** IN/OUT ARGS : N/A ////*** RETURN : N/A ////************************************************************************ //public void SearchSymbols(string filePath) //{ // FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan); // StreamReader streamReader = new StreamReader(fileStream); // while (!streamReader.EndOfStream) // { // //get the line and trim whitespace // string currentLine = streamReader.ReadLine().CompactAndTrimWhitespaces(); // bool discardLine = false; // if (currentLine == "") // { // Chronicler.LogError("blank line, skipping: \"" + currentLine + "\"" + "\n", "Adding Symbol"); // discardLine = true; // } // if (discardLine !=true && ValidateLabel(currentLine, currentLine, "Seeking Symbol")) // { // StringBuilder stringBuilder = new StringBuilder(); // for (int i = 0; i < currentLine.Length && i < 6; i++) // stringBuilder.Append(currentLine[i]); // if (SearchSymbol(stringBuilder.ToString(), out Globals.Symbol? temp)) // { // Chronicler.Write("Found symbol: "); // temp.Value.Print(Chronicler.OutputOptions.IGNORE); // } // else // { // Chronicler.WriteLine("Symbol(" + currentLine + ") not found"); // } // } // } // streamReader.Dispose(); // fileStream.Dispose(); //} //************************************************************************ //*** FUNCTION Print //*** ******************************************************************** //*** DESCRIPTION : prints the current contents of the symbol table //*** INPUT ARGS : N/A //*** OUTPUT ARGS : N/A //*** IN/OUT ARGS : N/A //*** RETURN : N/A //************************************************************************ public void Print(Chronicler.OutputOptions outputOptions = Chronicler.OutputOptions.IGNORE) { Chronicler.WriteLine("Symbol\tRFlag\tValue \tMFlag \tIFlag", outputOptions); Chronicler.WriteLine("=====================================", outputOptions); foreach (KeyValuePair <string, Globals.Symbol> keyValuePair in SymbolTableBST) { keyValuePair.Value.Print(outputOptions); } }
//************************************************************************ //*** FUNCTION Print //*** ******************************************************************** //*** DESCRIPTION : prints the current contents of the symbol //*** INPUT ARGS : N/A //*** OUTPUT ARGS : N/A //*** IN/OUT ARGS : N/A //*** RETURN : N/A //************************************************************************ public void Print(Chronicler.OutputOptions outputOptions, StreamWriter streamWriter = null) { if (streamWriter == null) { Chronicler.WriteLine(label + "\t" + RFlag + "\t" + value.ToString("X6") + "\t" + MFlag + "\t" + IFlag, outputOptions); } else { streamWriter.WriteLine(label + "\t" + RFlag + "\t" + value.ToString("X6") + "\t" + MFlag + "\t" + IFlag, outputOptions); } }
static void DumpIntermidiateFile(string filePath, List <ExpresionLine> expresionLines, Globals.DataStructures dataStructures) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath + ".tmp")) { Chronicler.HoldOutput(); int curLinNum = 0; foreach (ExpresionLine expLine in expresionLines) { if (expLine != null && expLine.OriginalLine != null && expLine.OriginalLine != "") { curLinNum = expLine.lineNumber; file.Write(curLinNum.ToString() + "\t"); if (expLine.CommentLine != true) { file.Write(expLine.locationCounter.ToString("X6") + "\t"); } file.Write(expLine.OriginalLine); file.Write("\n"); Chronicler.Write(curLinNum.ToString() + "\t"); if (expLine.CommentLine != true) { Chronicler.Write(expLine.locationCounter.ToString("X6") + "\t"); } Chronicler.WriteLine(expLine.OriginalLine); } } foreach (LiteralTable.LiteralValue lv in dataStructures.literalTable.literalTable) { curLinNum++; file.Write(curLinNum.ToString() + "\t"); file.Write(lv.address.ToString("X6") + "\t"); file.Write("*" + "\t"); file.Write(lv.label); file.Write("\n"); Chronicler.Write(curLinNum.ToString() + "\t"); Chronicler.Write(lv.address.ToString("X6") + "\t"); Chronicler.Write("*" + "\t"); Chronicler.WriteLine(lv.label); } } Chronicler.HoldOutput(); dataStructures.symbolTable.Print(); Chronicler.HoldOutput(); dataStructures.literalTable.PrintTable(); }
//************************************************************************* //*** FUNCTION Main //*** ********************************************************************* //*** DESCRIPTION : This is the main program driver //*** INPUT ARGS : string[] args //*** OUTPUT ARGS : N/A //*** IN/OUT ARGS : N/A //*** RETURN : N/A //************************************************************************* static void Main(string[] args) { Chronicler.WriteLine("Loading symbols..."); Globals.DataStructures dataStructures = new Globals.DataStructures(); string filePath; if (args.Length == 0) { Chronicler.WriteLine("Please enter the search file name: "); filePath = Console.ReadLine(); } else { filePath = args[0]; } Chronicler.HoldOutput(); Chronicler.NewLine(); Chronicler.NewLine(); PassOne.Execute(dataStructures, out System.Collections.Generic.List <PassOne.ExpresionLine> expresionLines, "../../../" + filePath); //symbolTable.SearchSymbols("../../../" + filePath); Chronicler.HoldOutput(); PassTwo.Execute(dataStructures, expresionLines, "../../../" + filePath); }
//************************************************************************ //*** FUNCTION PrintTable //*** ******************************************************************** //*** DESCRIPTION : prints the current contents of the literal table //*** INPUT ARGS : N/A //*** OUTPUT ARGS : N/A //*** IN/OUT ARGS : N/A //*** RETURN : N/A //************************************************************************ public void PrintTable(Chronicler.OutputOptions outputOptions = Chronicler.OutputOptions.IGNORE) { Chronicler.WriteLine("NAME\t\tVALUE\t\tLENGTH\tADDRESS", outputOptions); Chronicler.WriteLine("===============================================", outputOptions); StringBuilder sb = new StringBuilder(""); foreach (LiteralValue lv in literalTable) { sb.Clear(); for (int x = 0; x < (16 - lv.label.Length); x++) { sb.Append(" "); } Chronicler.Write(lv.label + sb.ToString(), outputOptions); sb.Clear(); for (int x = 0; x < (16 - lv.hexValue.Length); x++) { sb.Append(" "); } Chronicler.Write(lv.hexValue + sb.ToString(), outputOptions); Chronicler.Write(lv.Length.ToString(), outputOptions); Chronicler.WriteLine("\t" + lv.address.ToString("X6"), outputOptions); } }
public static void Execute(Globals.DataStructures dataStructures, List <PassOne.ExpresionLine> expresionLines, string filePath) { Chronicler.Write("\nPassTwo\n"); foreach (PassOne.ExpresionLine expresionLine in expresionLines) { //evaluate expresion if (expresionLine.DeferExpresionResolutiontoPass2 && (expresionLine.instructionFormat == 3 || expresionLine.instructionFormat == 4)) { if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, expresionLine.operandFieldAndComment, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } } //convert expresion format if (expresionLine.Opcode == "" && expresionLine.IsEQU != true && (expresionLine.instructionFormat == 3 || expresionLine.instructionFormat == 4))//don't redo work { switch (expresionLine.expresionData.ExpresionType) { case Globals.ExpresionData.Contents.SYMBOL: int?value = null; if (expresionLine.expresionData.second.HasValue) { if (expresionLine.expresionData.operatorValue != null && expresionLine.expresionData.operatorValue == Globals.ExpresionData.Arithmetic.SUBTRACT) { value = (expresionLine.expresionData.first.Value.value) - expresionLine.expresionData.second.Value.value; } if (expresionLine.expresionData.operatorValue != null && expresionLine.expresionData.operatorValue == Globals.ExpresionData.Arithmetic.ADD) { value = (expresionLine.expresionData.first.Value.value) + expresionLine.expresionData.second.Value.value; } } if (value.HasValue && expresionLine.instructionFormat == 4) { expresionLine.Opcode = value.Value.ToString("X5"); } else if (value.HasValue && expresionLine.instructionFormat == 3) { expresionLine.Opcode = value.Value.ToString("X3"); } break; case Globals.ExpresionData.Contents.ERROR: Chronicler.WriteLine("This is wrong for some reason, you should look at it:\n" + expresionLine.OriginalLine, Chronicler.OutputOptions.ERR); break; case Globals.ExpresionData.Contents.LITERAL: if (expresionLine.expresionData.literal.isOldStyleLiteral != true) { expresionLine.Opcode = expresionLine.locationCounter.ToString("X6"); } break; case Globals.ExpresionData.Contents.EMPTY: //consider break break; } } if (expresionLine.operationData.HasValue && expresionLine.operationData.Value.format == 2) { string[] tmp = expresionLine.operandFieldAndComment.Split(";", StringSplitOptions.RemoveEmptyEntries); if (tmp.Length > 0) { expresionLine.operandFieldAndComment = tmp[0]; } string[] subStrs = expresionLine.operandFieldAndComment.Split(",", StringSplitOptions.RemoveEmptyEntries); if (subStrs.Length == 1) { int val; if (dataStructures.opcodeTable.RegisterTable.TryGetValue(subStrs[0].Trim(), out val)) { expresionLine.Opcode = val.ToString(); } else { Chronicler.LogError("failed parsing F2: " + expresionLine.OriginalLine); } } else if (subStrs.Length == 2) { int val; if (dataStructures.opcodeTable.RegisterTable.TryGetValue(subStrs[0].Trim(), out val)) { expresionLine.Opcode = val.ToString() + "0"; } else { Chronicler.LogError("failed parsing F2: " + expresionLine.OriginalLine); } if (dataStructures.opcodeTable.RegisterTable.TryGetValue(subStrs[1].Trim(), out val)) { expresionLine.Opcode += val.ToString(); } else { Chronicler.LogError("failed parsing F2: " + expresionLine.OriginalLine); } } else { Chronicler.LogError("failed parsing F2, incorect term count: " + expresionLine.OriginalLine); } } if (expresionLine.operationData.HasValue) { expresionLine.Opcode = expresionLine.operationData.Value.code.ToString("X") + expresionLine.Opcode; } } DumpObjFile(filePath, expresionLines, dataStructures); }
static void DumpObjFile(string filePath, List <PassOne.ExpresionLine> expresionLines, Globals.DataStructures dataStructures) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath + ".txt")) { Chronicler.HoldOutput(); int curLinNum = 0; foreach (PassOne.ExpresionLine expLine in expresionLines) { if (expLine != null && expLine.OriginalLine != null && expLine.OriginalLine != "") { curLinNum = expLine.lineNumber; file.Write(curLinNum.ToString() + "\t"); if (expLine.CommentLine != true) { file.Write(expLine.locationCounter.ToString("X6") + "\t"); } file.Write(expLine.OriginalLine + "\t"); file.Write(expLine.Opcode); file.Write("\n"); Chronicler.Write(curLinNum.ToString() + "\t"); if (expLine.CommentLine != true) { Chronicler.Write(expLine.locationCounter.ToString("X6") + "\t"); } Chronicler.Write(expLine.OriginalLine + "\t"); Chronicler.WriteLine(expLine.Opcode); } } foreach (LiteralTable.LiteralValue lv in dataStructures.literalTable.literalTable) { curLinNum++; file.Write(curLinNum.ToString() + "\t"); file.Write(lv.address.ToString("X6") + "\t"); file.Write("*" + "\t"); file.Write(lv.label); file.Write("\n"); Chronicler.Write(curLinNum.ToString() + "\t"); Chronicler.Write(lv.address.ToString("X6") + "\t"); Chronicler.Write("*" + "\t"); Chronicler.WriteLine(lv.label); } } Chronicler.HoldOutput(); dataStructures.symbolTable.Print(); Chronicler.HoldOutput(); dataStructures.literalTable.PrintTable(); using (System.IO.StreamWriter file = new System.IO.StreamWriter(filePath + ".obj")) { Chronicler.HoldOutput(); string buffer = ""; string prevOpCode = "start"; int sl = 0; if (expresionLines.Count > 0) { sl = expresionLines[0].locationCounter; } Chronicler.WriteLine("Object code:"); foreach (PassOne.ExpresionLine expLine in expresionLines) { if (expLine != null && expLine.OriginalLine != null && expLine.OriginalLine != "") { if ((buffer.Length + expLine.Opcode.Length) > 60 || (prevOpCode == "" && buffer != "")) { //dump buffer contents file.Write("T" + sl.ToString("X5") + buffer.Length.ToString("X2") + buffer); file.Write("\n"); Chronicler.WriteLine("T" + buffer); sl = expLine.locationCounter; //clear buffer buffer = ""; } prevOpCode = expLine.Opcode; //check continuity buffer += expLine.Opcode; //add curr line to buffer } } if (buffer != "") { //dump buffer contents file.Write("T" + sl.ToString("X5") + buffer.Length.ToString("X2") + buffer); file.Write("\n"); Chronicler.WriteLine("T" + buffer); //clear buffer buffer = ""; } foreach (LiteralTable.LiteralValue lv in dataStructures.literalTable.literalTable) { if (buffer != "" && (buffer.Length + lv.hexValue.Length) > 60) { //dump buffer contents file.Write("T" + sl.ToString("X5") + buffer.Length.ToString("X2") + buffer); file.Write("\n"); Chronicler.WriteLine("T" + buffer); //clear buffer buffer = ""; } buffer += lv.hexValue;//add curr line to buffer } if (buffer != "") { //dump buffer contents file.Write("T" + sl.ToString("X5") + buffer.Length.ToString("X2") + buffer); file.Write("\n"); Chronicler.WriteLine("T" + buffer); //clear buffer buffer = ""; } dataStructures.symbolTable.PrintFile(file); dataStructures.symbolTable.Print(); } }
//******************************************************************************************* //*** FUNCTION ParseTerms //*** *************************************************************************************** //*** DESCRIPTION : parses an arithmatic operation str to a symbol, or add a literal //*** INPUT ARGS : SymbolTable symbolTable, LiteralTable literalTable, //*** string expresion, string currentLine //*** OUTPUT ARGS : out Globals.Symbol? result //*** IN/OUT ARGS : N/A //*** RETURN : bool rtnVal //******************************************************************************************* static bool ParseTerms(Globals.DataStructures dataStructures, string expresionString, string currentLine, out Globals.ExpresionData expresionData) { bool rtnVal = true; expresionString = expresionString.Trim(); expresionData = new Globals.ExpresionData(); Regex testForCommentOnlyLine = new Regex(@"^[\t ]*(;.*)$"); Match commentOnlyLine = testForCommentOnlyLine.Match(expresionString); if (commentOnlyLine.Success) { expresionData.comment = commentOnlyLine.Groups["$1"].Value; return(true); } string line = expresionString.Trim(); if (rtnVal == true && (Regex.Match(line, @"^[\t ]{0,}(-{0,1}[\t ]{0,}[A-Za-z0-9]+).*$").Success != true)) { rtnVal = false; Chronicler.LogError("Could not parse first(" + line + ") term in: " + currentLine, "parsing terms"); } if (rtnVal == true && (Regex.Match(line, @"^[\t ]{0,}(-{0,1}[\t ]{0,}[A-Za-z0-9]+)[\t ]{0,}(([+-])[\t ]{0,}(-{0,1}[\t ]{0,}[A-Za-z0-9]+)){0,1}[\t ]{0,}(;.*){0,1}$").Success != true)) { rtnVal = false; Chronicler.LogError("Couldn't parse second term in: " + currentLine, "parsing terms"); } string first = ""; string arithmaticOperator = ""; string second = ""; if (rtnVal == true) { Match fullLine = (Regex.Match(line, @"^[\t ]{0,}(?<first>-{0,1}[\t ]{0,}[A-Za-z0-9]+)[\t ]{0,}(?<testTermCount>(?<operand>[+-])[\t ]{0,}(?<second>-{0,1}[\t ]{0,}[A-Za-z0-9]+)){0,1}[\t ]{0,}(;.*){0,1}$")); if (fullLine.Success != true) { rtnVal = false; Chronicler.WriteLine("Error parsing literal"); } first = fullLine.Groups["first"].Value.Trim(); arithmaticOperator = fullLine.Groups["operand"].Value.Trim(); second = fullLine.Groups["second"].Value.Trim(); Regex stripWhiteSpace = new Regex(@"\s+"); first = stripWhiteSpace.Replace(first, ""); arithmaticOperator = stripWhiteSpace.Replace(arithmaticOperator, ""); second = stripWhiteSpace.Replace(second, ""); if (second != "" && arithmaticOperator == "") { rtnVal = false; } } if (rtnVal == true) { rtnVal = ParseTerm(dataStructures.symbolTable, first, out expresionData.first, currentLine); if (second != "") { rtnVal = ParseTerm(dataStructures.symbolTable, second, out expresionData.second, currentLine); if (expresionData.first.HasValue && expresionData.second.HasValue) { if (arithmaticOperator == "+") { expresionData.rflag = Globals.Symbol.AddRFlags(expresionData.first.Value, expresionData.second.Value); expresionData.operatorValue = Globals.ExpresionData.Arithmetic.ADD; } else if (arithmaticOperator == "-") { expresionData.rflag = Globals.Symbol.SubtractRFlags(expresionData.first.Value, expresionData.second.Value); expresionData.operatorValue = Globals.ExpresionData.Arithmetic.SUBTRACT; } else { rtnVal = false; Chronicler.LogError("Invalid operator value for line: " + currentLine, "term arithmatic module"); } } else { Chronicler.LogError("Couldn't resolve symbols in artithmetic: " + currentLine, "term arithmatic module"); } rtnVal = rtnVal == true?ParseTerm(dataStructures.symbolTable, second, out expresionData.second, currentLine) : false; } if (expresionData.first.HasValue && !expresionData.second.HasValue && second == "") { expresionData.rflag = expresionData.first.Value.RFlag; } } return(rtnVal); }