Problem with Scope

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

Problem with Scope

sarthak
In Jack we have defined two scopes: class and subroutine. I do not understand the need of the subroutine scope. The way I implemented "call" in assembly is that whenever a call to function is made, it is replaced with the assembly code saving all the local variables of the original function on the stack and forming a new LCL for the called function.
If the local variables of the calling and called functions are saved in distinct locations why do we need scope, isn't it already taken care of by the assembly code?
Reply | Threaded
Open this post in threaded view
|

Re: Problem with Scope

ybakos
You're closer to understanding this than you think!

First, to clarify, a call does not need to explicitly save the caller's local variables on the stack - they should already be on top of the bookkeeping information (see the diagrams in the lecture slides for chapter 8).

In short, by changing the value of LCL, you are, in effect, changing the scope of the currently executing instructions. Once LCL is changed, then the local memory segment also changes. It is this local memory segment and the data on top of the working stack that constitutes the current scope.

Does that make sense?
Reply | Threaded
Open this post in threaded view
|

Re: Problem with Scope

sarthak
Yes it is making sense.
But if I took care of the bookkeeping by saving the information about the original function (its LCL etc.) in my assembly code. Do I need scope in Jack code?
Reply | Threaded
Open this post in threaded view
|

Re: Problem with Scope

cadet1620
Administrator
Class scope versus function scope is about variable names, and does not directly relate to function calling.  Consider:
class Foo {
    field int x;

    method int read_x() {
        return x;
    }

    method void set_x(int value) {
        let x = value;
        return;
    }

    method int bad_read_x() {
        var int x;    // Initialized to 0.
        return x;
    }

    method bad_set_x(int x) {
        let x = x;
        return;
    }
}
read_x() and set_x() methods do what you expect.

bad_read_x() always returns 0 because the 'return x' is referring to the local variable 'x' which hides the class variable 'x'.

bad_write_x() never changes the call variable 'x' because the argument 'x' hides the class variable; the 'x=x' sets the argument to itself.

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

Re: Problem with Scope

ybakos
sarthak, see Mark's example above for what is meant by scope (class scope and function scope).

The point is that, in order for a high-level language like Jack to have the concept of scope, the underlying architecture (virtual machine memory management, compiler symbol table, etc) must support it. In other words, the phenomena of scope in Jack manifests as the work you did in managing the book-keeping information in the stack frame (LCL, ARG, etc).

In other words, you are probably done handling scope.