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

            Assert.IsTrue(File.Exists(outputPath), "file not found.");
        }
예제 #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);
            }
        }
예제 #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);
            }
        }
예제 #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));
            }
        }
예제 #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));
            }
        }
예제 #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();
            }
        }
예제 #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));
            }
        }