Differentiating between 'function' and 'method' calls

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

Differentiating between 'function' and 'method' calls

peterxu422
How do you tell whether a 'do' statement calls a subroutine that is a 'function' or a 'method' at compile time? I get stuck because there's the case when you have

do sub()

where if sub() were a method, implies that you need to push 'this' onto the stack. And if were a function you wouldn't need to do that. SymbolTable is no use since we don't store the subroutine identifiers in there.

I looked at a few .jack and their corresponding .vm files from the SampleGames and noticed that all of the projects with multiple classes had 'methods' as subroutines except for main(). Is it okay to assume that all the subroutines are methods and just push 'this' whenever they are called?
Reply | Threaded
Open this post in threaded view
|

Re: Differentiating between 'function' and 'method' calls

cadet1620
Administrator
peterxu422 wrote
How do you tell whether a 'do' statement calls a subroutine that is a 'function' or a 'method' at compile time? I get stuck because there's the case when you have

do sub()

where if sub() were a method, implies that you need to push 'this' onto the stack. And if were a function you wouldn't need to do that. SymbolTable is no use since we don't store the subroutine identifiers in there.

I looked at a few .jack and their corresponding .vm files from the SampleGames and noticed that all of the projects with multiple classes had 'methods' as subroutines except for main(). Is it okay to assume that all the subroutines are methods and just push 'this' whenever they are called?
See 9.2.6.

Methods are called using
    method(...), or
    variable.method(...)
The first version calls method passing the current object. Think this.method(...), except that Jack doesn't allow access to the this variable.

Functions and constructors are called using
    ClassName.function(...)

So, if it doesn't have a ".", this is a method call.
If it has a ".", you need to see if the symbol on the left is a variable. If it is, this is a method call.
Otherwise, this is function (or constructor) call.

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

Re: Differentiating between 'function' and 'method' calls

stansbuj
Thank you Mark! That was my exact problem.

Jack
Reply | Threaded
Open this post in threaded view
|

Re: Differentiating between 'function' and 'method' calls

Thomas Kwon
In reply to this post by cadet1620
wow~ thank you. your answer is really helpful to go on my project