//******************************************************************************************* //*** FUNCTION ParseTerm //*** *************************************************************************************** //*** DESCRIPTION : parses an term str(int or symbol) to a symbol struct //*** INPUT ARGS : SymbolTable symbolTable, string term, string currentLine="" //*** OUTPUT ARGS : out Globals.Symbol? sym //*** IN/OUT ARGS : N/A //*** RETURN : bool rtnVal //******************************************************************************************* static bool ParseTerm(SymbolTable symbolTable, string term, out Globals.Symbol?sym, string currentLine) { bool rtnVal = true; if (char.IsDigit(term[0]) || (term[0] == '-' && char.IsDigit(term[1]))) { sym = null; if (ParseNum(term, out Globals.Symbol? tmp, currentLine) == true) { sym = tmp; } else { Chronicler.LogDetailedInfo("Parse term(digit) in Expresion Handler failed"); rtnVal = false; } } else { int Mval = 1; if (term[0] == '-') { term.Substring(1, term.Length - 1); Mval = -1; } Chronicler.LogDetailedInfo("Parse term(sym) start"); if ((symbolTable.SearchSymbol(term.Trim(), currentLine, out sym)) != true) { Chronicler.LogDetailedInfo("Parse term(sym) in Expresion Handler failed"); rtnVal = false; } if (sym.HasValue) { Globals.Symbol tmp = sym.Value; tmp.value *= Mval; sym = tmp; } Chronicler.LogDetailedInfo("Parse term(sym) end"); } return(rtnVal); }
//******************************************************************************************* //*** FUNCTION ParseNum //*** *************************************************************************************** //*** DESCRIPTION : parses an int str to a symbol //*** INPUT ARGS : string digits, string currentLine="" //*** OUTPUT ARGS : out Globals.Symbol? sym //*** IN/OUT ARGS : N/A //*** RETURN : bool rtnVal //******************************************************************************************* static public bool ParseNum(string digits, out Globals.Symbol?sym, string currentLine = "") { Globals.Symbol tmp = default; bool rtnVal = true; if (int.TryParse(digits, out tmp.value) == true) { tmp.label = digits; tmp.RFlag = false; tmp.MFlag = false; sym = tmp; } else { Chronicler.LogError("Unable to parse integer value(" + digits + "), skipping: \"" + currentLine + "\"", "Resolving Expresion"); sym = null; rtnVal = false; } return(rtnVal); }
public bool addSymbol(string label, bool rflag, int value, string currentLine) { label = label.Trim(); if (ValidateLabel(label, currentLine, "Pass One adding symbol")) { Globals.Symbol newSym = default; newSym.label = label; if (newSym.label.Length > 6) { newSym.label = newSym.label.Substring(0, 6); } newSym.RFlag = rflag; newSym.value = value; newSym.MFlag = false; if (SymbolTableBST.ContainsKey(newSym.label)) { Chronicler.LogError("Symbol with same Label('" + newSym.label + "') already exists! Setting MFlag and \n\tskipping: \"" + currentLine + "\"" + "\n", "Adding Symbol"); Globals.Symbol sym = SymbolTableBST.GetValueOrDefault(newSym.label); if (sym.MFlag == false) { sym.MFlag = true; SymbolTableBST.Remove(newSym.label); SymbolTableBST.Add(newSym.label, sym); } return(false); } else { Chronicler.Write("Adding symbol: ", Chronicler.OutputOptions.INFO); newSym.Print(Chronicler.OutputOptions.INFO); SymbolTableBST.Add(newSym.label, newSym); } } else { return(false); } return(true); }
//************************************************************************ //*** FUNCTION LoadSymbols //*** ******************************************************************** //*** DESCRIPTION : opens a file and populates SymbolTableBST //*** INPUT ARGS : string filePath //*** OUTPUT ARGS : N/A //*** IN/OUT ARGS : N/A //*** RETURN : N/A //************************************************************************ public void LoadSymbols(string filePath) { Globals.Symbol symbol = default; symbol.label = ""; symbol.IFlag = true; char[] tmpLabel = new char[7]; //discard line flag bool discardLine; //get the line and trim whitespace string currentLine; string[] rflagAndValueStrings; string rFlag; 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) { //discard line flag discardLine = false; //get the line and trim whitespace currentLine = streamReader.ReadLine().CompactAndTrimWhitespaces(); if (currentLine == "") { Chronicler.LogError("blank line, skipping: \"" + currentLine + "\"" + "\n", "Adding Symbol"); discardLine = true; } else if (currentLine.CountStringCharachters(':') != 1)//check incorrect colon seperator count { Chronicler.LogError("Extra colon seperators in current line, skipping: \"" + currentLine + "\"" + "\n", "Adding Symbol"); discardLine = true; } if (!discardLine) { string[] symbolSubstrings = currentLine.Split(':'); //validate Label string label = symbolSubstrings[0].Trim(); discardLine = !ValidateLabel(label, currentLine, "Adding Symbol"); if (!discardLine) { rflagAndValueStrings = symbolSubstrings[1].Trim().Split(' '); //validate RFlag rFlag = rflagAndValueStrings[0].Trim(); discardLine = TestAndSetRFlag(rFlag, out symbol.RFlag, currentLine, "Adding Symbol"); if (!discardLine) { string value = rflagAndValueStrings[1].Trim(); if (!int.TryParse(value, out symbol.value) || value[0] == '+') { discardLine = true; Chronicler.LogError("Invalid integer value(" + value + "), skipping: \"" + currentLine + "\"" + "\n", "Adding Symbol"); } if (!discardLine) { //parse data into Symbol struct int symbolLabelIndex = 0; for (symbolLabelIndex = 0; symbolLabelIndex < 6 && symbolLabelIndex < label.Length; symbolLabelIndex++) { tmpLabel[symbolLabelIndex] = label[symbolLabelIndex]; } tmpLabel[symbolLabelIndex] = '\0'; StringBuilder sb = new StringBuilder(""); foreach (char c in tmpLabel.TakeWhile(c => c != '\0')) { sb.Append(c); } symbol.label = sb.ToString().Trim(); symbol.MFlag = false; if (SymbolTableBST.ContainsKey(symbol.label)) { Chronicler.LogError("Symbol with same Label('" + symbol.label + "') already exists! Setting MFlag and \n\tskipping: \"" + currentLine + "\"" + "\n", "Adding Symbol"); Globals.Symbol sym = SymbolTableBST.GetValueOrDefault(symbol.label); if (sym.MFlag == false) { sym.MFlag = true; SymbolTableBST.Remove(symbol.label); SymbolTableBST.Add(symbol.label, sym); } } else { Chronicler.Write("Adding symbol: ", Chronicler.OutputOptions.INFO); symbol.Print(Chronicler.OutputOptions.INFO); SymbolTableBST.Add(symbol.label, symbol); } } } } } } } } catch (IOException e) { Chronicler.LogError("failed to open File: " + filePath); } } } catch (IOException e) { Chronicler.LogError("failed to open File: " + filePath); } }