-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjects.fs
547 lines (446 loc) · 17.3 KB
/
objects.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
\ yet another Forth objects extension
\ written by Anton Ertl 1996-2000
\ public domain; NO WARRANTY
\ This (in combination with compat/struct.fs) is in ANS Forth (with an
\ environmental dependence on case insensitivity; convert everything
\ to upper case for state sensitive systems).
\ compat/struct.fs and this file together use the following words:
\ from CORE :
\ : 1- + swap invert and ; DOES> @ immediate drop Create rot dup , >r
\ r> IF ELSE THEN over chars aligned cells 2* here - allot execute
\ POSTPONE ?dup 2dup move Variable 2@ 2! ! ['] >body = 2drop ' r@ +!
\ Constant recurse 1+ BEGIN 0= UNTIL negate Literal ." .
\ from CORE-EXT :
\ tuck pick nip true <> 0> erase Value :noname compile,
\ from BLOCK-EXT :
\ \
\ from DOUBLE :
\ 2Constant
\ from EXCEPTION :
\ throw catch
\ from EXCEPTION-EXT :
\ abort"
\ from FILE :
\ (
\ from FLOAT :
\ faligned floats
\ from FLOAT-EXT :
\ dfaligned dfloats sfaligned sfloats
\ from LOCAL :
\ TO
\ from MEMORY :
\ allocate resize free
\ from SEARCH :
\ get-order set-order wordlist get-current set-current
\ needs struct.fs
\ helper words
s" gforth" environment? [if]
2drop
[else]
: -rot ( a b c -- c a b )
rot rot ;
: perform ( ... addr -- ... )
@ execute ;
: ?dup-if ( compilation: -- orig ; run-time: n -- n| )
POSTPONE ?dup POSTPONE if ; immediate
: save-mem ( addr1 u -- addr2 u ) \ gforth
\ copy a memory block into a newly allocated region in the heap
swap >r
dup allocate throw
swap 2dup r> -rot move ;
: save-mem-dict ( addr1 u -- addr2 u )
here swap dup allot ( addr1 addr2 u )
2dup 2>r move 2r> ;
: resize ( a-addr1 u -- a-addr2 ior ) \ gforth
over
if
resize
else
nip allocate
then ;
: extend-mem ( addr1 u1 u -- addr addr2 u2 )
\ extend memory block allocated from the heap by u aus
\ the (possibly reallocated) piece is addr2 u2, the extension is at addr
over >r + dup >r resize throw
r> over r> + -rot ;
: \g ( -- )
postpone \ ; immediate
[then]
[undefined] heap>dict [if]
: heap>dict ( addr1 u -- addr2 u )
over >r save-mem-dict r> free throw ;
[then]
[undefined] create-field [if]
: create-field ( align1 offset1 align size "name" -- align2 offset2 )
create swap rot over nalign dup , ( align1 size align offset )
rot + >r nalign r> ;
[then]
\ data structures
struct
cell% field object-map
end-struct object%
struct
cell% 2* field interface-map
cell% field interface-map-offset \ aus
\ difference between where interface-map points and where
\ object-map points (0 for non-classes)
cell% field interface-offset \ aus
\ offset of interface map-pointer in class-map (0 for classes)
end-struct interface%
interface%
cell% field class-parent
cell% field class-wordlist \ inst-vars and other protected words
cell% 2* field class-inst-size ( class -- addr ) \ objects- objects
\g Give the size specification for an instance (i.e. an object)
\g of @var{class};
\g used as @code{class-inst-size 2@ ( class -- align size )}.
end-struct class%
struct
cell% field selector-offset \ the offset within the (interface) map
cell% field selector-interface \ the interface offset
end-struct selector%
\ maps are not defined explicitly; they have the following structure:
\ pointers to interface maps (for classes) <- interface-map points here
\ interface%/class% pointer <- (object-)map points here
\ xts of methods
\ code
\ selectors and methods
variable current-interface ( -- addr ) \ objects- objects
\g Variable: contains the class or interface currently being
\g defined.
: no-method ( -- )
true abort" no method defined for this object/selector combination" ;
: do-class-method ( -- )
does> ( ... object -- ... )
( object selector-body )
selector-offset @ over object-map @ + ( object xtp ) perform ;
: do-interface-method ( -- )
does> ( ... object -- ... )
( object selector-body )
2dup selector-interface @ ( object selector-body object interface-offset )
swap object-map @ + @ ( object selector-body map )
swap selector-offset @ + perform ;
: method ( xt "name" -- ) \ objects- objects
\g @code{name} execution: @code{... object -- ...}@*
\g Create selector @var{name} and makes @var{xt} its method in
\g the current class.
create
current-interface @ interface-map 2@ ( xt map-addr map-size )
dup current-interface @ interface-map-offset @ - ,
1 cells extend-mem current-interface @ interface-map 2! ! ( )
current-interface @ interface-offset @ dup ,
( 0<> ) if
do-interface-method
else
do-class-method
then ;
: selector ( "name" -- ) \ objects- objects
\g @var{name} execution: @code{... object -- ...}@*
\g Create selector @var{name} for the current class and its
\g descendents; you can set a method for the selector in the
\g current class with @code{overrides}.
['] no-method method ;
: interface-override! ( xt sel-xt interface-map -- )
\ xt is the new method for the selector sel-xt in interface-map
swap >body ( xt map selector-body )
selector-offset @ + ! ;
: class->map ( class -- map ) \ objects- objects
\g @var{map} is the pointer to @var{class}'s method map; it
\g points to the place in the map to which the selector offsets
\g refer (i.e., where @var{object-map}s point to).
dup interface-map 2@ drop swap interface-map-offset @ + ;
: unique-interface-map ( class-map offset -- )
\ if the interface at offset in class map is the same as its parent,
\ copy it to make it unique; used for implementing a copy-on-write policy
over @ class-parent @ class->map ( class-map offset parent-map )
over + @ >r \ the map for the interface for the parent
+ dup @ ( interface-mapp interface-map )
dup r> =
if
dup @ interface-map 2@ nip save-mem-dict drop
swap !
else
2drop
then ;
: class-override! ( xt sel-xt class-map -- ) \ objects- objects
\g @var{xt} is the new method for the selector @var{sel-xt} in
\g @var{class-map}.
over >body ( xt sel-xt class-map selector-body )
selector-interface @ ( xt sel-xt class-map offset )
?dup-if \ the selector is for an interface
2dup unique-interface-map
+ @
then
interface-override! ;
: overrides ( xt "selector" -- ) \ objects- objects
\g replace default method for @var{selector} in the current class
\g with @var{xt}. @code{overrides} must not be used during an
\g interface definition.
' current-interface @ class->map class-override! ;
\ interfaces
\ every interface gets a different offset; the latest one is stored here
variable last-interface-offset 0 last-interface-offset !
: interface ( -- ) \ objects- objects
\g Start an interface definition.
interface% %allot >r
r@ current-interface !
current-interface 1 cells save-mem r@ interface-map 2!
-1 cells last-interface-offset +!
last-interface-offset @ r@ interface-offset !
0 r> interface-map-offset ! ;
: end-interface-noname ( -- interface ) \ objects- objects
\g End an interface definition. The resulting interface is
\g @var{interface}.
current-interface @
dup interface-map dup 2@ heap>dict rot 2! ;
: end-interface ( "name" -- ) \ objects- objects
\g @code{name} execution: @code{-- interface}@*
\g End an interface definition. The resulting interface is
\g @var{interface}.
end-interface-noname constant ;
\ visibility control
variable public-wordlist
: protected ( -- ) \ objects- objects
\g Set the compilation wordlist to the current class's wordlist
current-interface @ class-wordlist @
dup get-current <>
if \ we are not protected already
get-current public-wordlist !
then
set-current ;
: public ( -- ) \ objects- objects
\g Restore the compilation wordlist that was in effect before the
\g last @code{protected} that actually changed the compilation
\g wordlist.
current-interface @ class-wordlist @ get-current =
if \ we are protected
public-wordlist @ set-current
then ;
\ classes
: add-class-order ( n1 class -- wid1 ... widn n+n1 )
dup >r class-parent @
?dup-if
recurse \ first add the search order for the parent class
then
r> class-wordlist @ swap 1+ ;
: class>order ( class -- ) \ objects- objects
\g Add @var{class}'s wordlists to the head of the search-order.
>r get-order r> add-class-order set-order ;
: push-order class>order ; \ old name
: 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 ! class>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
\g @var{field} etc.
class% %allot >r
dup interface-map 2@ save-mem r@ interface-map 2!
dup interface-map-offset @ r@ interface-map-offset !
r@ dup class->map !
0 r@ interface-offset !
dup r@ class-parent !
wordlist r@ class-wordlist !
r> methods
class-inst-size 2@ ;
: remove-class-order ( wid1 ... widn n+n1 class -- n1 )
\ note: no checks, whether the wordlists are correct
begin
>r nip 1-
r> class-parent @ dup 0=
until
drop ;
: class-previous ( class -- ) \ objects- objects
\g Drop @var{class}'s wordlists from the search order. No
\g checking is made whether @var{class}'s wordlists are actually
\g on the search order.
>r get-order r> remove-class-order set-order ;
: drop-order class-previous ; \ old name
: 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 @ class-previous ;
: end-class-noname ( align offset -- class ) \ objects- objects
\g End a class definition. The resulting class is @var{class}.
public end-methods
current-interface @ class-inst-size 2!
end-interface-noname ;
: end-class ( align offset "name" -- ) \ objects- objects
\g @var{name} execution: @code{-- class}@*
\g End a class definition. The resulting class is @var{class}.
\ name execution: ( -- class )
end-class-noname constant ;
\ classes that implement interfaces
: front-extend-mem ( addr1 u1 u -- addr addr2 u2 )
\ Extend memory block allocated from the heap by u aus, with the
\ old stuff coming at the end
2dup + dup >r allocate throw ( addr1 u1 u addr2 ; R: u2 )
dup >r + >r over r> rot move ( addr1 ; R: u2 addr2 )
free throw
r> dup r> ;
: implementation ( interface -- ) \ objects- objects
\g The current class implements @var{interface}. I.e., you can
\g use all selectors of the interface in the current class and its
\g descendents.
dup interface-offset @ ( interface offset )
current-interface @ interface-map-offset @ negate over - dup 0>
if \ the interface does not fit in the present class-map
>r current-interface @ interface-map 2@
r@ front-extend-mem
current-interface @ interface-map 2!
r@ erase
dup negate current-interface @ interface-map-offset !
r>
then ( interface offset n )
drop >r
interface-map 2@ save-mem-dict drop ( map )
current-interface @ dup interface-map 2@ drop
swap interface-map-offset @ + r> + ! ;
\ this/self, instance variables etc.
\ rename "this" into "self" if you are a Smalltalk fiend
[defined] uvalue [if]
uvalue this ( -- object ) \ objects- objects
[else]
0 value this ( -- object ) \ objects- objects
[then]
\g the receiving object of the current method (aka active object).
: to-this ( object -- ) \ objects- objects
\g Set @code{this} (used internally, but useful when debugging).
TO this ;
\ another implementation, if you don't have (fast) values
\ variable thisp
\ : this ( -- object )
\ thisp @ ;
\ : to-this ( object -- )
\ thisp ! ;
: 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}.
POSTPONE r>
POSTPONE to-this
POSTPONE exit ; immediate
: ;m ( colon-sys --; run-time: -- ) \ objects- objects
\g End a method definition; restore old @code{this}.
POSTPONE r>
POSTPONE to-this
POSTPONE ; ; immediate
: catch ( ... xt -- ... n ) \ exception
\ Make it safe to call CATCH within a method.
\ should also be done with all words containing CATCH.
this >r catch r> to-this ;
\ the following is a bit roundabout; this is caused by the standard
\ disallowing to change the compilation wordlist between CREATE and
\ DOES> (see RFI 3)
: inst-something ( align1 size1 align size xt "name" -- align2 size2 )
\ xt ( -- ) typically is for a DOES>-word
get-current >r
current-interface @ class-wordlist @ set-current
>r create-field r> execute
r> set-current ;
: do-inst-var ( -- )
does> \ name execution: ( -- addr )
( addr1 ) @ this + ;
: inst-var ( align1 offset1 align size "name" -- align2 offset2 ) \ objects- objects
\g @var{name} execution: @code{-- addr}@*
\g @var{addr} is the address of the field @var{name} in
\g @code{this} object.
['] do-inst-var inst-something ;
: do-inst-value ( -- )
does> \ name execution: ( -- w )
( addr1 ) @ this + @ ;
: inst-value ( align1 offset1 "name" -- align2 offset2 ) \ objects- objects
\g @var{name} execution: @code{-- w}@*
\g @var{w} is the value of the field @var{name} in @code{this}
\g object.
cell% ['] do-inst-value inst-something ;
: <to-inst> ( w xt -- ) \ objects- objects
\g store @var{w} into the field @var{xt} in @code{this} object.
>body @ this + ! ;
: [to-inst] ( compile-time: "name" -- ; run-time: w -- ) \ objects- objects
\g store @var{w} into field @var{name} in @code{this} object.
' >body @ POSTPONE literal
POSTPONE this
POSTPONE +
POSTPONE ! ; immediate
\ class binding stuff
: <bind> ( class selector-xt -- xt ) \ objects- objects
\g @var{xt} is the method for the selector @var{selector-xt} in
\g @var{class}.
>body swap class->map over selector-interface @
?dup-if
+ @
then
swap selector-offset @ + @ ;
: bind' ( "class" "selector" -- xt ) \ objects- objects
\g @var{xt} is the method for @var{selector} in @var{class}.
' execute ' <bind> ;
: bind ( ... "class" "selector" -- ... ) \ objects- objects
\g Execute the method for @var{selector} in @var{class}.
bind' execute ;
: [bind] ( compile-time: "class" "selector" -- ; run-time: ... object -- ... ) \ objects- objects
\g Compile the method for @var{selector} in @var{class}.
bind' compile, ; immediate
: current' ( "selector" -- xt ) \ objects- objects
\g @var{xt} is the method for @var{selector} in the current class.
current-interface @ ' <bind> ;
: [current] ( compile-time: "selector" -- ; run-time: ... object -- ... ) \ objects- objects
\g Compile the method for @var{selector} in the current class.
current' compile, ; immediate
: [parent] ( compile-time: "selector" -- ; run-time: ... object -- ... ) \ objects- objects
\g Compile the method for @var{selector} in the parent of the
\g current class.
current-interface @ class-parent @ ' <bind> compile, ; immediate
\ the object class
\ because OBJECT has no parent class, we have to build it by hand
\ (instead of with class)
class% %allot current-interface !
current-interface 1 cells save-mem current-interface @ interface-map 2!
0 current-interface @ interface-map-offset !
0 current-interface @ interface-offset !
0 current-interface @ class-parent !
wordlist current-interface @ class-wordlist !
object%
current-interface @ class>order
' drop ( object -- )
method construct ( ... object -- ) \ objects- objects
\g Initialize the data fields of @var{object}. The method for the
\g class @var{object} just does nothing: @code{( object -- )}.
:noname ( object -- )
." object:" dup . ." class:" object-map @ @ . ;
method print ( object -- ) \ objects- objects
\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.
\ constructing objects
: init-object ( ... class object -- ) \ objects- objects
\g Initialize a chunk of memory (@var{object}) to an object of
\g class @var{class}; then performs @code{construct}.
swap class->map over object-map ! ( ... object )
construct ;
: xt-new ( ... class xt -- object ) \ objects- objects
\g Make a new object, using @code{xt ( align size -- addr )} to
\g get memory.
over class-inst-size 2@ rot execute
dup >r init-object r> ;
: dict-new ( ... class -- object ) \ objects- objects
\g @code{allot} and initialize an object of class @var{class} in
\g the dictionary.
['] %allot xt-new ;
: heap-new ( ... class -- object ) \ objects- objects
\g @code{allocate} and initialize an object of class @var{class}.
['] %alloc xt-new ;