I understood that I should call sys.init() at the beginning my program but I can't understand how. If I translate xxx.vm and with the bootstrap code I add the code that translates call sys.init 0 how can the program work if it hasn't the definition of the sys.init function? I mean, if there's only the call of the function (that I always translate by default at the beginning of the translation) how could it work? I think I'm missing something about the translating all the directory that I don't understand too
You are correct. When you generate the bootstrap code, the program (collection of VM files) must contain a Sys.init function. But, once you start writing the bootstrap code, you can no longer translate and test the earlier test programs that don't have Sys.init.
The way that I handle this dilemma is that I only generate the bootstrap code when I'm translating a directory.
VmTranslator file.vm -> file.asm without bootstrap
VmTranslator dir dir/*.vm -> dir.asm with bootstrap
It's handy to detect '.' so that you can get the parent directory name. Then you can support
VmTranslator . *.vm -> parent.asm with bootstrap
The expected use for the translator (other than the test programs) is that the programs are generated by the Jack compiler and that the OS files from nand2tetris/tools/OS have been manually copied into the directory.
Thank you a lot, I'll take a look to those links.
Anyway I don't understand the else part. Why bootstrapping is done here? I should do it anyway if the input it's a directory or not. I also don't understand what the other lines should do