예제 #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
        public void SomeTest()
        {
            Parser parser = new Parser();
            parser.currentTxtCommand = "D=A";

            String destMnemonic = parser.Dest();
            Assert.IsTrue(destMnemonic == "D", "parser failed");

            String compMnemonic = parser.Comp();
            Assert.IsTrue(compMnemonic == "A", "parser failed");

            string destBin = CodeGenerator.Dest(destMnemonic);
            Assert.IsTrue(destBin == "010");

            string compbin = CodeGenerator.Comp(compMnemonic);
            Assert.IsTrue(compbin == "0110000");
        }
예제 #3
0
        public void CompTest()
        {
            Parser parser = new Parser();

            parser.currentTxtCommand = "D=A";
            Assert.IsTrue(parser.Comp() == "A");

            parser.currentTxtCommand = "M=D";
            Assert.IsTrue(parser.Comp() == "D");

            parser.currentTxtCommand = ";JMP";
            parser.Comp();
            Assert.IsTrue(parser.Comp() == String.Empty);

            parser.currentTxtCommand = "0;JMP";
            Assert.IsTrue(parser.Comp() == "0");

            parser.currentTxtCommand = "M=D+M";
            Assert.IsTrue(parser.Comp() == "D+M");

            parser.currentTxtCommand = "MD=M+1";
            Assert.IsTrue(parser.Comp() == "M+1");

            parser.currentTxtCommand = "D;JNE";
            Assert.IsTrue(parser.Comp() == "D");

            bool exceptionThrown = false;
            try
            {
                parser.currentTxtCommand = "@0";
                parser.Comp();
            }
            catch
            {
                exceptionThrown = true;
            }
            Assert.IsTrue(exceptionThrown);
        }
예제 #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

        }