Ejemplo n.º 1
0
        /// <summary>
        /// Seconds the pass.
        /// TODO - refactor! break into smaller methods!!!
        /// after the first symbol table filling pass,
        /// we march thru the lines again, the parser gives us each command and its type
        /// we pass these to the code generator to build our binary representation
        /// </summary>
        /// <param name="inputBytes">The input bytes.</param>
        /// <param name="symbolTable">The symbol table.</param>
        /// <returns></returns>
        private string FinalPass(byte[] inputBytes)
        {
            MemoryStream inputStream = new MemoryStream(inputBytes);

            using (Parser parser = new Parser(inputStream))
            {
                StringBuilder fullBinaryListing = new StringBuilder();
                // magic number, new variables in the hack platform are stored from address 16 upwards.
                int addressCounter = 16;

                //TODO  - horrible nested while and if chain - break into methods
                while (parser.HasMoreCommands())
                {
                    String binaryLine = String.Empty;
                    parser.Advance();
                    if (parser.CommandType() == Command.C_COMMAND)
                    {
                        binaryLine = CodeGenerator.GetFullCInstruction(parser.Comp(), parser.Dest(), parser.Jump()) + Environment.NewLine;
                        fullBinaryListing.Append(binaryLine);
                    }
                    else if (parser.CommandType() == Command.A_COMMAND)
                    {
                        binaryLine = this.Handle_A_Command(parser.Symbol(), ref addressCounter);

                        fullBinaryListing.Append(binaryLine + Environment.NewLine);
                    }
                }

                return(fullBinaryListing.ToString());
            }
            #endregion
        }
Ejemplo n.º 2
0
        /// <summary>
        /// First pass of the assembly file.
        /// Finds all symbols and stores them in a Dictionary along (with the next line number)
        /// i.e. fills the symbol table
        /// </summary>
        /// <param name="inputBytes">The input bytes.</param>
        /// <param name="symbolTable">The symbol table.</param>
        private void FillSymbolTable(byte[] inputBytes)
        {
            using (MemoryStream inputStream = new MemoryStream(inputBytes))
                using (Parser parser = new Parser(inputStream))
                {
                    int lineCounter = 0;
                    while (parser.HasMoreCommands())
                    {
                        parser.Advance();

                        if (parser.CommandType() == Command.L_COMMAND)
                        {
                            // store the lable and next line
                            this.SymbolTable.AddEntry(parser.Symbol(), lineCounter);
                        }
                        else if (parser.CommandType() == Command.C_COMMAND ||
                                 parser.CommandType() == Command.A_COMMAND)
                        {
                            // ignore non label commands in the asm file.
                            lineCounter++;
                        }
                    }
                }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// First pass of the assembly file.
        /// Finds all symbols and stores them in a Dictionary along (with the next line number)
        /// i.e. fills the symbol table
        /// </summary>
        /// <param name="inputBytes">The input bytes.</param>
        /// <param name="symbolTable">The symbol table.</param>
        private void FillSymbolTable(byte[] inputBytes)
        {
            using (MemoryStream inputStream = new MemoryStream(inputBytes))
            using (Parser parser = new Parser(inputStream))
            {
                int lineCounter = 0;
                while (parser.HasMoreCommands())
                {
                    parser.Advance();

                    if (parser.CommandType() == Command.L_COMMAND)
                    {
                        // store the lable and next line
                        this.SymbolTable.AddEntry(parser.Symbol(), lineCounter);
                    }
                    else if (parser.CommandType() == Command.C_COMMAND
                        || parser.CommandType() == Command.A_COMMAND)
                    {
                        // ignore non label commands in the asm file.
                        lineCounter++;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void SymbolTest()
        {
            Parser parser = new Parser();

            parser.currentTxtCommand = "@0";
            Assert.IsTrue(parser.Symbol() == "0");

            parser.currentTxtCommand = "@Something";
            Assert.IsTrue(parser.Symbol() == "Something");

            parser.currentTxtCommand = "(foo)";
            Assert.IsTrue(parser.Symbol() == "foo");

            bool exceptionThrown = false;
            try
            {
                parser.currentTxtCommand = "M=D+M";
                parser.Symbol();
            }
            catch
            {
                exceptionThrown = true;
            }
            Assert.IsTrue(exceptionThrown);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Seconds the pass.
        /// TODO - refactor! break into smaller methods!!!
        /// after the first symbol table filling pass,
        /// we march thru the lines again, the parser gives us each command and its type
        /// we pass these to the code generator to build our binary representation
        /// </summary>
        /// <param name="inputBytes">The input bytes.</param>
        /// <param name="symbolTable">The symbol table.</param>
        /// <returns></returns>
        private string FinalPass(byte[] inputBytes)
        {
            MemoryStream inputStream = new MemoryStream(inputBytes);

            using (Parser parser = new Parser(inputStream))
            {
                StringBuilder fullBinaryListing = new StringBuilder();
                // magic number, new variables in the hack platform are stored from address 16 upwards.
                int addressCounter = 16;

                //TODO  - horrible nested while and if chain - break into methods
                while (parser.HasMoreCommands())
                {
                    String binaryLine = String.Empty;
                    parser.Advance();
                    if (parser.CommandType() == Command.C_COMMAND)
                    {
                        binaryLine = CodeGenerator.GetFullCInstruction(parser.Comp(), parser.Dest(), parser.Jump()) + Environment.NewLine;
                        fullBinaryListing.Append(binaryLine);
                    }
                    else if (parser.CommandType() == Command.A_COMMAND)
                    {
                        binaryLine = this.Handle_A_Command(parser.Symbol(), ref addressCounter);

                        fullBinaryListing.Append(binaryLine + Environment.NewLine);
                    }
                }

                return fullBinaryListing.ToString();
            }
        #endregion

        }