Thoughts on class inheritance and method overriding?

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

Thoughts on class inheritance and method overriding?

tungsten
Anyone have ideas on how class inheritance and method overriding can be accomplished in Jack?

Also of any resources can look at/read for ideas?
Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on class inheritance and method overriding?

cadet1620
Administrator
Disclaimer: I've never written an object oriented compiler, but I've debugged a lot of asm code generated by them!

My compiler books are too old to deal with OO Languages.  I found a copy of "Modern Compiler Implementation in Java", 2e online and it addresses OO issues in its final chapter. It's rather deep.
 

The fist thing that your compiler will need to do is have complete symbol information for all parent objects (and their parents...) so that it will know what subroutines to call. Consider Foo.jack that implements class Foo(Bar) [stealing Python syntax].

A method in Main has
    Foo f;
    do f.fun();
In jack this calls Foo.fun with f as the this parameter. In Jack++, if Foo did not define fun(), it needs to call Bar.fun(), again with f as the this parameter. (Bar might not have fun() either; it my be in its partent class.)

The fact that Bar.fun() can be called with a Foo object or a Bar object dictates that the field layout in the object will be Bar's fields followed by Foo's fields.
    aFoo -> Bar field 0
            Bar field 1
            Bar field 2
            Foo field 0
            ...
The constructor will need to know the size of its parent(s) so that it can properly allote its object and assign appropriate offsets fo it's fields.

The easiest way to do this is to require import or uses commands that name classes before they used. This would cause the compiler to parse that class's Jack file to build the required symbol tables. You would need to handle/limit recursive imports.


Hmm, not sure how constructors will work.  Jack constructors allocate and initialize fields.  A child class's constructor must allocate an object big enough to hold all the subclasses and then call the parent constructor to initialize its fields.  C++ handles this by having the CALLING function allocate the memory and pass that new block into the constructor as this.

Enough to think about for now.


Dynamic functions (virtual functions in C++) require function pointer tables as part of the instance data which is not something that the VM language supports.

Multiple inheritance makes object field layout a nightmare.

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

Re: Thoughts on class inheritance and method overriding?

tungsten
Thanks Mark! Doesn't look like it will be straightforward =/
**Adds to long term goals (current know-how is inadequate)

cadet1620 wrote
My compiler books are too old to deal with OO Languages.  I found a copy of "Modern Compiler Implementation in Java", 2e online and it addresses OO issues in its final chapter.
Got a copy and will read the chapter. Didn't know such books existed =)
Reply | Threaded
Open this post in threaded view
|

Re: Thoughts on class inheritance and method overriding?

cadet1620
Administrator
tungsten wrote
Got a copy and will read the chapter. Didn't know such books existed =)
Welcome to the world of academic computer science books...

If you are interested in the low-level implementation of things like the collection types and sorting utilities that come standard in higher level languages these days, you might want to take a look at
    "Fundamentals of Data Structures" by Horowitz and Sahni (my book from school), or,
    "Algorithms in C" by Sedgewick.
I just found this free (creative commons) book that looks pretty good.
    http://opendatastructures.org/

--Mark