Exemple #1
0
        /********************************************************************
         *** FUNCTION    : ProcessFile                                     ***
         *********************************************************************
         *** DESCRIPTION : run pass one                                    ***
         *** INPUT ARGS  : string inFile, OpcodeTable opcodeTable          ***
         *** OUTPUT ARGS : No Output Args                                  ***
         *** RETURN      : Void                                            ***
         *********************************************************************/
        public void ProcessFile(string inFile, OpcodeTable oTable)
        {
            BinSearchTree sTable = new BinSearchTree();
            LinkedList    lTable = new LinkedList();

            //read file
            string[] program = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), ($@"{Environment.CurrentDirectory}\\..\\..\\" + inFile)));
            var      file    = inFile.Remove(inFile.IndexOf('.')) + ".tmp";
            int      num     = 0;
            string   sAdd;
            string   LC;
            string   pLength;

            string[] line = ProcessLine(program[num], out bool check);
            File.Create(file).Close();

            while (check)   //while there is lines
            {
                num++;
                line = ProcessLine(program[num], out check);
            }

            if (line[1] == "START") //for start
            {
                sAdd = line[2];
                LC   = sAdd;
                sTable.Insert(new Symbol()
                {
                    Element = line[0],                          //insert new symbol
                    Value   = int.Parse(LC, System.Globalization.NumberStyles.HexNumber),
                    RFlag   = true
                });
                ToFile(ref num, LC, line, file);    //put in file the symbol
                line = ProcessLine(program[num], out check);
            }
            else
            {
                sAdd = "00000"; //starting address 00000
                LC   = sAdd;
            }

            while (line[1] != "END")    //for end
            {
                int count = 0;
                if (line[1] != null && line[2] != null)
                {
                    if (line[0].Contains(";"))
                    {
                        check = true;
                    }

                    if (!check)
                    {
                        if (line[0].Contains(";"))  //check for comment
                        {
                            Console.WriteLine("Comment: " + line[0]);
                        }
                        if (line[0] != string.Empty && line[1] != "EQU")
                        {
                            try
                            { sTable.Search(line[2]); }
                            catch (KeyNotFoundException)
                            {
                                string symbol = line[0];
                                if (char.IsLetter(symbol[0]))
                                {
                                    sTable.Insert(new Symbol()
                                    {
                                        Element = line[0] + ':',
                                        Value   = int.Parse(LC, System.Globalization.NumberStyles.HexNumber),
                                        RFlag   = true
                                    });
                                }
                                else
                                {
                                    if (line[0].Contains(";"))  //check for comment
                                    {
                                        Console.WriteLine("Comment: " + line[0]);
                                    }
                                    else
                                    {
                                        Console.WriteLine(string.Format("||ERROR|| Symbol {0} has an invalid character(s).", symbol));
                                    }
                                }
                            }
                        }
                        if (oTable.Search(line[1], out Opcode opcode))
                        {
                            count = opcode.form;
                        }
                        else if (line[1] == "WORD") //for word
                        {
                            count = 3;
                        }
                        else if (line[1] == "BYTE") //for byte
                        {
                            count = InsertL("=" + line[2]).length;
                        }
                        else if (line[1] == "RESW") //for resw
                        {
                            count = int.Parse(line[2]) * 3;
                        }
                        else if (line[1] == "RESB") //for resb
                        {
                            count = int.Parse(line[2]);
                        }
                        else if (line[2] == "FLOAT" || line[2] == "NORM" || line[2] == "FIX" || line[2] == "HIO" || line[2] == "TIO" || line[2] == "SIO")
                        {
                            count = 1;
                            string[] temp = line;
                            line[0] = temp[1];
                            line[1] = temp[2];
                            line[2] = " ";
                        }
                        else if (line[1] == "EQU")  //for equ
                        {
                            sTable.Insert(CheckSymbol(ref num, LC, line, sTable, file));
                            line = ProcessLine(program[num], out check);
                            continue;
                        }
                        else //if not any of those
                        {
                            Console.WriteLine(string.Format("||ERROR|| Opcode {0} not found.", line[2]));
                            num++;
                            line = ProcessLine(program[num], out check);
                            continue;
                        }

                        if (line[2][0] == '=')
                        {
                            lTable.Add(InsertL(line[2]));
                        }
                    }

                    ToFile(ref num, LC, line, file);
                    LC   = LCCount(LC, count);
                    line = ProcessLine(program[num], out check);
                }
                else
                {
                    num++;
                    line = ProcessLine(program[num], out check);
                }
            }

            ToFile(ref num, LC, line, file);
            ToScreen(file); //display sic
            pLength = ((int.Parse(LC, System.Globalization.NumberStyles.HexNumber) - int.Parse(sAdd, System.Globalization.NumberStyles.HexNumber)) + CutLiteral(ref lTable, ref num, ref LC, file)).ToString("X");
            Console.WriteLine("_______________________________________________________");
            Console.WriteLine("\nProgram Length = " + pLength);
            Console.WriteLine("_______________________________________________________");
            Console.WriteLine("");
            lTable.Display();
            Console.WriteLine("_______________________________________________________");
            Console.WriteLine("\n                    SYMBOL TABLE");
            Console.WriteLine("{0,-10} {1,-10} {2,-10} {3,-10} {4,-10}", "LABEL", "VALUE", "RFLAG", "IFLAG", "MFLAG");
            Console.WriteLine("-------------------------------------------------------");
            sTable.Display();
        }
Exemple #2
0
        /********************************************************************
        *** FUNCTION    : CheckSymbol                                     ***
        *********************************************************************
        *** DESCRIPTION : check symbols                                   ***
        *** INPUT ARGS  : ref int lineNumber, string LC, string[] line,   ***
        ***                 BinSearchTree symbolTable, string fileName    ***
        *** OUTPUT ARGS : None                                            ***
        *** RETURN      : Symbol                                          ***
        ********************************************************************/
        private static Symbol CheckSymbol(ref int lineNumber, string LC, string[] line, BinSearchTree symbolTable, string fileName)
        {
            List <Symbol> symbols = new List <Symbol>();
            bool          rFlag   = true;

            string[] sLine;

            if (int.TryParse(line[2], out int val))
            {
                rFlag = false;
            }
            else if (line[2] == "*")
            {
                rFlag = true;
                val   = int.Parse(LC, System.Globalization.NumberStyles.HexNumber);
            }
            else if (line[2].Contains("+")) //if has +
            {
                sLine = line[2].Split('+'); //split at +
                foreach (var symbol in sLine)
                {
                    symbols.Add(symbolTable.Search(symbol));
                }

                CheckExpression('+', sLine, symbols.ToArray(), out val, out rFlag);
            }
            else if (line[2].Contains("-")) //if has -
            {
                sLine = line[2].Split('-'); //split at -
                foreach (var symbol in sLine)
                {
                    symbols.Add(symbolTable.Search(symbol));
                }

                CheckExpression('-', sLine, symbols.ToArray(), out val, out rFlag);
            }
            ToFile(ref lineNumber, val.ToString("X"), line, fileName);
            return(new Symbol {
                Element = line[0],                      //new symbol
                Value = val,
                RFlag = rFlag,
                IFlag = true,
                MFlag = false
            });
        }