--- gforth/objects.fs 1999/06/17 15:32:13 1.13 +++ gforth/objects.fs 1999/07/24 13:07:19 1.17 @@ -1,6 +1,6 @@ \ yet another Forth objects extension -\ written by Anton Ertl 1996-1998 +\ written by Anton Ertl 1996-1999 \ public domain; NO WARRANTY \ This (in combination with compat/struct.fs) is in ANS Forth (with an @@ -263,6 +263,12 @@ variable public-wordlist \g Add @var{class}'s wordlists to the head of the search-order. >r get-order r> add-class-order set-order ; +: methods ( class -- ) \ objects- objects + \g Makes @var{class} the current class. This is intended to be + \g used for defining methods to override selectors; you cannot + \g define new fields or selectors. + dup current-interface ! push-order ; + : class ( parent-class -- align offset ) \ objects- objects \g Start a new class definition as a child of \g @var{parent-class}. @var{align offset} are for use by @@ -274,8 +280,7 @@ variable public-wordlist 0 r@ interface-offset ! dup r@ class-parent ! wordlist r@ class-wordlist ! - r@ current-interface ! - r> push-order + r> methods class-inst-size 2@ ; : remove-class-order ( wid1 ... widn n+n1 class -- n1 ) @@ -292,10 +297,15 @@ variable public-wordlist \g on the search order. >r get-order r> remove-class-order set-order ; +: end-methods ( -- ) \ objects- objects + \g Switch back from defining methods of a class to normal mode + \g (currently this just restores the old search order). + current-interface @ drop-order ; + : end-class-noname ( align offset -- class ) \ objects- objects \g End a class definition. The resulting class is @var{class}. - public - current-interface @ dup drop-order class-inst-size 2! + public end-methods + current-interface @ class-inst-size 2! end-interface-noname ; : end-class ( align offset "name" -- ) \ objects- objects @@ -349,12 +359,20 @@ variable public-wordlist \ : to-this ( object -- ) \ thisp ! ; -: m: ( -- xt colon-sys; run-time: object -- ) \ objects- objects - \g Start a method definition; @var{object} becomes new @code{this}. - :noname +: enterm ( -- ; run-time: object -- ) + \g method prologue; @var{object} becomes new @code{this}. POSTPONE this POSTPONE >r POSTPONE to-this ; + +: m: ( -- xt colon-sys; run-time: object -- ) \ objects- objects + \g Start a method definition; @var{object} becomes new @code{this}. + :noname enterm ; + +: :m ( "name" -- xt; run-time: object -- ) \ objects- objects + \g Start a named method definition; @var{object} becomes new + \g @code{this}. Has to be ended with @code{;m}. + : enterm ; : exitm ( -- ) \ objects- objects \g @code{exit} from a method; restore old @code{this}. @@ -476,6 +494,8 @@ method print ( object -- ) \ objects- ob \g Print the object. The method for the class @var{object} prints \g the address of the object and the address of its class. +selector equal ( object1 object2 -- flag ) + end-class object ( -- class ) \ objects- objects \g the ancestor of all classes.