//************************************************************************* //*** 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); }
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(); } }
public static void Execute(Globals.DataStructures dataStructures, out List <ExpresionLine> expresionLines, string filePath) { int locationCounter = 0; int lineNumber = 0; expresionLines = new List <ExpresionLine>(); Regex regex = new Regex(@"^([\t ]*(?<label>[^\s]+:)[\t ]*){0,1}[\t ]*(?<flag>\+{0,1}[\t ]*)(?<operation>[^\s]+)(?<operandFieldAndComment>[\t ].*){0,1}[\t ]*($|(\r?\n))"); int programLength = 0; bool skipOperandParsing = false; try { using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) { try { using (StreamReader streamReader = new StreamReader(fileStream)) { while (!streamReader.EndOfStream) { lineNumber++; string currentLine = streamReader.ReadLine().Trim(); Match match = regex.Match(currentLine); skipOperandParsing = false; if (match.Success) { ExpresionLine expresionLine = new ExpresionLine(); expresionLine.OriginalLine = currentLine; expresionLine.lineNumber = lineNumber; expresionLine.locationCounter = locationCounter; expresionLine.label = match.Groups["label"].Value.Trim(); expresionLine.operation = match.Groups["operation"].Value.Trim(); expresionLine.operandFieldAndComment = match.Groups["operandFieldAndComment"].Value.Trim(); expresionLine.lineNumber = lineNumber; expresionLine.format4indicator = match.Groups["flag"].Value.Trim(); expresionLine.DeferExpresionResolutiontoPass2 = false; if (expresionLine.label != "") { if (expresionLine.label[expresionLine.label.Length - 1] == ':') { expresionLine.label = expresionLine.label.Substring(0, expresionLine.label.Length - 1).Trim(); } else { expresionLine.validLine = false; Chronicler.LogError("Labels must end in a ':'"); } if (expresionLine.validLine) { SymbolTable.ValidateLabel(expresionLine.label, currentLine, "pass one"); } } Regex commentChecker = new Regex(@"[\t ]*(?<com>;.*)"); Match comment = commentChecker.Match(currentLine); if (comment.Success) { expresionLine.OriginalLine = currentLine; expresionLine.locationCounter = 0; expresionLine.label = ""; expresionLine.operation = ""; expresionLine.operandFieldAndComment = ""; expresionLine.lineNumber = lineNumber; expresionLine.format4indicator = ""; expresionLine.CommentLine = true; expresionLine.DeferExpresionResolutiontoPass2 = false; skipOperandParsing = true; expresionLines.Add(expresionLine); } else { if (dataStructures.opcodeTable.assemblerDirectives.Contains(expresionLine.operation)) { switch (expresionLine.operation) { case "EQU": skipOperandParsing = true; expresionLine.IsEQU = true; if (expresionLine.operandFieldAndComment[0] == '*' && expresionLine.validLine) { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } else if (expresionLine.validLine) { if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } else { if (expresionLine.expresionData.first.HasValue) { int value = expresionLine.expresionData.first.Value.value; 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 (expresionLine.expresionData.rflag.HasValue != true) { expresionLine.validLine = false; } if (expresionLine.validLine) { expresionLine.locationCounter = value; dataStructures.symbolTable.addSymbol(expresionLine.label, expresionLine.expresionData.rflag.Value, value, currentLine); } else { Chronicler.LogError("Couldn't add \"" + currentLine + "\" to symbol table.", "pass one"); } } else { expresionLine.expresionData.rflag = expresionLine.expresionData.first.Value.RFlag; if (expresionLine.validLine) { expresionLine.locationCounter = value; dataStructures.symbolTable.addSymbol(expresionLine.label, expresionLine.expresionData.rflag.Value, value, currentLine); } else { Chronicler.LogError("Couldn't add \"" + currentLine + "\" to symbol table.", "pass one"); } } } else { Chronicler.LogError("EQU can not be a literal value", "pass one"); expresionLine.validLine = false; } } } break; case "BYTE": if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } else { skipOperandParsing = false; if (expresionLine.expresionData.first.HasValue) { Chronicler.LogError("BYTE doesn't accept integer values", "pass one"); expresionLine.validLine = false; } else if (expresionLine.expresionData.literal != null) { if (expresionLine.expresionData.literal.isOldStyleLiteral) { if (expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } locationCounter += expresionLine.expresionData.literal.Length; expresionLine.Opcode = expresionLine.expresionData.literal.hexValue; } else { expresionLine.validLine = false; Chronicler.LogError("New style literals are not allowed in assembler directives"); } } } break; case "WORD": expresionLine.DeferExpresionResolutiontoPass2 = true; if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } else { if (expresionLine.expresionData.first.HasValue) { int value = expresionLine.expresionData.first.Value.value; 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 (expresionLine.expresionData.rflag.HasValue != true) { expresionLine.validLine = false; } if (expresionLine.validLine && expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } if (expresionLine.validLine) { expresionLine.Opcode = value.ToString("X"); locationCounter += 3; } else { Chronicler.LogError("Couldn't parse \"" + currentLine + "\"", "pass one"); } } else if (expresionLine.expresionData.literal != null) { Chronicler.LogError("BYTE doesn't accept Charachter/Hex values", "pass one"); expresionLine.validLine = false; } } break; case "START": skipOperandParsing = true; Regex testBlankLine = new Regex(@"[\t ]*;.*"); if (expresionLine.operandFieldAndComment == "") { locationCounter = 0; } else if (testBlankLine.IsMatch(expresionLine.operandFieldAndComment)) { locationCounter = 0; } else { if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } else { Regex stripComment = new Regex(@"[\t ]*(?<min>-{0,1})[\t ]*(?<val>[0-9]+)[\t ]*(;.*){0,1}"); Match match1 = stripComment.Match(expresionLine.operandFieldAndComment); if (match1.Success) { Globals.Symbol?tmp; ExpresionHandler.ParseNum(match1.Groups["min"].Value + match1.Groups["val"].Value, out tmp, "(" + match1.Groups["min"].Value + match1.Groups["val"].Value + ")" + currentLine); expresionLine.expresionData.comment = match1.Groups["$3"].Value; if (tmp.HasValue && locationCounter == 0) { locationCounter = tmp.Value.value; ExpresionLine.startLoc = locationCounter; } else { Chronicler.LogError("couldn't parse starting adress number, or start was not at beggining of the program", "Pass One"); } } else { expresionLine.validLine = false; Chronicler.LogError("couldn't parse starting adress", "Pass One"); } } } if (expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } break; case "END": skipOperandParsing = true; testBlankLine = new Regex(@"[\t ]*;.*"); if (expresionLine.operandFieldAndComment == "") { locationCounter = 0; } else if (testBlankLine.IsMatch(expresionLine.operandFieldAndComment)) { locationCounter = 0; } else { ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData); if (expresionLine.expresionData.first.HasValue) { int value = expresionLine.expresionData.first.Value.value; 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 (expresionLine.expresionData.second.HasValue && expresionLine.expresionData.rflag.HasValue != true) { expresionLine.validLine = false; } if (expresionLine.validLine) { Globals.Symbol?symbol; if (dataStructures.symbolTable.SearchSymbol(expresionLine.expresionData.first.Value.label, currentLine, out symbol)) { programLength = locationCounter - symbol.Value.value; } } else { Chronicler.LogError("Couldn't add \"" + currentLine + "\" to symbol table.", "pass one"); } } else { Chronicler.LogError("END can not be a literal value", "pass one"); expresionLine.validLine = false; } } if (expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } break; case "RESB": skipOperandParsing = true; if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } else { if (expresionLine.expresionData.first.HasValue) { int value = expresionLine.expresionData.first.Value.value; 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 (expresionLine.validLine && expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } else { Chronicler.LogError("Couldn't add \"" + currentLine + "\" to symbol table.", "pass one"); } if (expresionLine.validLine) { locationCounter += value; } } else if (expresionLine.expresionData.literal != null) { Chronicler.LogError("BYTE doesn't accept Charachter/Hex values", "pass one"); expresionLine.validLine = false; } } break; case "RESW": skipOperandParsing = true; if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } else { if (expresionLine.expresionData.first.HasValue) { int value = expresionLine.expresionData.first.Value.value; 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 (expresionLine.validLine && expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } else { Chronicler.LogError("Couldn't add \"" + currentLine + "\" to symbol table.", "pass one"); } if (expresionLine.validLine) { locationCounter += value * 3; } } else if (expresionLine.expresionData.literal != null) { Chronicler.LogError("BYTE doesn't accept Charachter/Hex values", "pass one"); expresionLine.validLine = false; } } break; case "EXTREF": string[] arr = expresionLine.operandFieldAndComment.Split(',', StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < arr.Length; i++) { arr[i] = arr[i].Trim(); dataStructures.symbolTable.addSymbol(arr[i], false, 0, currentLine); } break; default: Chronicler.LogDetailedInfo("Doing: " + expresionLine.operation); break; } } else { opcodeTable.operationData operDat; if (dataStructures.opcodeTable.operationTableDictionary.TryGetValue(expresionLine.operation, out operDat)) { if (expresionLine.label != "") { dataStructures.symbolTable.addSymbol(expresionLine.label, true, locationCounter, currentLine); } switch (operDat.format) { case 1: locationCounter += 1; break; case 2: locationCounter += 2; break; case 3: if (expresionLine.format4indicator == "") { locationCounter += 3; } else { locationCounter += 4; } break; case 4: locationCounter += 4; break; default: break; } if (Parser.guessOperandType(expresionLine.operandFieldAndComment) == Parser.OperandType.DISPLACEMENT_OR_ADDRESS) { expresionLine.DeferExpresionResolutiontoPass2 = true; } else { expresionLine.DeferExpresionResolutiontoPass2 = false; } expresionLine.operationData = operDat; } else { expresionLine.operationData = null; } Chronicler.Write("Parsing operation: " + expresionLine.operation + "\n", Chronicler.OutputOptions.INFO); } } if (expresionLine.validLine && skipOperandParsing != true && expresionLine.operandFieldAndComment != "" && !expresionLine.DeferExpresionResolutiontoPass2) { if (ExpresionHandler.ResolveF3F4Expresion(dataStructures, expresionLine.operandFieldAndComment, currentLine, out expresionLine.expresionData) != true) { expresionLine.validLine = false; } } expresionLines.Add(expresionLine); } else { Chronicler.LogError("Couldn't parse fields on line: " + lineNumber); } } } } catch (IOException e) { Chronicler.LogError("failed to open File: " + filePath); } } } catch (IOException e) { Chronicler.LogError("failed to open File: " + filePath); } dataStructures.literalTable.setStartAddress(locationCounter); DumpIntermidiateFile(filePath, expresionLines, dataStructures); }
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 ParseExpresionFile ////*** *************************************************************************************** ////*** DESCRIPTION : Parses the Expresion File ////*** INPUT ARGS : SymbolTable symbolTable, LiteralTable literalTable, string filePath ////*** OUTPUT ARGS : N/A ////*** IN/OUT ARGS : N/A ////*** RETURN : N/A ////******************************************************************************************* //public static void ParseExpresionFile(SymbolTable symbolTable, LiteralTable literalTable, string filePath) //{ // try // { // using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) // { // try // { // using (StreamReader streamReader = new StreamReader(fileStream)) // { // int LineNumber = 1; // Chronicler.WriteLine("EXPRESION\tVALUE\tRELOCATABLE\tN-Bit\tI-Bit\tX-Bit"); // while (!streamReader.EndOfStream) // { // //get the line and trim whitespace // string currentLine = streamReader.ReadLine().Trim(); // if (ResolveF3F4Expresion(symbolTable, literalTable, currentLine, currentLine, out Globals.Symbol? symbol)) // { // if (symbol != null) // { // StringBuilder sb = new StringBuilder(""); // for (int x = 0; x < (16 - currentLine.Length); x++) // { // sb.Append(" "); // } // Chronicler.Write(currentLine + sb.ToString()); // Chronicler.Write(symbol.Value.value.ToString()); // Chronicler.Write("\t" + ((symbol.Value.RFlag) ? "RELOCATABLE" : "ABSOLUTE")); // Chronicler.NewLine(); // } // } // else // { // Chronicler.LogError("Line: \"" + LineNumber + "\" Skipping: \"" + currentLine + "\"", "Expresion File Parsing"); // } // LineNumber++; // } // Chronicler.LogInfo("DONE loading symbols!"); // } // } // catch (IOException e) // { // Chronicler.WriteLine(e.Message); // Chronicler.LogError("failed to open File: " + filePath); // } // } // } // catch (IOException e) // { // Chronicler.WriteLine(e.Message); // Chronicler.LogError("failed to open File: " + filePath); // } //} //******************************************************************************************* //*** FUNCTION ResolveF3F4Expresion //*** *************************************************************************************** //*** DESCRIPTION : Resolves an expresion and adds a literal or gets a symbol //*** INPUT ARGS : SymbolTable symbolTable, LiteralTable literalTable, //*** string expresion, string currentLine //*** OUTPUT ARGS : out Globals.Symbol? symbol //*** IN/OUT ARGS : N/A //*** RETURN : bool rtnVal //******************************************************************************************* public static bool ResolveF3F4Expresion(Globals.DataStructures dataStructures, string expresionString, string currentLine, out Globals.ExpresionData expresionData, string callingModule = "ExpresionHandler") { expresionData = new Globals.ExpresionData(); bool rtnVal = true; Regex testXValue = new Regex(@"(^[^=][^,;]*(?<x>,x|X){0,1}.*$)|(^[\t ]*=.*$)"); bool hasXValue = testXValue.Match(expresionString).Groups["x"].Value != ""; expresionString = expresionString.Trim(); if (Parser.guessOperandType(expresionString) == Parser.OperandType.NEW_STYLE_LITERAL || Parser.guessOperandType(expresionString) == Parser.OperandType.OLD_STYLE_LITERAL) { Chronicler.LogInfo("Parsing as literal: " + expresionString); if (Parser.guessOperandType(expresionString) == Parser.OperandType.NEW_STYLE_LITERAL) { LiteralTable.LiteralValue tmp = new LiteralTable.LiteralValue(); string comment = ""; Parser.ParseNewStyleLiteral(expresionString, out tmp, out comment, callingModule); expresionData.literal = tmp; dataStructures.literalTable.add(tmp); } else if (Parser.guessOperandType(expresionString) == Parser.OperandType.OLD_STYLE_LITERAL) { LiteralTable.LiteralValue tmp = new LiteralTable.LiteralValue(); string comment = ""; Parser.ParseOldStyleLiteral(expresionString, out tmp, out comment, callingModule); expresionData.literal = tmp; } } else if (expresionString.Length > 0 && expresionString[0] == '@') { expresionString = expresionString.Substring(1, expresionString.Length - 1); hasXValue = testXValue.Match(expresionString).Groups["x"].Value != ""; if (hasXValue != true) { if (ParseTerms(dataStructures, expresionString, currentLine, out expresionData) != true) { rtnVal = false; } expresionData.N = true; expresionData.I = false; } else { rtnVal = false; Chronicler.LogError("Can not apply both indirect adressing \n\tand x register indexing, skipping: \"" + currentLine + "\"", "Resovling Expresion"); } } else if (expresionString.Length > 0 && expresionString[0] == '#') { expresionString = expresionString.Substring(1, expresionString.Length - 1); hasXValue = testXValue.Match(expresionString).Groups["x"].Value != ""; if (hasXValue != true) { if (ParseTerms(dataStructures, expresionString, currentLine, out expresionData) != true) { rtnVal = false; } expresionData.N = false; expresionData.I = false; } else { rtnVal = false; Chronicler.LogError("Can not apply both immediate adressing \n\tand x register indexing, skipping:\"" + currentLine + "\"", "Resovling Expresion"); } } else if (hasXValue == true) { expresionString = testXValue.Replace(expresionString, "$2$4$5"); if (ParseTerms(dataStructures, expresionString, currentLine, out expresionData) != true) { rtnVal = false; } expresionData.N = true; expresionData.I = true; expresionData.X = true; } else { rtnVal = ParseTerms(dataStructures, expresionString, currentLine, out expresionData); } return(rtnVal); }
//******************************************************************************************* //*** 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); }