1: \ oof.fs Object Oriented FORTH
2: \ This file is (c) 1996 by Bernd Paysan
3: \ e-mail: paysan@informatik.tu-muenchen.de
4: \
5: \ Please copy and share this program, modify it for your system
6: \ and improve it as you like. But don't remove this notice.
7: \
8: \ Thank you.
9: \
10: \ The program uses the following words
11: \ from CORE :
12: \ decimal : bl word 0= ; = cells Constant Variable ! Create , allot @ IF
13: \ POSTPONE >r ELSE +! dup + THEN immediate r> * >body cell+ Literal drop
14: \ align here aligned DOES> execute ['] 2@ recurse swap 1+ over LOOP and
15: \ EXIT ?dup 0< rot r@ - i negate +LOOP 2drop BEGIN WHILE 2dup REPEAT 1-
16: \ rshift > / ' move UNTIL or count
17: \ from CORE-EXT :
18: \ nip tuck true ?DO compile, false Value erase pick :noname 0<>
19: \ from BLOCK-EXT :
20: \ \
21: \ from EXCEPTION :
22: \ throw
23: \ from EXCEPTION-EXT :
24: \ abort"
25: \ from FILE :
26: \ ( S"
27: \ from FLOAT :
28: \ faligned
29: \ from LOCAL :
30: \ TO
31: \ from MEMORY :
32: \ allocate free
33: \ from SEARCH :
34: \ find definitions get-order set-order get-current wordlist set-current
35: \ search-wordlist
36: \ from SEARCH-EXT :
37: \ also Forth previous
38: \ from STRING :
39: \ /string compare
40: \ from TOOLS-EXT :
41: \ [IF] [THEN] [ELSE] state
42:
43: \ Loadscreen 27dec95py
44:
45: decimal
46:
47: : define? ( -- flag )
48: bl word find nip 0= ;
49:
50: define? cell [IF]
51: 1 cells Constant cell
52: [THEN]
53:
54: define? ?EXIT [IF]
55: : ?EXIT postpone IF postpone EXIT postpone THEN ; immediate
56: [THEN]
57:
58: define? Vocabulary [IF]
59: : Vocabulary wordlist create ,
60: DOES> @ >r get-order nip r> swap set-order ;
61: [THEN]
62:
63: define? faligned [IF] false [ELSE] 1 faligned 8 = [THEN]
64: [IF]
65: : 8aligned ( n1 -- n2 ) faligned ;
66: [ELSE]
67: : 8aligned ( n1 -- n2 ) 7 + -8 and ;
68: [THEN]
69:
70: Vocabulary Objects also Objects also definitions
71:
72: Vocabulary types types also
73:
74: 0 cells Constant :wordlist
75: 1 cells Constant :parent
76: 2 cells Constant :child
77: 3 cells Constant :next
78: 4 cells Constant :method#
79: 5 cells Constant :var#
80: 6 cells Constant :newlink
81: 7 cells Constant :iface
82: 8 cells Constant :init
83:
84: 0 cells Constant :inext
85: 1 cells Constant :ilist
86: 2 cells Constant :ilen
87: 3 cells Constant :inum
88:
89: Variable op
90: : op! ( o -- ) op ! ;
91:
92: Forth definitions
93:
94: Create ostack 0 , 16 cells allot
95:
96: : ^ ( -- o ) op @ ;
97: : o@ ( -- o ) op @ @ ;
98: : >o ( o -- )
99: state @
100: IF postpone ^ postpone >r postpone op!
101: ELSE 1 ostack +! ^ ostack dup @ cells + ! op!
102: THEN ; immediate
103: : o> ( -- )
104: state @
105: IF postpone r> postpone op!
106: ELSE ostack dup @ cells + @ op! -1 ostack +!
107: THEN ; immediate
108: : o[] ( n -- ) o@ :var# + @ * ^ + op! ;
109:
110: Objects definitions
111:
112: \ Coding 27dec95py
113:
114: 0 Constant #static
115: 1 Constant #method
116: 2 Constant #early
117: 3 Constant #var
118: 4 Constant #defer
119:
120: : exec? ( addr -- flag )
121: >body cell+ @ #method = ;
122: : static? ( addr -- flag )
123: >body cell+ @ #static = ;
124: : early? ( addr -- flag )
125: >body cell+ @ #early = ;
126: : defer? ( addr -- flag )
127: >body cell+ @ #defer = ;
128:
129: false Value oset?
130:
131: : o+, ( addr offset -- )
132: postpone Literal postpone ^ postpone +
133: oset? IF postpone op! ELSE postpone >o THEN drop ;
134: : o*, ( addr offset -- )
135: postpone Literal postpone * postpone Literal postpone +
136: oset? IF postpone op! ELSE postpone >o THEN ;
137: : ^+@ ( offset -- addr ) ^ + @ ;
138: : o+@, ( addr offset -- )
139: postpone Literal postpone ^+@ oset? IF postpone op! ELSE postpone >o THEN drop ;
140: : ^*@ ( offset -- addr ) ^ + @ tuck @ :var# + @ 8aligned * + ;
141: : o+@*, ( addr offset -- )
142: postpone Literal postpone ^*@ oset? IF postpone op! ELSE postpone >o THEN drop ;
143:
144: \ variables / memory allocation 30oct94py
145:
146: Variable lastob
147: Variable lastparent 0 lastparent !
148: Variable vars
149: Variable methods
150: Variable decl 0 decl !
151: Variable 'link
152:
153: : crash true abort" unbound method" ;
154:
155: : link, ( addr -- ) align here 'link ! , 0 , 0 , ;
156:
157: 0 link,
158:
159: \ type declaration 30oct94py
160:
161: : vallot ( size -- offset ) vars @ >r dup vars +!
162: 'link @ 0=
163: IF lastparent @ dup IF :newlink + @ THEN link,
164: THEN
165: 'link @ 2 cells + +! r> ;
166:
167: : valign ( -- ) vars @ aligned vars ! ;
168: define? faligned 0= [IF]
169: : vfalign ( -- ) vars @ faligned vars ! ;
170: [THEN]
171:
172: : mallot ( -- offset ) methods @ cell methods +! ;
173:
174: types definitions
175:
176: : static ( -- ) mallot Create , #static ,
177: DOES> @ o@ + ;
178: : method ( -- ) mallot Create , #method ,
179: DOES> @ o@ + @ execute ;
180: : early ( -- ) Create ['] crash , #early ,
181: DOES> @ execute ;
182: : var ( size -- ) vallot Create , #var ,
183: DOES> @ ^ + ;
184: : defer ( -- ) valign cell vallot Create , #defer ,
185: DOES> @ ^ + @ execute ;
186:
187: \ dealing with threads 29oct94py
188:
189: Objects definitions
190:
191: : object-order ( wid0 .. widm m addr -- wid0 .. widn n )
192: dup IF 2@ >r recurse r> swap 1+ ELSE drop THEN ;
193:
194: : interface-order ( wid0 .. widm m addr -- wid0 .. widn n )
195: dup IF 2@ >r recurse r> :ilist + @ swap 1+
196: ELSE drop THEN ;
197:
198: : add-order ( addr -- n ) dup 0= ?EXIT >r
199: get-order r> swap >r 0 swap
200: dup >r object-order r> :iface + @ interface-order
201: r> over >r + set-order r> ;
202:
203: : drop-order ( n -- ) 0 ?DO previous LOOP ;
204:
205: \ object compiling/executing 20feb95py
206:
207: : o, ( xt early? -- )
208: over exec? over and IF
209: drop >body @ o@ + @ compile, EXIT THEN
210: over static? over and IF
211: drop >body @ o@ + @ postpone Literal EXIT THEN
212: drop dup early? IF >body @ THEN compile, ;
213:
214: : findo ( string -- cfa n )
215: o@ add-order >r
216: find
217: ?dup 0= IF drop set-order true abort" method not found!" THEN
218: r> drop-order ;
219:
220: false Value method?
221:
222: : method, ( object early? -- ) true to method?
223: swap >o >r bl word findo 0< state @ and
224: IF r> o, ELSE r> drop execute THEN o> false to method? ;
225:
226: : cmethod, ( object early? -- )
227: state @ >r state on method, r> state ! ;
228:
229: : early, ( object -- ) true to oset? true method,
230: state @ oset? and IF postpone o> THEN false to oset? ;
231: : late, ( object -- ) true to oset? false method,
232: state @ oset? and IF postpone o> THEN false to oset? ;
233:
234: \ new, 29oct94py
235:
236: previous Objects definitions
237:
238: Variable alloc
239: 0 Value ohere
240:
241: : oallot ( n -- ) ohere + to ohere ;
242:
243: : ((new, ( link -- )
244: dup @ ?dup IF recurse THEN cell+ 2@ swap ohere + >r
245: ?dup IF ohere >r dup >r :newlink + @ recurse r> r> ! THEN
246: r> to ohere ;
247:
248: : (new ( object -- )
249: ohere >r dup >r :newlink + @ ((new, r> r> ! ;
250:
251: : init-instance ( pos link -- pos )
252: dup >r @ ?dup IF recurse THEN r> cell+ 2@
253: IF drop dup >r ^ +
254: >o o@ :init + @ execute 0 o@ :newlink + @ recurse o>
255: r> THEN + ;
256:
257: : init-object ( object -- size )
258: >o o@ :init + @ execute 0 o@ :newlink + @ init-instance o> ;
259:
260: : (new, ( object -- ) ohere dup >r over :var# + @ erase (new
261: r> init-object drop ;
262:
263: : size@ ( objc -- size ) :var# + @ 8aligned ;
264: : (new[], ( n o -- addr ) ohere >r
265: dup size@ rot over * oallot r@ ohere dup >r 2 pick -
266: ?DO I to ohere >r dup >r (new, r> r> dup negate +LOOP
267: 2drop r> to ohere r> ;
268:
269: \ new, 29oct94py
270:
271: Create chunks here 16 cells dup allot erase
272:
273: : DelFix ( addr root -- ) dup @ 2 pick ! ! ;
274:
275: : NewFix ( root size # -- addr )
276: BEGIN 2 pick @ ?dup 0=
277: WHILE 2dup * allocate throw over 0
278: ?DO dup 4 pick DelFix 2 pick +
279: LOOP
280: drop
281: REPEAT
282: >r drop r@ @ rot ! r@ swap erase r> ;
283:
284: : >chunk ( n -- root n' )
285: 1- -8 and dup 3 rshift cells chunks + swap 8 + ;
286:
287: : Dalloc ( size -- addr )
288: dup 128 > IF allocate throw EXIT THEN
289: >chunk 2048 over / NewFix ;
290:
291: : Salloc ( size -- addr ) align here swap allot ;
292:
293: : dispose, ( addr size -- )
294: dup 128 > IF drop free throw EXIT THEN
295: >chunk drop DelFix ;
296:
297: : new, ( o -- addr ) dup :var# + @
298: alloc @ execute dup >r to ohere (new, r> ;
299:
300: : new[], ( n o -- addr ) dup :var# + @ 8aligned
301: 2 pick * alloc @ execute to ohere (new[], ;
302:
303: Forth definitions
304:
305: : dynamic ['] Dalloc alloc ! ; dynamic
306: : static ['] Salloc alloc ! ;
307:
308: Objects definitions
309:
310: \ instance creation 29mar94py
311:
312: : instance, ( o -- ) alloc @ >r static new, r> alloc ! drop
313: DOES> state @ IF dup postpone Literal oset? IF postpone op! ELSE postpone >o THEN THEN early, ;
314: : ptr, ( o -- ) 0 , ,
315: DOES> state @
316: IF dup postpone Literal postpone @ oset? IF postpone op! ELSE postpone >o THEN cell+
317: ELSE @ THEN late, ;
318:
319: : array, ( n o -- ) alloc @ >r static new[], r> alloc ! drop
320: DOES> ( n -- ) dup dup @ size@
321: state @ IF o*, ELSE nip rot * + THEN early, ;
322:
323: \ class creation 29mar94py
324:
325: Variable voc#
326: Variable classlist
327: Variable old-current
328: Variable ob-interface
329:
330: : voc! ( addr -- ) get-current old-current !
331: add-order 2 + voc# !
332: get-order wordlist tuck classlist ! 1+ set-order
333: also types classlist @ set-current ;
334:
335: : (class ( parent -- )
336: here lastob ! true decl ! 0 ob-interface !
337: 0 , dup voc! dup lastparent !
338: dup 0= IF 0 ELSE :method# + 2@ THEN methods ! vars !
339: DOES> false method, ;
340:
341: : (is ( addr -- ) bl word findo drop
342: dup defer? abort" not deferred!"
343: >body @ state @
344: IF postpone ^ postpone Literal postpone + postpone !
345: ELSE ^ + ! THEN ;
346:
347: : inherit ( -- ) bl word findo drop
348: dup exec? IF >body @ dup o@ + @ swap lastob @ + ! EXIT THEN
349: abort" Not a polymorph method!" ;
350:
351: \ instance variables inside objects 27dec93py
352:
353: : instvar, ( addr -- ) dup , here 0 , 0 vallot swap !
354: 'link @ 2 cells + @ IF 'link @ link, THEN
355: 'link @ >r dup r@ cell+ ! :var# + @ dup vars +! r> 2 cells + !
356: DOES> dup 2@ swap state @ IF o+, ELSE ^ + nip nip THEN
357: early, ;
358:
359: : instptr> ( -- ) DOES> dup 2@ swap
360: state @ IF o+@, ELSE ^ + @ nip nip THEN late, ;
361:
362: : instptr, ( addr -- ) , here 0 , cell vallot swap !
363: instptr> ;
364:
365: : (o* ( i addr -- addr' ) dup @ :var# + @ 8aligned rot * + ;
366:
367: : instarray, ( addr -- ) , here 0 , cell vallot swap !
368: DOES> dup 2@ swap
369: state @ IF o+@*, ELSE ^ + @ nip nip (o* THEN
370: late, ;
371:
372: \ bind instance pointers 27mar94py
373:
374: : ((link ( addr -- o addr' ) 2@ swap ^ + ;
375:
376: : (link ( -- o addr ) bl word findo drop >body state @
377: IF postpone Literal postpone ((link EXIT THEN ((link ;
378:
379: : parent? ( class o -- class class' ) @
380: BEGIN 2dup = ?EXIT dup WHILE :parent + @ REPEAT ;
381:
382: : (bound ( obj1 obj2 adr2 -- ) >r over parent?
383: nip 0= abort" not the same class !" r> ! ;
384:
385: : (bind ( addr -- ) \ <name>
386: (link state @ IF postpone (bound EXIT THEN (bound ;
387:
388: : (sbound ( o addr -- ) dup cell+ @ swap (bound ;
389:
390: Forth definitions
391:
392: : bind ( o -- ) ' state @
393: IF postpone Literal postpone >body postpone (sbound EXIT THEN
394: >body (sbound ; immediate
395:
396: Objects definitions
397:
398: \ method implementation 29oct94py
399:
400: Variable m-name
401: Variable last-interface 0 last-interface !
402:
403: : interface, ( -- ) last-interface @
404: BEGIN dup WHILE dup , @ REPEAT drop ;
405:
406: : inter, ( iface -- )
407: align here over :inum + @ lastob @ + !
408: here over :ilen + @ dup allot move ;
409:
410: : interfaces, ( -- ) ob-interface @ lastob @ :iface + !
411: ob-interface @
412: BEGIN dup WHILE 2@ inter, REPEAT drop ;
413:
414: : lastob! ( -- ) lastob @ dup
415: BEGIN nip dup @ here cell+ 2 pick ! dup 0= UNTIL drop
416: dup , op! o@ lastob ! ;
417:
418: : thread, ( -- ) classlist @ , ;
419: : var, ( -- ) methods @ , vars @ , ;
420: : parent, ( -- o parent )
421: o@ lastparent @ 2dup dup , 0 ,
422: dup IF :child + dup @ , ! ELSE , drop THEN ;
423: : 'link, ( -- )
424: 'link @ ?dup 0=
425: IF lastparent @ dup IF :newlink + @ THEN THEN , ;
426: : cells, ( -- )
427: methods @ :init ?DO ['] crash , cell +LOOP ;
428:
429: \ method implementation 20feb95py
430:
431: types definitions
432:
433: : how: ( -- ) decl @ 0= abort" not twice!" 0 decl !
434: align interface,
435: lastob! thread, parent, var, 'link, 0 , cells, interfaces,
436: dup
437: IF dup :method# + @ >r :init + swap r> :init /string move
438: ELSE 2drop THEN ;
439:
440: : class; ( -- ) decl @ IF how: THEN 0 'link !
441: voc# @ drop-order old-current @ set-current ;
442:
443: : ptr ( -- ) Create immediate lastob @ here lastob ! instptr, ;
444: : asptr ( addr -- ) cell+ @ Create immediate
445: lastob @ here lastob ! , , instptr> ;
446:
447: : Fpostpone postpone postpone ; immediate
448:
449: : : ( <methodname> -- ) decl @ abort" HOW: missing! "
450: bl word findo 0= abort" not found"
451: dup exec? over early? or over >body cell+ @ 0< or
452: 0= abort" not a method"
453: m-name ! :noname ;
454:
455: Forth
456:
457: : ; ( xt colon-sys -- ) postpone ;
458: m-name @ dup >body swap exec?
459: IF @ o@ +
460: ELSE dup cell+ @ 0< IF 2@ swap o@ + @ + THEN
461: THEN ! ; immediate
462:
463: Forth definitions
464:
465: \ object 23mar95py
466:
467: Create object immediate 0 (class \ do not create as subclass
468: cell var oblink \ create offset for backlink
469: static thread \ method/variable wordlist
470: static parento \ pointer to parent
471: static childo \ ptr to first child
472: static nexto \ ptr to next child of parent
473: static method# \ number of methods (bytes)
474: static size \ number of variables (bytes)
475: static newlink \ ptr to allocated space
476: static ilist \ interface list
477: method init
478: method dispose
479:
480: early class
481: early new immediate
482: early new[] immediate
483: early :
484: early ptr
485: early asptr
486: early []
487: early :: immediate
488: early class?
489: early super immediate
490: early self
491: early bind immediate
492: early bound
493: early link immediate
494: early is immediate
495: early send immediate
496: early with immediate
497: early endwith immediate
498: early ' immediate
499: early postpone immediate
500: early definitions
501:
502: \ base object class implementation part 23mar95py
503:
504: how:
505: 0 parento !
506: 0 childo !
507: 0 nexto !
508: : class ( -- ) Create immediate o@ (class ;
509: : : ( -- ) Create immediate o@
510: decl @ IF instvar, ELSE instance, THEN ;
511: : ptr ( -- ) Create immediate o@
512: decl @ IF instptr, ELSE ptr, THEN ;
513: : asptr ( addr -- )
514: decl @ 0= abort" only in declaration!"
515: Create immediate o@ , cell+ @ , instptr> ;
516: : [] ( n -- ) Create immediate o@
517: decl @ IF instarray, ELSE array, THEN ;
518: : new ( -- o ) o@ state @
519: IF Fpostpone Literal Fpostpone new, ELSE new, THEN ;
520: : new[] ( n -- o ) o@ state @
521: IF Fpostpone Literal Fpostpone new[], ELSE new[], THEN ;
522: : dispose ( -- ) ^ size @ dispose, ;
523: : bind ( addr -- ) (bind ;
524: : bound ( o1 o2 addr2 -- ) (bound ;
525: : link ( -- o addr ) (link ;
526: : class? ( class -- flag ) ^ parent? nip 0<> ;
527: : :: ( -- )
528: state @ IF ^ true method, ELSE inherit THEN ;
529: : super ( -- ) parento true method, ;
530: : is ( cfa -- ) (is ;
531: : self ( -- obj ) ^ ;
532: : init ( -- ) ;
533:
534: : ' ( -- xt ) bl word findo 0= abort" not found!"
535: state @ IF Fpostpone Literal THEN ;
536: : send ( xt -- ) execute ;
537: : postpone ( -- ) o@ add-order Fpostpone Fpostpone drop-order ;
538:
539: : with ( -- )
540: state @ oset? 0= and IF Fpostpone >o THEN
541: o@ add-order voc# ! false to oset? ;
542: : endwith Fpostpone o> voc# @ drop-order ;
543:
544: : definitions
545: o@ add-order 1+ voc# ! also types o@ lastob !
546: false to oset? get-current old-current !
547: thread @ set-current ;
548: class; \ object
549:
550: \ interface 01sep96py
551:
552: Objects definitions
553:
554: : implement ( interface -- )
555: align here over , ob-interface @ , ob-interface !
556: :ilist + @ >r get-order r> swap 1+ set-order 1 voc# +! ;
557:
558: : inter-method, ( interface -- )
559: :ilist + @ bl word count 2dup s" '" compare
560: 0= dup >r IF 2drop bl word count THEN
561: rot search-wordlist
562: dup 0= abort" Not an interface method!"
563: r> IF drop state @ IF postpone Literal THEN EXIT THEN
564: 0< state @ and IF compile, ELSE execute THEN ;
565:
566: Variable inter-list
567: Variable lastif
568: Variable inter#
569:
570: Vocabulary interfaces interfaces definitions
571:
572: : method ( -- ) mallot Create , inter# @ ,
573: DOES> 2@ swap o@ + @ + @ execute ;
574:
575: : how: ( -- ) align
576: here lastif @ ! 0 decl !
577: here last-interface @ , last-interface !
578: inter-list @ , methods @ , inter# @ ,
579: methods @ :inum cell+ ?DO ['] crash , LOOP ;
580:
581: : interface; ( -- ) old-current @ set-current
582: previous previous ;
583:
584: : : ( <methodname> -- ) decl @ abort" HOW: missing! "
585: bl word count lastif @ @ :ilist + @
586: search-wordlist 0= abort" not found"
587: dup >body cell+ @ 0< 0= abort" not a method"
588: m-name ! :noname ;
589:
590: Forth
591:
592: : ; ( xt colon-sys -- ) postpone ;
593: m-name @ >body @ lastif @ @ + ! ; immediate
594:
595: Forth definitions
596:
597: : interface ( -- )
598: Create here lastif ! 0 , get-current old-current !
599: last-interface @ dup IF :inum @ THEN 1 cells - inter# !
600: get-order wordlist
601: dup inter-list ! dup set-current swap 1+ set-order
602: true decl !
603: 0 vars ! :inum cell+ methods ! also interfaces
604: DOES> @ decl @ IF implement ELSE inter-method, THEN ;
605:
606: previous previous
607:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>