HYPE - a simple OOF implementation

Helge Horch heho@gmx.de

May 1998

[URL: http://home.munich.netsurf.de/Helge.Horch/hype.html DATE: 06jan99] NOTE: THIS WAS WORK IN PROGRESS FOR QUITEAWHILE. DON'T EXPECT TOO MUCH ANYMORE.

What?

Why now?

has object-message syntax,

is a parsing implementation (objects parse selectors),

uses WORDLIST s for its implementation (not vtables).

Why at all?

Major differences to Pountain's model are:

HYPE uses SEARCH-WORDLIST instead of patching link fields. Existing classes can be extended later (by using METHODS ). Subclasses inherit instance variables, although they must be accessed with SUPER. Instance variables are public now (data hiding was of no great use to me in Forth anyway...) No extra object stack, which was synchronous with the return stack anyway.

What do I need?

from the CORE EXTENSION word set

VALUE TO 0> 2>R 2R@ 2R> COMPILE,

from the SEARCH ORDER word set

WORDLIST SEARCH-WORDLIST DEFINITIONS SET-CURRENT GET-ORDER SET-ORDER

from the SEARCH ORDER EXTENSION word set

PREVIOUS

How much?

64 C/L

: LIT, ( x) POSTPONE LITERAL ; : >SIZE ( ta - n) CELL+ @ ; 0 VALUE SELF : SELF+ ( n - a) SELF + ; : SEND ( a xt) SELF >R SWAP TO SELF EXECUTE R> TO SELF ; VARIABLE CLS ( contains ta) : SIZE^ ( - aa) CLS @ ?DUP 0= ABORT" scope?" CELL+ ; : MFIND ( ta ca u - xt n) 2>R BEGIN DUP WHILE DUP @ 2R@ ROT SEARCH-WORDLIST ?DUP IF ROT DROP 2R> 2DROP EXIT THEN CELL+ CELL+ @ REPEAT -1 ABORT" can't?" ; : SEND' ( a ta "m ") BL WORD COUNT MFIND 0< STATE @ AND IF SWAP LIT, LIT, POSTPONE SEND ELSE SEND THEN ; : SUPER ( "m ") SIZE^ CELL+ @ BL WORD COUNT MFIND 0> IF EXECUTE ELSE COMPILE, THEN ; IMMEDIATE : DEFS ( n "f ") CREATE SIZE^ @ , SIZE^ +! IMMEDIATE DOES> @ STATE @ IF LIT, POSTPONE SELF+ ELSE SELF+ THEN ; : METHODS ( ta) DUP CLS ! @ DUP SET-CURRENT >R GET-ORDER R> SWAP 1+ SET-ORDER ; ( ALSO CONTEXT !) : CLASS ( "c ") CREATE HERE 0 , 0 , 0 , WORDLIST OVER ! METHODS ; : SUBCLASS ( ta "c ") CLASS SIZE^ OVER >SIZE OVER ! CELL+ ! ; : END ( ) SIZE^ DROP PREVIOUS DEFINITIONS 0 CLS ! ; : NEW ( ta "name ") CREATE DUP , >SIZE ALLOT IMMEDIATE DOES> DUP CELL+ SWAP @ SEND' ;

ta the address of a class (formerly "type"), ta -> | wid | size | super-ta | oa the address of an object (instance), oa -> | ta | ... | "c " a class name follows the word "f " a field name (instance variable name) follows the word "m " an existing method name (selector) follows the word

It is small enough to be understandable. (Is it?)

It treats instance variables very much like "normal" variables.

Methods are just normal forth words. They can be colon definitions, code definitions, constants, whatever.

The words SELF , SELF+ and SEND' are easily replaced by code words for a particular implementation. For example, in F/X and PoFo on the Macintosh, I used a register for SELF .

, and are easily replaced by code words for a particular implementation. For example, in F/X and PoFo on the Macintosh, I used a register for . It reads like Smalltalk. I like that, I'm used to it.

What does it look like?

1 CELLS CONSTANT CELL CLASS BUTTON CELL DEFS TEXT CELL DEFS LEN CELL DEFS X CELL DEFS Y : DRAW ( ) X @ Y @ AT-XY TEXT @ LEN @ TYPE ; : INIT ( ca u) 0 X ! 0 Y ! LEN ! TEXT ! ; END : BOLD 27 EMIT ." [1m" ; : NORMAL 27 EMIT ." [0m" ; BUTTON SUBCLASS BOLD-BUTTON : DRAW ( ) BOLD SUPER DRAW NORMAL ; END BUTTON NEW FOO S" thin foo" FOO INIT PAGE FOO DRAW BOLD-BUTTON NEW BAR S" fat bar" BAR INIT 1 BAR Y ! BAR DRAW

: VAR 1 CELLS DEFS ;

: IV ( ta "name ") DUP >SIZE DEFS , DOES> 2@ SELF+ SWAP SEND' ;

CLASS Foo Button IV btn1 Button IV btn2 END

: REF ( ta "name ") VAR , DOES> 2@ SELF+ @ SWAP SEND' ;

CLASS Link 0 DEFS 'next Link REF next END

'next

(I always wanted to say that sometime)

Also, for special cases, one might want to factor NEW into

: INSTANCE ( ta) DUP , >SIZE ALLOT DOES> DUP CELL+ SWAP @ SEND' ; : NEW ( ta "name ") CREATE INSTANCE IMMEDIATE ;

Now what?

Is it really ANS compliant?

I don't know. I ran the example code on gforth under NT. My favourite, ZENForth, itself not being fully ANS-compliant, needs a different version due to its ROM/RAM model.

I don't know. I ran the example code on gforth under NT. My favourite, ZENForth, itself not being fully ANS-compliant, needs a different version due to its ROM/RAM model. Do you see a way of making it shorter?

...without sacrificing too much (or any) of its functionality, of course. Hacking Forth has degraded into a part-time hobby for me, so I'm not spending too much time on it.

Appendix

>SIZE

3

0>

:-)

