Sys.init

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Sys.init

asev98
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
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

cadet1620
Administrator
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.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

asev98
So should I put the call sys.init translation code at the beginning? And how can I do to translate all the directory? I don't understand also how to use all the directory as input
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

cadet1620
Administrator
Yes, the bootstrap code must be the first thing in the .asm file so that is the first thing that gets executed.

How to test if the argument is a directory and how to enumerate the files in that directory vary between languages.  What language are you using to write your translator?

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

asev98
cadet1620 wrote
Yes, the bootstrap code must be the first thing in the .asm file so that is the first thing that gets executed.
So have I to translate sys.init by default or do I have to check something before?

cadet1620 wrote
 What language are you using to write your translator?
I'm using C
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

cadet1620
Administrator
C is a bit tough because there is no standard cross-platform way to handle directories.

You will want to write functions
    int isDir(char *path);               // returns 1 if path is a directory
    char **vmFiles(char *path);     // returns malloc()ed array of malloc()ed strings, array ends with NULL

These links will give you help writing those functions.

Windows
    Test if path is a directory
        https://msdn.microsoft.com/en-us/library/windows/desktop/bb773621(v=vs.85).aspx
       
    Listing files in a directory
        https://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx

Linux/Mac      
    Test if path is a directory
        https://stackoverflow.com/questions/3828192/checking-if-a-directory-exists-in-unix-system-call
       
    Listing files in a directory
        https://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program

Your main() will do something like
if (!isDir(argv[1])) {
    outName = changeExtension(argv[1], ".asm");
    outFile = fopen(...
    translate(argv[1], outFile);
    fclose(outFile);
} else {
    (write C code for these)
    (outName = last component of argv[1] )
    (outName = argv[1] + '/' + outName      // '/' works on windows an unix
    vmName = vmFiles(argv[1]);
    outFile = fopen(...
    writeBootstrap(outFile);
    while (**vmNames) {
        ( vmName = argv[1] + '/' + *vmNames )
        translate(vmName, outFile);
        free(*vmNames);     // prevent memory leak
        ++vmNames;
    }
    free(vmNames);      // prevent memory leak
    fclose(outFile);

--Mark
       
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

asev98
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
Reply | Threaded
Open this post in threaded view
|

Re: Sys.init

cadet1620
Administrator
This has become too complex for the forum.

Please email me and I'll help you as I have time available.
Let me know what your experience level in C is and what OS you are using.

--Mark