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