-
Notifications
You must be signed in to change notification settings - Fork 2
/
pboof.fs
288 lines (211 loc) · 6.71 KB
/
pboof.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
\ OBJECT STACK
require later.fs
expose-module private
16 cells
constant /ostack
\g Size of object stack
create otop /ostack allot
\g Start of object stack
otop /ostack +
constant obottom
\g Bottom of object stack
obottom
value otos
\g Pointer to top of object stack
\g Push object to object stack
: >o ( obj -- O: -- obj )
otos cell- to otos otos ! ;
\g Drop object from object stack
: odrop ( O: obj -- )
otos cell+ to otos ;
\g Fetch topmost object from object stack
: o@ ( -- obj O: obj -- obj )
otos @ ;
\g Pop object from object stack
: o> ( -- obj O: obj -- )
o@ odrop ;
\g Duplicate topmost object
: odup ( O: obj -- obj )
o@ >o ;
\g Pick item from object stack
: opick ( u -- obj )
cells otos + @ ;
\g Depth of object stack
: odepth ( -- u )
obottom otos - 1 cells / ;
\g Print object stack
: o.s
." O: " odepth 0 <# [char] > hold #s [char] < hold #> type space
odepth 0 ?do odepth 1- i - opick . loop cr ;
\ OBJECT ARENA
1024 16 * constant /oarena \ Size of object arena
create o0 /oarena allot \ Start of object arena
here constant otop \ End of object arena
o0 value ohere \ Object arena pointer
: osize ( -- u ) ohere o0 - ; \ Current size of object arena
: (>oarena)
dict0 dicttop here 3
o0 to dict0 otop to dicttop ohere to here ;
: >oarena \ Switch compilation area to object arena
postpone (>oarena) postpone n>r ; immediate compile-only
: (oarena>)
here to ohere
3 <> throw to here to dicttop to dict0 ;
: oarena> \ Restore normal compilation area
postpone nr> postpone (oarena>) ; immediate compile-only
: oallot ( u -- ) \ Allocate space in object arena
>oarena allot oarena> ;
: o, ( u -- ) \ Compile word in object arena
>oarena , oarena> ;
: owordlist ( -- addr ) \ Create wordlist in object arena
ohere 0 o, ;
\ HELPERS
-1 value mbrhandler
-1 value objhandler
-1 value inshandler
: handler ( xt -- addr )
?dodefine ['] docreate = swap @ and ;
\ Add current object to order stack and set it as current wordlist
: (extend) ( O: obj -- obj )
o@ +order definitions ;
\ Remove active object from the order stack and restore previously
\ current wordlist
: (extended) previous definitions ;
\g Address of current object size field
: sizeof ( -- addr O: obj -- obj )
o@ cell+ ;
\g Send late message to current object
: (late) ( addr len -- O: obj -- ) nfa doword ;
\ Create wordlist and chain it to prototype wordlist
: clonewl o@ @ owordlist ! ;
\ Clone the sizeof field
: clonesz sizeof @ o, ;
: mbr? name>xt handler mbrhandler = ;
: ins? name>xt handler inshandler = ;
: obj? name>xt handler objhandler = ;
: slot? dup mbr? swap ins? or ;
0 value nextslot
: (findoffset) ( offset nfa -- offset nfa )
>r r@ slot? if
r@ name>xt >body @ over = if r@ to found else r@ to nextslot then
then r> ;
: offset>slot ( offset -- nfa )
0 to nextslot
o@ ['] (findoffset) forwordsin drop found ;
: forslots ( xt -- )
0 to nextslot
>r 0 begin dup sizeof @ < while
dup offset>slot ?dup if
r@ execute
then 1+
repeat rdrop drop ;
: slotoffset ( nfa -- offset )
name>xt >body @ ;
: slotaddr ( nfa -- addr )
slotoffset o@ + ;
: /slot ( nfa -- )
nextslot if nextslot slotoffset else sizeof @ then swap slotoffset - ;
: clonembr ( nfa -- )
>r r@ slotaddr ohere r> /slot dup oallot move ;
: cloneins ( nfa -- )
slotaddr >o (extend) s" cloned" (late) (extended) odrop ;
: cloneslot ( nfa -- )
dup mbr? if clonembr else cloneins then ;
: cloneslots ( cloneaddr -- cloneaddr )
['] cloneslot forslots ;
: /method odrop state @ if postpone odrop then ;
\g Runtime for cloned objects
: doobj @r+ >o ;
: single (extend) parse-word (late) (extended) ;
: (clone)
create immediate here 0 ,
does> @ state @ if postpone doobj dup , then
>o single /method ;
\g Runtime for instance members
: doinst @r+ o@ + >o ;
: (instance)
create immediate sizeof @ , sizeof +!
does> @ state @ if postpone doinst dup , then
o@ + >o single /method ;
: doref @r+ o@ + @ >o ;
: (reference)
dup o,
create immediate sizeof @ , 1 cells sizeof +!
does> @ state @ if postpone doref dup , then
o@ + @ >o single /method ;
: .addr ( addr -- )
16 based . ;
: .mbr ( nfa -- )
." member at " slotaddr dup .addr ." : " @ . cr ;
: .ins ( nfa -- )
slotaddr
." instance at " dup .addr cr
>o (extend) s" print" (late) (extended) odrop ;
: .slot ( nfa -- )
odepth 1- spaces dup .name dup mbr? if .mbr else .ins then ;
\ WORLD
\ Some definitions:
\ Current object: topmost object in object stack
\ Active object: object that will receive subsequent messages
\ Create the bootstrap world, just a wordlist and a sizeof field
\ in the object arena
:noname owordlist 2 cells o, ; execute constant (world)
\ Push the bootstrap world to the object and order stacks and establish it as
\ current worldist, making it the active object
(world) >o (extend)
\g Make current object the active object
: extend ( O: obj -- obj )
odup (extend) ;
\g Restore previously active object
: extended ( O: obj -- )
(extended) odrop ;
\g Send late message to current object
: late ( "message" -- ? runtime: ? -- ? O: obj -- )
parse-word postpone sliteral postpone (late) ; immediate
\g Create member in active object
: member ( size "name" -- )
dup oallot create sizeof @ , sizeof +! does> @ o@ + ; immediate
\g Clone active object
: cloned ( -- O: prototype -- prototype )
ohere clonewl clonesz cloneslots extended >o (extend) ;
\g Clone active object by sending a late CLONED message
: clone ( "name" -- )
(extended) (clone) (extend) late cloned o@ swap ! ;
\g Instance active object as member of the previously active object
: instance ( "name" -- )
o@ sizeof @ extended (instance) >o (extend) late cloned ;
: reference ( "name" -- )
o@ extended (reference) >o (extend) ;
: dorto @r+ o@ + ! ;
: rto postpone dorto ' >body @ , ; immediate compile-only
\g Dump object memory
: dump o@ sizeof @ dump ;
\g Print object
: print
odepth 2 - spaces ." object at " o@ .addr ." sized " sizeof @ . cr
['] .slot forslots ;
\ Get current object pointer
: self ( -- object) o@ ;
\ Evaluate method posing as object
: as ( "object" method -- )
here 4 cells + postpone literal postpone ! ; immediate compile-only
\g Print context information
: .ctx
cr order o.s .s cr ;
extended
(world) +order
: world (world) >o single odrop ;
previous
export world osize
\ OBJECT
world extend
world clone object
\g Prototype object
object clone dummy
dummy extend
' dummy handler to objhandler
1 cells member mbr ' mbr handler to mbrhandler
object instance ins ' ins handler to inshandler
extended
extended
end-module