[ColorForth] immediate
- Subject: [ColorForth] immediate
- From: "Michael A. Losh" <forthist@xxxxxxxx>
- Date: Wed, 13 Jun 2001 22:49:21 -0400
----- Original Message -----
From: "Dirk Heise" <dheise@xxxxxxxxxxx>
To: "ColorForth List Member" <ColorForth@xxxxxxxxxxxxxxxxxx>
Sent: Tuesday, June 12, 2001 3:11 PM
Subject: [ColorForth] immediate
...
> I'll code an OO extension. And i haven't decided yet
> about where to leave the "this" pointer (the pointer
> to the object a method has to deal with). Are there
> experiences? Shall i
> - put it on the data stack?
> - on the r-stack?
> - on a dedicated "this"-stack?
> - in a global variable?
>
> At the moment, i tend to use the last variant...
I can share a few opinions on this. If you want to have an object's method
functions call the methods of other objects, you have a problem if there is
only a single global "this" pointer. Therefore, it makes a lot of sense to
store it in some stack. Using the parameter stack might tend to clutter
things up. I can see using the return stack, as long as you restrain from
using the return stack for other temporary variables. Perhaps most elegant
is a dedicated "this" stack for object contexts (not to be confused with
voculary context... although some Forth OO implementations use vocabularies
to enforce information hiding for objects).
You can set the current object context by placing the object's "this"
pointer on the parameter stack, followed by calling an object method The
special entry code for nesting into a method would push the "this" pointer
to the object context stack. The complementary "unnest" code would either
pop the object context stack, or drop the current "this" pointer, depending
on whether you like to chain several method calls tothether.
Of course in color forth, you would use distinct "color" for class names,
member names, method names, and so on. But for simplicity in email, here is
a sample syntax:
class{ MyClass
cell member: TheMax ( -- a)
cell member: TheMin ( -- a)
8 cells member: LittleArray ( -- a)
method: Max@ ( -- n ) TheMax @ ;method
method: Max! ( n -- ) TheMax ! ;method
method: Min! ( n -- ) TheMin ! ;method
method: Spread ( -- n ) TheMax @ TheMin @ - ;method
method: into-LittleArray ( -- a) cells + ;method
}class
\ Sample with object context lost on each method call
MyClass object foo
23 foo Min! 37 foo Max! foo Spread .
14 OK
18 2 foo into-LittleArray !
OK
2 foo into-LittleArray @ .
18 OK
\ Sample with object context saved until the next unnest
MyClass object foo
foo -> 23 Min! 37 Max! Spread .
14 OK
foo -> 18 2 into-LittleArray !
OK
foo -> 2 into-LittleArray @ .
18 OK
member: is a defining word that reserves space in the object's data
area, and when used in a method call, offsets the appropriate amount from
the "this pointer, leaving the specific address on the parameter stack.
method: is a defining word that compiles a method nesting routine. The
method nesting routine may either:
1) assume the "this" pointer is on the parameter stack, and then push it to
the object context stack
or 2) nest like a normal colon word, assuming the "this" pointer is already
where it should be (see below)
;method is a compiling word that compiles a method unnest routine. It can
pop the object context stack, or leave it alone.
-> is a helper word for alternative 2), which pushes the "this" pointer
onto the object context stack. Perhaps returning to the outer interpreter
(OK prompt) clears the entire contents of the object context stack. Perhaps
you can define a word to explicitly pop or drop its items one at a time.
Hope this gives you some ideas. I have seen a lot about C++ and COM
objects, and also have reviewed many object-oriented approaches in Forth.
If I remember correctly, Win32Forth's OO mechanisms are similar to what I
described above. Hope this note is food for thought!
--
Mike Losh http://members.home.net/forthist/
------------------------
To Unsubscribe from this list, send mail to Mdaemon@xxxxxxxxxxxxxxxxxx with:
unsubscribe ColorForth
as the first and only line within the message body
Problems - List-Admin@xxxxxxxxxxxxxxxxxx
Main ColorForth site - http://www.ultratechnology.com