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