A compiler for jack language, designed by instructors in the Nand2Tetris course.
It is a compiler for a High-Level OO programming language called Jack, designed by instructors in Nand2Tetris course. The compiler task compiling your code from Jack OOPL to VM language. VN is a procedural language, that was also designed by the instructors form the Nand2Tetris course.
I build this compile with just Java.
I faced many challenges in this project, but the main one was building the parser.
- What is the architecture of the parser?
- How to get benefits from the result of my Tokenizer?
- How to make the next step (Building code generator) after built the parser more easier?
After the tokenizer finishes its work, it gives a stream of objects, each of them presents information for a single token, And it makes it easier to start working with objects instead of starting with a stream of chars again from plain text. Also for generating code it will be hard to work just starting with a stream of objects without but them in some more complex object that can present some more advanced contexts. So I find the best solution for me, is to build this parser as an OOP project, that each command of Jack language can represent as an object, for example, class jack command instruction is an object, that contains multiple objects as properties of ClassJackCommand object, and these objects are all subroutines of the jack class object, and variable declarations. and each subroutine and variable declarations are also objects that contain other objects that can represent more specific commands and so on.
The solution that I chose answered my questions, but in the same time is make me recognize some big costs that my solution lead to, although I chose it in the end.
The big cost was the memory, if you imagine that the compiler will build a real parsing tree in the memory, and your code is millions or billions of lines, this will lead to high memory and time costs, because the compiler will record each class and subroutines or statement as an object, to be more detectable, each word or symbol in your base code, will have an object in the memory represent it, so it's a big cost. So another solution that you can follow: is to build the compiler in a more procedural way, I haven't tried this solution before, but I can expect, it may be more challenging, and it's the recommended solution in Nand2Tetris course.
To install the project you can open your terminal and enter this command:
git clone https://github.com/Ahmed101Mohammed/Jack-compiler.git
You can follow the following steps to compile your assembly code:
- Enter the project folder, after installing it.
- Compiling the JackAnalyzer.java file, which should lead to compiling all dependencies files that the JackAnalyzer file needs.
- Run the project, as a normal terminal java app, and the entry point is JackAnalyzer.
- Just so you know – the program will ask you to input the path from the root of the compiler, to the target file, or folder.
- Finally, you will get the results with a separate file/s ending with the vm extension, these files will be next to the compiled files. The results not only contain vm files, it also contain 2 xml files for each jack file, which contains the result of the tokenizing and parsing phases.
There is no famous specific license, but I give you all the ability to use this project for good reasons, and I will be thanked for sharing my GitHub account as a creator for this basic compiler. Thanks.