How do we know that a function definition has ended?

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

How do we know that a function definition has ended?

Anjum
In Figure 8.6 of the book it mentions that all labels within a function body should be prefixed with the name of the function plus a dollar sign (i.e. label 'loop' in function 'multiply' should be named 'multiply$loop').

My question is, how can we reliably determine when a function body has completed?
Functions can have multiple 'return' statements within them, so I cannot rely on that.

The only thing I can think of is that there is no 'global' scope in the Jack language - i.e. all statements (apart from the bootstrap code) have to live within one function or another. If that is indeed the case then there is no issue.

Can anyone verify this for me please?

Cheers.
Anjum.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

krakerjak
This post was updated on .
* that was wrong.

It doesn't matter where the function exits.
You know you are in the scope of a specific function until you hit the return statement and reach another function statement.
If you for some reason are not in scope of a function I have seen the VM emulator use null as the function...
ie. null$loop
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

Anjum
Ahhh! Thanks for the quick response krakerjak .

I wrongly assumed that Jack was like Java where each class file can contain one or more classes within it.
Anjum.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

krakerjak
This post was updated on .
If you are talking jack classes, yes. All in separate files.
functions can be multiple

So, a class is found in a jack file. Each class in it's own file.

Once converted to VM file
Each function is identified by the class it belongs to...
eg. function Circle.getCircumference

and labels are identified to the function it belongs to.
You know you have a problem in a vm file if you reach a second function command without encountering a return command...but you may encounter multiple return commands within a function.

finally, once converting to assembly you are creating unique labels in some fashion you devise yourself.
For me i am creating a random alphanumeric string (12 characters long) for every label i create.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

cadet1620
Administrator
Also note that the label can include the class name to make it unique across classes, e.g.: class1.function$label and class2.function$label.

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

Re: How do we know that a function definition has ended?

Anjum
Interesting points raised by krakerjak and Mark.

krakerjak - are you implying that all functions should be generated with a name that includes the name of the VM file it is defined in (e.g. if I have Circle.vm with a method function named getCircumference defined inside it, then should I name this function Circle.getCircumference or is this naming scheme decided by a higher-level component that I have not got to yet?

Mark - if there is a higher level component responsible for naming functions in the VM file then would I still need to qualify the label names with the name of the file as well?

PS: I create an auto-incrementing label index (which is reset to zero for each new VM file) that I append to the labels.

Cheers,
Anjum.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

cadet1620
Administrator
Correct. You need to include the class name in the generated function name.

The .vm files are generated by the compiler that you will write next. A separate .vm file is created for each .jack file compiled. The spec for the compiler is not to include the class name on the function names.

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

Re: How do we know that a function definition has ended?

Anjum
I just re-read Chapter 7 on this topic where it states (on page 109):

"Each Jack class consists of one or more methods. When the Jack compiler is applied to a directory which includes n class files, it produces n VM files (in the same directory). Each Jack method xxx within a class Yyy is translated into one VM function called Yyy.xxx within the corresponding VM file."

So I am now confused - I understand this to imply that all functions within a VM file will always be qualified by the name of the file that they reside in (i.e. I do not need to qualify the names during translation as they are already in the right form).

Have I mis-read this or is the book incorrect in stating this?

Cheers,
Anjum.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

krakerjak
In reply to this post by Anjum
Yes, your compiler will end up creating vm files that your vm to hack translator will use:
You are already seeing and handling this convention when translating vm files to hack .asm files.
You will encounter a vm command like this
function Class2.set 0
(from class2.vm in StaticsTest)

Using this style will allow you to have functions with the same name in separate classes. Otherwise requiring all function names to be unique.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

cadet1620
Administrator
In reply to this post by Anjum
My bad. Parity error in my brain. I'm at work and didn't take the time to check the book.

Sorry about the confusion,
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

Anjum
In reply to this post by krakerjak
Hi Mark,

I think I may have misunderstood your earlier remark where you said: "You need to include the class name in the generated function name."

I took this to mean that while translating from VM to ASM I would need to add the VM filename as a prefix to all functions that I read from within the VM file - I guess you meant that this needs to be the case in the next part where we start writing the compiler.

Cheers,
Anjum.
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

krakerjak
In reply to this post by Anjum
It is interesting to note that, although hack is rather simple, by observing the class/function/file handling during compilation and translation you get a bit of a preview as to how a language like java is able to allow use of inheritance and polymorphism
Reply | Threaded
Open this post in threaded view
|

Re: How do we know that a function definition has ended?

Anjum
I totally agree - this course has opened my eyes to so many things that I just took as being magic. It's a great course - I only wish I had come across it much earlier in my life, and again, kudos to all those who worked so hard to deliver this course!
Anjum.