Example #1
0
 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);
 }
Example #2
0
        //************************************************************************
        //***  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);
            }
        }