public ExpresionData() { literal = null; first = null; operatorValue = null; second = null; N = false; I = false; X = false; B = false; P = false; E = false; }
public static bool ParseNewStyleLiteral(string operand, out LiteralTable.LiteralValue literalValue, out string comment, string callingModule = "") { operand = operand.Trim(); callingModule = callingModule == "" ? "Parse New Style Literal" : callingModule; literalValue = new LiteralTable.LiteralValue(); comment = ""; if (operand[0] != '=') { Chronicler.LogError("New style literals must begin with '='", callingModule); return(false); } operand = operand.Substring(1, operand.Length - 1).Trim(); bool rtnVal = ParseOldStyleLiteral(operand, out literalValue, out comment, callingModule); literalValue.isOldStyleLiteral = false; return(rtnVal); }
public static bool ParseOldStyleLiteral(string operand, out LiteralTable.LiteralValue literalValue, out string comment, string callingModule = "") { Regex literalRegex = new Regex(@"(^[\t ]*(?<literalCharID>[Cc])[\t ]*(?<literalValueString>'.*')[\t ]*(?<comment>;.*){0,1}$)|(^[\t ]*(?<literalCharIDx>[Xx])[\t ]*(?<literalValueStringx>'[a-fA-F0-9]*')[\t ]*(?<commentx>;.*){0,1}$)"); Match match = literalRegex.Match(operand); literalValue = new LiteralTable.LiteralValue(); comment = ""; if (match.Success)//if we succeed, format it { literalValue.isOldStyleLiteral = true; if (match.Groups["literalCharID"].Value != "") { literalValue.label = match.Groups["literalCharID"].Value + match.Groups["literalValueString"].Value; comment = match.Groups["comment"].Value; StringBuilder sb = new StringBuilder(literalValue.label); sb[0] = 'C'; literalValue.label = sb.ToString(); char[] tmpArr = literalValue.label.Substring(2, literalValue.label.Length - 3).ToCharArray(); literalValue.hexValue = Globals.CharArrToHexStr(tmpArr); literalValue.dataType = LiteralTable.LiteralValue.DataType.CHAR; } else { literalValue.label = match.Groups["literalCharIDx"].Value + match.Groups["literalValueStringx"].Value; comment = match.Groups["commentx"].Value; literalValue.label = literalValue.label.ToUpper(); literalValue.hexValue = literalValue.label.Substring(2, literalValue.label.Length - 3); literalValue.hexValue = (literalValue.hexValue.Length % 2 == 1 ? "0" : "") + literalValue.hexValue; literalValue.dataType = LiteralTable.LiteralValue.DataType.HEX; } return(true); } //if we fail try to figure out how and provide detailed feedback StringBuilder operandMutaion = new StringBuilder(operand.Trim()); callingModule = callingModule == "" ? "Parse Old Style Literal" : callingModule; if (operandMutaion.Length == 0) { Chronicler.LogError("No literal value was given", callingModule); } operandMutaion[0] = char.ToUpper(operandMutaion[0]); bool isChar = false; if (operandMutaion[0] != 'C' && operandMutaion[0] == 'X') { Chronicler.LogError("The charachter '" + operandMutaion[0] + "' is not a valid literal identifier. Valid identifiers are 'XxCc'.", callingModule); return(false); } else if (operandMutaion[0] == 'C') { isChar = true; } //remove whitespace while (operandMutaion.Length > 2 && char.IsWhiteSpace(operandMutaion[1])) { operandMutaion = operandMutaion.Remove(1, 1); } if (operandMutaion[1] != '\'') { Chronicler.LogError("Was expecting ''', but found '" + operandMutaion[1] + "'", callingModule); return(false); } int lastApos = -1; for (int i = 2; i < operandMutaion.Length; i++) { if (operandMutaion[i] == '\'') { lastApos = i; } } if (lastApos == -1) { Chronicler.LogError("Was unable to find closing '''", callingModule); return(false); } else { string subStr = operandMutaion.ToString().Substring(2, lastApos - 2); if (!isChar) { char?errorChar; if (!validateHex(subStr, out errorChar) && errorChar != '\'') { Chronicler.LogError("Invalid Hex char detected in hex literal: " + errorChar.Value, callingModule); return(false); } else { lastApos = -1; for (int i = 2; i < operandMutaion.Length && lastApos == -1; i++) { if (operandMutaion[i] == '\'') { lastApos = i; } } } } int lastComma = operandMutaion.Length; for (int i = lastApos; i < operandMutaion.Length && lastComma == operandMutaion.Length; i++) { if (operandMutaion[i] == ';') { lastComma = i; } } subStr = operandMutaion.ToString().Substring(lastApos, lastComma - lastApos); Chronicler.LogError("Parsing literal failed; was their garbage after the literal before the comment? Found:(" + subStr + ")", callingModule); return(false); } }
////******************************************************************************************* ////*** 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); }