All right, so let us begin to discuss how to actually build a VM translator using some high level language. Well to begin with I'd like to remind you as I did in the previous unit, what the VM translator is all about. It's a program that reads a given text file, which contains VM commands and produces from it another text file that contains assembly commands. So for example the VM translator can, begins to parse this particular command, the push constant 17, breaks it into its lexical elements and builds from it the output @17, D=A and so on and so forth. Then it moves on to the next command, it processes it also and then it generates the assembly code that realizes push local 2 on the target platform. Then it does the same with add. It reads add and it does some hocus focus in assembly that causes a stack oriented add operation. Pop local 2 is translated into a set of assembly commands that represent pop local 2, and so on and so forth until the end of the input file. So that's what the VM translator is all about. What I would like to do next is show you how this VM translator will be used once you end up writing it. So let us assume that we've implemented a VM translator and we want to apply it to a particular VM program that we wrote before. Either a test program or some real program that we wrote in one way or another. And now we seek to translate this program into assembly code. How do we do it? Well, on my computer, I open a terminal window. If this were Windows, I would probably do the same using the command prompt. And then because I wrote this VM Translator using Java, I apply the Java runtime system to my VM Translator and I supply the name of the Solice program as a parameter to my VM Translator. And once the translation has ended, if I will look again into the current directory, I will see that a new file was created. This new file has the same name as my input file, but it has a different extension, an asm extension. And presumably this file contains the assembly code which is the translation of the source VM code. Now, obviously, the next thing that we do is that we take a very eager look into this asm file, using some plain text editor. And I would like to try to make sure that the code that my VM translator generated is indeed valid. But that's a separate story, we'll discuss it later when we talk about compare files and so on. So now that we've seen this VM translator in action we're ready to talk about how to actually build the VM translator. Now, it really all depends on which implementation language you're going to use, and it can be Java or Python, for example. And whichever language you will use, we recommend a certain software architecture which we think is appropriate for this project. So we propose to develop three stand alone modules if you will or in the world of Java this would be called the three different classes, a Parser class, a CodeWriter class and a Main class. And the Main class is just a surrogate name for the class that we saw before which will be called VM Translator. So, the main class, the VM Translator has one input which is file name .vm. And it generates as output as filename.asm as we saw in the demo that they previously showed you. Now, how do this main class operate? Well, the logic of this main class is that it will have some main method that does the following. First of all, it constructs a parser, in order to handle the input file, and we'll discuss this parser later on. Then it constructs a codewriter, to handle the generation of the code that we have to output, and we'll discuss this CodeWriter later as well. And then once we have this to object setup, we can march through the input file, through fileName.vm and handle each row in this file separately. For each of these rows which represents a VM command, we can parse this role or line using a Parser, [COUGH] sorry, using a Parser functionality. And once we parse this line into it's constituent VM elements, we can pass this information to the CodeWriter in order to generate the appropriate code in assembly that we will later put together and write to the output file. So that's the general logic of the main class called VM translator. And that's what you have to implement when you develop this particular class. Now I'd like to point out that as you see, in order to implement this class you have to know how to handle text files in your implementation language. So for example if you use Java you have to know how to open a text file, how to read the next line from this file. You have to know how to open the file for output. You have to know how to write strings into this output file and so on. And all of this obviously is not part of this course, it is on you. We assume that you know how to handle files in your implementation language. And if you don't, maybe you want to take a break from this course and spend a few hours on learning how to do this using Java, Python, or whatever language you use for this project. All right, so here's the API of the three modules, actually the two remaining modules, the Parser and the CodeWriter. The Parser, once again, handles the parsing of a single .vm file. It reads a VM command, it breaks it into its individual elements and it passes this information to the CodeWriter. And in the process it also removes all the white space and the comments from the source file. So it doesn't really remove them, it simply ignores them. It goes over the whitespace and focuses only on the real essence of the file, which are the commands themselves. So as usual, a module consists of a set of methods and constructors and here is what we propose to do. We propose to first of all write a constructor, who's job is to open the input file for processing. So it receives as a parameter, a string which is the name of the file and it goes on to open it. Then there is this method has more commands which is a boolean and it simply tells you if there are more commands to process in the input file. If there are more commands to process, if it has more commands, it's true, then we can do an advance. And by advance we take the current command, the command that we are now looking at in the file, and we turn into what we call the current command within our program. And we are going to refer to this current command In many different places. So moving along, the command type method looks at the current command and decides if this command is an arithmetic logical command, or a push or a pop or a label or a go to, and so on and so forth. And for every one of these command types, the command type method returns a constant. And once again, other methods in our API will use this constant down the road. Then there are two methods named Arg1 and Arg2, which return the arguments of the current command and you can read the specification to see exactly what these methods have to return. So this completes the description of the Parser API. Moving along, here's the CodeWriter API. This class or this module is responsible for generating the assembly code from the parsed commands. It also has a Constructor, and the Constructor opens the output file for processing and gets ready to write onto this file. Then there's a writeArithmetic method, which is responsible for writing The assembly code that implements the command which is the parameter of this method. And this is where most of your work will be invested, because based on which command you are currently processing, you have to write assembly code and there are several such commands, so there's some significant work to be done with this particular method. And the next method WritePushPop is also a heavyweight method, because based on whether you have a push, a pop and the segment in the index, you have to generate assembly code that implements this particular command. And finally, there's a close method that simply closes the output file and that's the end of the story. So, this completes the API of our basic VM translator and more routines will be added to this API in the next project where we handle the rest of the VM language. Now obviously what we gave here is what we think is a good architecture for the VM translator and you're welcome to add to your own code more private methods which are not exposed to the outside. And use these private methods to make your code more manageable, elegant, and readable. And with that, I want to remind you, what is the big picture, what we are doing. We have described the implementation over VM Translator that can handle the arithmetic, logical and the memory access commands. And what remains to be done is to complete this translator in the next project to handle also the branching commands and the function commands. So that's the end of this module, and moving along, the next module will describe how you actually have to do it and submit the project.