forked from factor/factor
/
kernel.factor
248 lines (161 loc) · 5.65 KB
/
kernel.factor
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
! Copyright (C) 2004, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel.private slots.private math.private ;
IN: kernel
DEFER: dip
DEFER: 2dip
DEFER: 3dip
! Stack stuff
: 2over ( x y z -- x y z x y ) pick pick ; inline
: clear ( -- ) { } set-datastack ;
! Combinators
GENERIC: call ( callable -- )
GENERIC: execute ( word -- )
GENERIC: ?execute ( word -- value )
M: object ?execute ;
DEFER: if
: ? ( ? true false -- true/false )
#! 'if' and '?' can be defined in terms of each other
#! because the JIT special-cases an 'if' preceeded by
#! two literal quotations.
rot [ drop ] [ nip ] if ; inline
: if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
! Single branch
: unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
swap [ drop ] [ call ] if ; inline
: when ( ..a ? true: ( ..a -- ..a ) -- ..a )
swap [ call ] [ drop ] if ; inline
! Anaphoric
: if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
pick [ drop call ] [ 2nip call ] if ; inline
: when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
over [ call ] [ 2drop ] if ; inline
: unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
over [ drop ] [ nip call ] if ; inline
! Default
: ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
! Dippers.
! Not declared inline because the compiler special-cases them
: dip ( x quot -- x ) swap [ call ] dip ;
: 2dip ( x y quot -- x y ) swap [ dip ] dip ;
: 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
: 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
! Keepers
: keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
over [ call ] dip ; inline
: 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
[ 2dup ] dip 2dip ; inline
: 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
[ 3dup ] dip 3dip ; inline
! Cleavers
: bi ( x p q -- )
[ keep ] dip call ; inline
: tri ( x p q r -- )
[ [ keep ] dip keep ] dip call ; inline
! Double cleavers
: 2bi ( x y p q -- )
[ 2keep ] dip call ; inline
: 2tri ( x y p q r -- )
[ [ 2keep ] dip 2keep ] dip call ; inline
! Triple cleavers
: 3bi ( x y z p q -- )
[ 3keep ] dip call ; inline
: 3tri ( x y z p q r -- )
[ [ 3keep ] dip 3keep ] dip call ; inline
! Spreaders
: bi* ( x y p q -- )
[ dip ] dip call ; inline
: tri* ( x y z p q r -- )
[ [ 2dip ] dip dip ] dip call ; inline
! Double spreaders
: 2bi* ( w x y z p q -- )
[ 2dip ] dip call ; inline
: 2tri* ( u v w x y z p q r -- )
[ 4dip ] 2dip 2bi* ; inline
! Appliers
: bi@ ( x y quot -- )
dup bi* ; inline
: tri@ ( x y z quot -- )
dup dup tri* ; inline
! Double appliers
: 2bi@ ( w x y z quot -- )
dup 2bi* ; inline
: 2tri@ ( u v w x y z quot -- )
dup dup 2tri* ; inline
! Quotation building
: 2curry ( obj1 obj2 quot -- curry )
curry curry ; inline
: 3curry ( obj1 obj2 obj3 quot -- curry )
curry curry curry ; inline
: with ( param obj quot -- obj curry )
swapd [ swapd call ] 2curry ; inline
: prepose ( quot1 quot2 -- compose )
swap compose ; inline
! Curried cleavers
<PRIVATE
: [curry] ( quot -- quot' ) [ curry ] curry ; inline
PRIVATE>
: bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
: tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
: bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
: tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
: bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
: tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
! Booleans
UNION: boolean POSTPONE: t POSTPONE: f ;
: >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
: not ( obj -- ? ) [ f ] [ t ] if ; inline
: and ( obj1 obj2 -- ? ) over ? ; inline
: or ( obj1 obj2 -- ? ) dupd ? ; inline
: xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
: both? ( x y quot -- ? ) bi@ and ; inline
: either? ( x y quot -- ? ) bi@ or ; inline
: most ( x y quot -- z ) 2keep ? ; inline
! Loops
: loop ( ... pred: ( ... -- ... ? ) -- ... )
[ call ] keep [ loop ] curry when ; inline recursive
: do ( pred body -- pred body )
dup 2dip ; inline
: while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
swap do compose [ loop ] curry when ; inline
: until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
[ [ not ] compose ] dip while ; inline
! Object protocol
GENERIC: hashcode* ( depth obj -- code )
M: object hashcode* 2drop 0 ; inline
M: f hashcode* 2drop 31337 ; inline
: hashcode ( obj -- code ) 3 swap hashcode* ; inline
: identity-hashcode ( obj -- code )
dup tag 0 eq? [
dup tag 1 eq? [ drop 0 ] [
dup (identity-hashcode) dup 0 eq? [
drop dup compute-identity-hashcode
(identity-hashcode)
] [ nip ] if
] if
] unless ; inline
GENERIC: equal? ( obj1 obj2 -- ? )
M: object equal? 2drop f ; inline
TUPLE: identity-tuple ;
M: identity-tuple equal? 2drop f ; inline
M: identity-tuple hashcode* nip identity-hashcode ; inline
: = ( obj1 obj2 -- ? )
2dup eq? [ 2drop t ] [
2dup both-fixnums? [ 2drop f ] [ equal? ] if
] if ; inline
GENERIC: clone ( obj -- cloned )
M: object clone ; inline
M: callstack clone (clone) ; inline
! Tuple construction
GENERIC: new ( class -- tuple )
GENERIC: boa ( slots... class -- tuple )
! Error handling -- defined early so that other files can
! throw errors before continuations are loaded
GENERIC: throw ( error -- * )
ERROR: assert got expect ;
: assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
<PRIVATE
: declare ( spec -- ) drop ;
: do-primitive ( number -- ) "Improper primitive call" throw ;
PRIVATE>