]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
exile roll and -roll to basis/shuffle and mark them deprecated
[factor.git] / core / kernel / kernel.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel.private slots.private math.private ;
4 IN: kernel
5
6 DEFER: dip
7 DEFER: 2dip
8 DEFER: 3dip
9
10 ! Stack stuff
11 : spin ( x y z -- z y x ) swap rot ; inline
12
13 : 2over ( x y z -- x y z x y ) pick pick ; inline
14
15 : clear ( -- ) { } set-datastack ;
16
17 ! Combinators
18 GENERIC: call ( callable -- )
19
20 GENERIC: execute ( word -- )
21
22 GENERIC: ?execute ( word -- value )
23
24 M: object ?execute ;
25
26 DEFER: if
27
28 : ? ( ? true false -- true/false )
29     #! 'if' and '?' can be defined in terms of each other
30     #! because the JIT special-cases an 'if' preceeded by
31     #! two literal quotations.
32     rot [ drop ] [ nip ] if ; inline
33
34 : if ( ? true false -- ) ? call ;
35
36 ! Single branch
37 : unless ( ? false -- )
38     swap [ drop ] [ call ] if ; inline
39
40 : when ( ? true -- )
41     swap [ call ] [ drop ] if ; inline
42
43 ! Anaphoric
44 : if* ( ? true false -- )
45     pick [ drop call ] [ 2nip call ] if ; inline
46
47 : when* ( ? true -- )
48     over [ call ] [ 2drop ] if ; inline
49
50 : unless* ( ? false -- )
51     over [ drop ] [ nip call ] if ; inline
52
53 ! Default
54 : ?if ( default cond true false -- )
55     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
56
57 ! Dippers.
58 ! Not declared inline because the compiler special-cases them
59
60 : dip ( x quot -- x ) swap [ call ] dip ;
61
62 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
63
64 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
65
66 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
67
68 ! Keepers
69 : keep ( x quot -- x ) over [ call ] dip ; inline
70
71 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
72
73 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
74
75 ! Cleavers
76 : bi ( x p q -- )
77     [ keep ] dip call ; inline
78
79 : tri ( x p q r -- )
80     [ [ keep ] dip keep ] dip call ; inline
81
82 ! Double cleavers
83 : 2bi ( x y p q -- )
84     [ 2keep ] dip call ; inline
85
86 : 2tri ( x y p q r -- )
87     [ [ 2keep ] dip 2keep ] dip call ; inline
88
89 ! Triple cleavers
90 : 3bi ( x y z p q -- )
91     [ 3keep ] dip call ; inline
92
93 : 3tri ( x y z p q r -- )
94     [ [ 3keep ] dip 3keep ] dip call ; inline
95
96 ! Spreaders
97 : bi* ( x y p q -- )
98     [ dip ] dip call ; inline
99
100 : tri* ( x y z p q r -- )
101     [ [ 2dip ] dip dip ] dip call ; inline
102
103 ! Double spreaders
104 : 2bi* ( w x y z p q -- )
105     [ 2dip ] dip call ; inline
106
107 : 2tri* ( u v w x y z p q r -- )
108     [ 4dip ] 2dip 2bi* ; inline
109
110 ! Appliers
111 : bi@ ( x y quot -- )
112     dup bi* ; inline
113
114 : tri@ ( x y z quot -- )
115     dup dup tri* ; inline
116
117 ! Double appliers
118 : 2bi@ ( w x y z quot -- )
119     dup 2bi* ; inline
120
121 : 2tri@ ( u v w x y z quot -- )
122     dup dup 2tri* ; inline
123
124 ! Quotation building
125 : 2curry ( obj1 obj2 quot -- curry )
126     curry curry ; inline
127
128 : 3curry ( obj1 obj2 obj3 quot -- curry )
129     curry curry curry ; inline
130
131 : with ( param obj quot -- obj curry )
132     swapd [ swapd call ] 2curry ; inline
133
134 : prepose ( quot1 quot2 -- compose )
135     swap compose ; inline
136
137 ! Curried cleavers
138 <PRIVATE
139
140 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
141
142 PRIVATE>
143
144 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
145
146 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
147
148 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
149
150 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
151
152 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
153
154 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
155
156 ! Booleans
157 UNION: boolean POSTPONE: t POSTPONE: f ;
158
159 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
160
161 : not ( obj -- ? ) [ f ] [ t ] if ; inline
162
163 : and ( obj1 obj2 -- ? ) over ? ; inline
164
165 : or ( obj1 obj2 -- ? ) dupd ? ; inline
166
167 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
168
169 : both? ( x y quot -- ? ) bi@ and ; inline
170
171 : either? ( x y quot -- ? ) bi@ or ; inline
172
173 : most ( x y quot -- z ) 2keep ? ; inline
174
175 ! Loops
176 : loop ( pred: ( -- ? ) -- )
177     [ call ] keep [ loop ] curry when ; inline recursive
178
179 : do ( pred body -- pred body )
180     dup 2dip ; inline
181
182 : while ( pred: ( -- ? ) body: ( -- ) -- )
183     swap do compose [ loop ] curry when ; inline
184
185 : until ( pred: ( -- ? ) body: ( -- ) -- )
186     [ [ not ] compose ] dip while ; inline
187
188 ! Object protocol
189 GENERIC: hashcode* ( depth obj -- code )
190
191 M: object hashcode* 2drop 0 ; inline
192
193 M: f hashcode* 2drop 31337 ; inline
194
195 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
196
197 GENERIC: equal? ( obj1 obj2 -- ? )
198
199 M: object equal? 2drop f ; inline
200
201 TUPLE: identity-tuple ;
202
203 M: identity-tuple equal? 2drop f ; inline
204
205 : = ( obj1 obj2 -- ? )
206     2dup eq? [ 2drop t ] [
207         2dup both-fixnums? [ 2drop f ] [ equal? ] if
208     ] if ; inline
209
210 GENERIC: clone ( obj -- cloned )
211
212 M: object clone ; inline
213
214 M: callstack clone (clone) ; inline
215
216 ! Tuple construction
217 GENERIC: new ( class -- tuple )
218
219 GENERIC: boa ( ... class -- tuple )
220
221 ! Error handling -- defined early so that other files can
222 ! throw errors before continuations are loaded
223 GENERIC: throw ( error -- * )
224
225 ERROR: assert got expect ;
226
227 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
228
229 <PRIVATE
230
231 : declare ( spec -- ) drop ;
232
233 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
234
235 : do-primitive ( number -- ) "Improper primitive call" throw ;
236
237 PRIVATE>