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