Пример #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
        }
Пример #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++;
                        }
                    }
                }
        }
Пример #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++;
                    }
                }
            }
        }
Пример #4
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

        }