Purpose of "this" as argument 0 in method subroutine symbol table

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

Purpose of "this" as argument 0 in method subroutine symbol table

sam11
This post was updated on .
I have finished chapter 11 and project 11 and do not understand the requirement of having this as argument 0 in method symbol table.
Whenever we access fields of the this object within a method, we do it using variables that are already present in the class symbol table.
Whenever we compile a method, we set the pointer 0 based on argument 0, so it is important to pass argument 0 from the caller but none of it requires having this in the symbol table of callee.
Only case I can think of to have this in symbol table of method is if this was used like an identifier within a method. What am I missing?

method subroutine symbol table "this"
Reply | Threaded
Open this post in threaded view
|

Re: Purpose of "this" as argument 0 in method subroutine symbol table

JerePengiuin
Hi Sam,
I think that this is the case of methods having two different "this" variables - the "this" that is (pointer 0), and used whenever the jack compiler encounters a "this".
Methods also have a "this-argument", (argument 0), which is used to set pointer 0, and never used again.
I think that "this" was included in the symbol table to reserve a space for arg 0, while also not conflicting with any other variable name the programmer might have chosen.

Hope this clears things up, keep it up!
Reply | Threaded
Open this post in threaded view
|

Re: Purpose of "this" as argument 0 in method subroutine symbol table

WBahn
Administrator
In reply to this post by sam11
I see your confusion and I think it is a case of the authors trying to give a complete picture, albeit not a really accurate one.

No identifier can be named 'this' because 'this' is a reserved word in Jack, which takes precedence over identifiers. Thus. having it in the symbol table serves no functional purpose (though it shouldn't cause problems).

I think, in that example, the authors were just trying to make it obvious what pieces of information were stored where.
Reply | Threaded
Open this post in threaded view
|

Re: Purpose of "this" as argument 0 in method subroutine symbol table

sam11
 thank you