: LIT, ( x) POSTPONE LITERAL ; 0 VALUE SELF : SELF+ ( n - a) SELF + ; : SEND ( a xt) SELF >R SWAP TO SELF EXECUTE R> TO SELF ; VARIABLE CLS ( contains ta -> |size|wid|super|) : SIZE^ ( - aa) CLS @ ?DUP 0= ABORT" scope?" ; : MFIND ( ta ca u - xt n) 2>R BEGIN DUP WHILE CELL+ DUP @ 2R@ ROT SEARCH-WORDLIST ?DUP IF ROT DROP 2R> 2DROP EXIT THEN CELL+ @ REPEAT -1 ABORT" can't?" ; : SEND' ( a ta "m ") BL WORD COUNT MFIND 0< STATE @ AND IF SWAP LIT, LIT, POSTPONE SEND ELSE SEND THEN ; : SUPER ( "m ") SIZE^ CELL+ CELL+ @ BL WORD COUNT MFIND 0< IF COMPILE, ELSE EXECUTE THEN ; IMMEDIATE : DEFS ( n "f ") CREATE SIZE^ @ , SIZE^ +! IMMEDIATE DOES> @ STATE @ IF LIT, POSTPONE SELF+ ELSE SELF+ THEN ; : METHODS ( ta) DUP CLS ! CELL+ @ DUP SET-CURRENT >R GET-ORDER R> SWAP 1+ SET-ORDER ; ( ALSO CONTEXT !) : CLASS ( "c ") CREATE HERE 0 , 0 , 0 , WORDLIST OVER CELL+ ! METHODS ; : SUBCLASS ( ta "c ") CLASS SIZE^ OVER @ OVER ! CELL+ CELL+ ! ; : END ( ) SIZE^ DROP PREVIOUS DEFINITIONS 0 CLS ! ; : NEW ( ta "name ") CREATE DUP , @ ALLOT IMMEDIATE DOES> DUP CELL+ SWAP @ SEND' ;