How to Compile Subroutine Calls eli5

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

How to Compile Subroutine Calls eli5

garamor
I have been struggling over compiling subroutine calls for a while now, and specifically figuring out whether or not the callee is a method. The challenge is that if I am compiling a jack program sequentially and I encounter a call to a subroutine that I have not yet come across, there does not seem to be any way to know what type of subroutine it is. Does anyone have any idea how I might be able to implement this in python? (eli5)
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

dolomiti7
The logic in the Jack language is as follows:
1. Static function call:
    Classname.function()
2. Constructor call:
    // For the call it makes no difference whether a static function or a constructor is called
    // The difference is just that a constructor will return a reference to the created object
    // Consequently the syntax is the same as for a static function call:
    Classname.constructor()
3. Method call
    objectVariable.method()
4. Method call to current object
    method() // this refers implicitly to "this"

Therefore you can distinguish the type of call directly from the Jack syntax:
 - is the call referring to a known variable?
 -> method call; derive class name from the type of the variable (lookup symbol table)
 - does the call not contain a class or variable?
 -> method call referring to own object (this)
 - otherwise the call must be referring to a class
 -> static call or constructor call

Hope that helps
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

garamor
ohhh, that make a lot of sense. I think my confusion was in part due to the fact that I thought you could also call constructors and static functions by referring to an instance of a class (objectVariable.constructer() or objectVariable.function()). Although now that I think about it, I doesn't seem there would be any practical value in this. Anyways, thanks so much. This helped a lot!
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

garamor
In reply to this post by dolomiti7
I have just encountered one other issue actually. What if you are calling a subroutine within your current class? The syntax for that is subroutineName(). The strategy you just showed me does not seem to work in this case because you are not referencing a class or object variable. How could I determine if the callee is a method in this case?
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

dolomiti7
garamor wrote
I have just encountered one other issue actually. What if you are calling a subroutine within your current class? The syntax for that is subroutineName(). The strategy you just showed me does not seem to work in this case because you are not referencing a class or object variable. How could I determine if the callee is a method in this case?
This refers to the following case
- does the call not contain a class or variable?
 -> method call referring to own object (this)

So if the call is just containing a method name, it is a call to a (class)-local method. It cannot be a call to a static function: Jack requires that static calls are always using the full Classname.function() format, even if it is a call to a static function within the same class.
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

garamor
Is the full class name format required for constructors too?
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

dolomiti7
Yes. See this excerpt from the book (1st edition), chapter 9.2.6:
"Within a class, methods are called using the syntax methodName(argumentlist),
while functions and constructors must be called using their full names, namely,
className.subroutineName(argument-list). Outside a class, the class functions and
constructors are also called using their full names, while methods are called using the
syntax varName.methodName(argument-list), where varName is a previously defined
object variable."
Reply | Threaded
Open this post in threaded view
|

Re: How to Compile Subroutine Calls eli5

garamor
Thanks so much this has all been vary helpful! I just got my compiler to successfully compile it's first program! Hooray!

Now it's time to bug test it. T-T