In this project we write a compiler for the Jack language. To make your life easier, I have already written the lexical and syntactic analysis, you only have to fill in the code generation.
You can find my code as pp14.zip in the Resources section on Piazza. You can also ignore my code and write your own, for instance using flex and bison.
My compiler consists of four source files:
To run the compiler, call it as
> python3 jackcompiler.py Main.jackor
> python3 jackcompiler.py directoryThe second form compiles all .jack files found in the directory.
I have already provided a framework for code generation in the Compiler class in jackcompiler.py. If you finish all methods of this class, you will have a working compiler. You should not need to change anything in the three other files.
To keep the project simple, you do not need to implement any error handling at all. Assume that your Jack input code is correct.
The project page on the Nand2Tetris web site has hints for testing and explains the test code. Basically, try your compiler on the various Jack sources provided in projects/11, then inspect the generated VM files, and try them on the VM emulator. (You can ignore the description of "Stage I" on that page—our parse tree already contains all the information about the symbols used.)
Undergraduate students can ignore fields, methods, and constructors. Your compiler does not need to handle those—instead, concentrate on functions, local variables, and static variables. In particular, the only function calls you need to handle are of the form
class_name.function_name
You can test your compiler with the Jack examples Seven, Average, ConvertToBin, and ComplexArrays.
Everything has to work :-)
In particular, you need to handle function calls for the forms
class_name.function_name variable.method_name method_nameThe second form calls a method of the object stored in the variable, the last form calls a method of the current object this.
In addition to the tests above, you can also test using the projects Square and Pong (and of course your own 2048 implementation).
Upload jackcompiler.py to our submission server. (If you ignored my code and wrote your own, upload your own file instead. You can either submit a single source file or a zip file with all sources.)