Esempio n. 1
0
        public void ConstructorTest()
        {
            string outputPath = "../../../TestFiles/PrintNumber/UnitTest.vm";
            VmWriter vmWriter = new VmWriter(outputPath);

            Assert.IsTrue(File.Exists(outputPath), "file not found.");
        }
Esempio n. 2
0
        public CompilationEngineVm(IList <Pair <string, string> > classTokensList, VmWriter vmWriter, string className)
        {
            // reverse tokens before push onto stack (so we Pop them in the correct order!)
            classTokensList  = classTokensList.Reverse().ToList();
            this.classTokens = new Stack <Pair <string, string> >();
            this.vmWriter    = vmWriter;
            this.className   = className;

            foreach (Pair <string, string> token in classTokensList)
            {
                this.classTokens.Push(token);
            }
        }
Esempio n. 3
0
        public CompilationEngineVm(IList<Pair<string, string>> classTokensList, VmWriter vmWriter, string className)
        {
            // reverse tokens before push onto stack (so we Pop them in the correct order!)
            classTokensList = classTokensList.Reverse().ToList();
            this.classTokens = new Stack<Pair<string, string>>();
            this.vmWriter = vmWriter;
            this.className = className;

            foreach (Pair<string, string> token in classTokensList)
            {
                this.classTokens.Push(token);
            }
        }
Esempio n. 4
0
        public void WriteGotoTest()
        {
            string outputPath = "../../../TestFiles/PrintNumber/UnitTest.vm";
            using (VmWriter vmWriter = new VmWriter(outputPath))
            {
                vmWriter.WriteGoto("SomeLabel");
            }

            using (StreamReader streamReader = new StreamReader(File.OpenRead("../../../TestFiles/PrintNumber/UnitTest.vm")))
            {
                string line = streamReader.ReadLine().Trim();
                Assert.IsTrue(string.Equals(line, "goto SomeLabel", StringComparison.InvariantCulture));
            }
        }
Esempio n. 5
0
        public void WriteArithmeticTest()
        {
            string outputPath = "../../../TestFiles/PrintNumber/UnitTest.vm";
            using (VmWriter vmWriter = new VmWriter(outputPath))
            {
                vmWriter.WriteArithmetic(ArithmeticCommand.Add);
            }

            using (StreamReader streamReader = new StreamReader(File.OpenRead("../../../TestFiles/PrintNumber/UnitTest.vm")))
            {
                string line = streamReader.ReadLine().Trim();
                Assert.IsTrue(string.Equals(line, "add", StringComparison.InvariantCulture));
            }
        }
Esempio n. 6
0
        public void Compile()
        {
            IList <Pair <string, string> > tokens = null;

            using (Tokenizer tokenizer = new Tokenizer(this.inputPath))
            {
                tokens = tokenizer.GetTokens();
            }

            tokens = tokens.Where(t => t.Value1 != "Comment").ToList();

            using (VmWriter vmWriter = new VmWriter(GetOutputPath()))
            {
                CompilationEngineVm compilationEngineVm = new CompilationEngineVm(tokens, vmWriter, Path.GetFileNameWithoutExtension(inputPath));
                compilationEngineVm.CompileClass();
            }
        }
Esempio n. 7
0
        public void WritePushTest()
        {
            string outputPath = "../../../TestFiles/PrintNumber/UnitTest.vm";
            using(VmWriter vmWriter = new VmWriter(outputPath))
            {
                vmWriter.WritePush(Segment.Argument, 1);
            }

            using (StreamReader streamReader = new StreamReader(File.OpenRead("../../../TestFiles/PrintNumber/UnitTest.vm")))
            {
                string line = streamReader.ReadLine().Trim();
                Assert.IsTrue(string.Equals(line, "push Argument 1", StringComparison.InvariantCulture));
            }
        }