Could need some help on 'this'

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

Could need some help on 'this'

Rather Iffy
I am testing my Jack Compiler on Square after passing the Seven and ConvertToBin tests.
I have a problem figuring out what 'this' means in the constructor SquareGame.new() and
in the method dispose().

The grammar says that 'this' is a KeywordConstant, so it is also a term and so it is also an expression.
But what does the expression 'this' mean?
In what sense can 'this' be interpreted as a constant on par with the other constants  'true' , 'false' or 'null' ?
And what is the relationship between the keyword constant 'this' and  the argument name 'this' that occurs in the
method-scope symbol table entries like :  " 'this' classname argument 0 "  ?

For easy reference i copy the first few lines of Square/SqaureGame.jack.
Could need some help on this.

--Chrit

class SquareGame {

    // The square
    field Square square;

    // The square's movement direction
    field int direction; // 0=none,1=up,2=down,3=left,4=right

    /** Constructs a new Square Game. */
    constructor SquareGame new() {
        let square = Square.new(0, 0, 30);
        let direction = 0;

        return this;
    }

    /** Deallocates the object's memory. */
    method void dispose() {
        do square.dispose();
        do Memory.deAlloc(this);
        return;
    }
    . . . . . . .

Reply | Threaded
Open this post in threaded view
|

Re: Could need some help on 'this'

ybakos
Have you ever done any programming in Java or, say, C++?

this is a keyword that represents a reference to the object itself.

The actual value of this changes depending on the scope within which it is used.
Reply | Threaded
Open this post in threaded view
|

Re: Could need some help on 'this'

cadet1620
Administrator
In reply to this post by Rather Iffy
Syntactically, this is a KeywordConstant like true, false, and null. Semantically, it means the current object pointer, rather than a numeric constant.

Since the this keyword is only allowed in methods, and the subroutine prologue for methods has already copied arg0 to THIS, all you need to do is push THIS onto the stack.

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

Re: Could need some help on 'this'

Rather Iffy
In reply to this post by Rather Iffy
The only possibility i can see  to refer in VM language to the location RAM[3] where the current object pointer is stored
 is by "pointer 0".

So in Python i would like to code your proposal " push THIS onto the stack" as:

class CompilationEngine():

#  Other methods not shown.

    def compile_keyword_constant(self):
       if self.match('KEYWORD',['THIS']):
            self.vmwriter.write_push('POINTER',0))
            self.move()

Is this a correct interpretation of your proposal?

And thanks for the very quick reactions.

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

Re: Could need some help on 'this'

ybakos
By the way, if it helps, you can think of Python's self as a corollary to Jack's this.
Reply | Threaded
Open this post in threaded view
|

Re: Could need some help on 'this'

cadet1620
Administrator
In reply to this post by Rather Iffy
Exactly right. FWIW, here's a snippet from my compiler:
        elif self.tokenizer.TokenType() == TK_KEYWORD and \
                self.tokenizer.Keyword() in (KW_FALSE, KW_NULL, KW_THIS, KW_TRUE):
            self._WriteXml('keyword', self.tokenizer.KeywordStr())

            if self.tokenizer.Keyword() == KW_THIS:
                self.vmWriter.WritePush(SEG_POINTER, 0)
            else:
                self.vmWriter.WritePush(SEG_CONST, 0)
                if self.tokenizer.Keyword() == KW_TRUE:
                    self.vmWriter.WriteArithmetic(OP_NOT)

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

Re: Could need some help on 'this'

jrd
Understood all above.

However, just to clarify on subroutine calls:

Whereas it's necessary to initially append subroutine scope symbol table with a "this" objectType ARG 0 for a Method call, it's not necessary to add any corresponding ARG 0 for either a Function or Constructor subroutine calls.  In those two cases (function/constructor), the ARG 0 index would begin with any actual parameters that are being passed into the subroutine function.  Doesn't make make the same sense with Functions or Constructors intuitively.

Is that correct pls?  Believe it is so, based on book text, but want to make sure that is the right way to handle.

Thx.

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

Re: Could need some help on 'this'

cadet1620
Administrator
That is correct.

Calling summary:
    // in class MyClass
    do Sys.error(x);       // function:  push x; call Sys.error 1
    let foo = Foo.new(x);  // constructor:  push x; call Foo.new 1
    do foo.bar(x);         // method:  push foo; push x; call Foo.bar 2
    do whatever(x);        // method:  push this; push x; call MyClass.whatever 2
--Mark