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