Beispiel #1
0
        public void GetArg1and2Test()
        {
            Parser parser = new Parser("push constant 2", true);
            Assert.IsTrue(parser.GetArg1() == "constant");
            Assert.IsTrue(parser.GetArg2() == "2");

            parser = new Parser("pop static 8", true);
            Assert.IsTrue(parser.GetArg1() == "static");
            Assert.IsTrue(parser.GetArg2() == "8");

            parser = new Parser("label IF_FALSE", true);
            Assert.IsTrue(parser.GetArg1() == "IF_FALSE");

            parser = new Parser("goto IF_FALSE", true);
            Assert.IsTrue(parser.GetArg1() == "IF_FALSE");

            parser = new Parser("if-goto IF_TRUE", true);
            Assert.IsTrue(parser.GetArg1() == "IF_TRUE");

            parser = new Parser("function Main.fibonacci 0", true);
            Assert.IsTrue(parser.GetArg1() == "Main.fibonacci");
            Assert.IsTrue(parser.GetArg2() == "0");

            parser = new Parser("call Main.fibonacci 1", true);
            Assert.IsTrue(parser.GetArg1() == "Main.fibonacci");
            Assert.IsTrue(parser.GetArg2() == "1");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            if (args.Length != 1 || !args[0].EndsWith(".vm"))
            {
                Console.WriteLine("Usage: VMTranslator <file>.vm");
                return;
            }

            string inputFile = args[0];
            string outputFile = Path.ChangeExtension(inputFile, "hack");
            var parser = new Parser(new FileStream(inputFile, FileMode.Open));
            var writer = new CodeWriter(new FileStream(outputFile, FileMode.Create));
        }
Beispiel #3
0
        public void GetCommandTypeTest()
        {
            Parser parser = new Parser("sub", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_ARITHMETIC);

            parser = new Parser("push constant 2", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_PUSH);

            parser = new Parser("pop static 8", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_POP);

            parser = new Parser("label IF_FALSE", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_LABEL);

            parser = new Parser("goto IF_FALSE", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_GOTO);

            parser = new Parser("if-goto IF_TRUE", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_IF);

            parser = new Parser("function Main.fibonacci 0", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_FUNCTION);

            parser = new Parser("return", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_RETURN);

            parser = new Parser("call Main.fibonacci 1", true);
            Assert.IsTrue(parser.GetCommandType() == CommandType.C_CALL);
        }