]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
kernel: new combinator 2with = with with
[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 BUILTIN: callstack ;
7 BUILTIN: tuple ;
8 BUILTIN: wrapper { wrapped read-only } ;
9
10 DEFER: dip
11 DEFER: 2dip
12 DEFER: 3dip
13
14 ! Stack stuff
15 : 2over ( x y z -- x y z x y ) pick pick ; inline
16
17 : clear ( -- ) { } set-datastack ;
18
19 ! Combinators
20 GENERIC: call ( callable -- )
21
22 GENERIC: execute ( word -- )
23
24 DEFER: if
25
26 : ? ( ? true false -- true/false )
27     #! 'if' and '?' can be defined in terms of each other
28     #! because the JIT special-cases an 'if' preceeded by
29     #! two literal quotations.
30     rot [ drop ] [ nip ] if ; inline
31
32 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
33
34 ! Single branch
35 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
36     swap [ drop ] [ call ] if ; inline
37
38 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
39     swap [ call ] [ drop ] if ; inline
40
41 ! Anaphoric
42 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
43     pick [ drop call ] [ 2nip call ] if ; inline
44
45 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
46     over [ call ] [ 2drop ] if ; inline
47
48 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
49     over [ drop ] [ nip call ] if ; inline
50
51 ! Default
52 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
53     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
54
55 ! Dippers.
56 ! Not declared inline because the compiler special-cases them
57
58 : dip ( x quot -- x ) swap [ call ] dip ;
59
60 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
61
62 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
63
64 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
65
66 ! Keepers
67 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
68     over [ call ] dip ; inline
69
70 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
71     [ 2dup ] dip 2dip ; inline
72
73 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
74     [ 3dup ] dip 3dip ; inline
75
76 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
77     [ 4dup ] dip 4dip ; 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 : 2with ( param1 param2 obj quot -- obj curry )
139     with with ; inline
140
141 : prepose ( quot1 quot2 -- compose )
142     swap compose ; inline
143
144 ! Curried cleavers
145 <PRIVATE
146
147 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
148
149 PRIVATE>
150
151 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
152
153 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
154
155 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
156
157 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
158
159 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
160
161 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
162
163 ! Booleans
164 UNION: boolean POSTPONE: t POSTPONE: f ;
165
166 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
167
168 : not ( obj -- ? ) [ f ] [ t ] if ; inline
169
170 : and ( obj1 obj2 -- ? ) over ? ; inline
171
172 : or ( obj1 obj2 -- ? ) dupd ? ; inline
173
174 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
175
176 : both? ( x y quot -- ? ) bi@ and ; inline
177
178 : either? ( x y quot -- ? ) bi@ or ; inline
179
180 : most ( x y quot -- z ) 2keep ? ; inline
181
182 ! Loops
183 : loop ( ... pred: ( ... -- ... ? ) -- ... )
184     [ call ] keep [ loop ] curry when ; inline recursive
185
186 : do ( pred body -- pred body )
187     dup 2dip ; inline
188
189 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
190     swap do compose [ loop ] curry when ; inline
191
192 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
193     [ [ not ] compose ] dip while ; inline
194
195 ! Object protocol
196 GENERIC: hashcode* ( depth obj -- code )
197
198 M: object hashcode* 2drop 0 ; inline
199
200 M: f hashcode* 2drop 31337 ; inline
201
202 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
203
204 : identity-hashcode ( obj -- code )
205     dup tag 0 eq? [
206         dup tag 1 eq? [ drop 0 ] [
207             dup (identity-hashcode) dup 0 eq? [
208                 drop dup compute-identity-hashcode
209                 (identity-hashcode)
210             ] [ nip ] if
211         ] if
212     ] unless ; inline
213
214 GENERIC: equal? ( obj1 obj2 -- ? )
215
216 M: object equal? 2drop f ; inline
217
218 TUPLE: identity-tuple ;
219
220 M: identity-tuple equal? 2drop f ; inline
221
222 M: identity-tuple hashcode* nip identity-hashcode ; inline
223
224 : = ( obj1 obj2 -- ? )
225     2dup eq? [ 2drop t ] [
226         2dup both-fixnums? [ 2drop f ] [ equal? ] if
227     ] if ; inline
228
229 : same? ( x y quot -- ? ) bi@ = ; inline
230
231 GENERIC: clone ( obj -- cloned )
232
233 M: object clone ; inline
234
235 M: callstack clone (clone) ; inline
236
237 ! Tuple construction
238 GENERIC: new ( class -- tuple )
239
240 GENERIC: boa ( slots... class -- tuple )
241
242 ! Error handling -- defined early so that other files can
243 ! throw errors before continuations are loaded
244 GENERIC: throw ( error -- * )
245
246 ERROR: assert got expect ;
247
248 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
249
250 <PRIVATE
251
252 : declare ( spec -- ) drop ;
253
254 : do-primitive ( number -- ) "Improper primitive call" throw ;
255
256 ! Special object count and identifiers must be kept in sync with:
257 !   vm/objects.hpp
258 !   basis/bootstrap/image/image.factor
259
260 CONSTANT: special-object-count 80
261
262 CONSTANT: OBJ-WALKER-HOOK 3
263
264 CONSTANT: OBJ-CALLCC-1 4
265
266 CONSTANT: ERROR-HANDLER-QUOT 5
267 CONSTANT: OBJ-ERROR 6
268
269 CONSTANT: OBJ-CELL-SIZE 7
270 CONSTANT: OBJ-CPU 8
271 CONSTANT: OBJ-OS 9
272
273 CONSTANT: OBJ-ARGS 10
274 CONSTANT: OBJ-STDIN 11
275 CONSTANT: OBJ-STDOUT 12
276
277 CONSTANT: OBJ-IMAGE 13
278 CONSTANT: OBJ-EXECUTABLE 14
279
280 CONSTANT: OBJ-EMBEDDED 15
281 CONSTANT: OBJ-EVAL-CALLBACK 16
282 CONSTANT: OBJ-YIELD-CALLBACK 17
283 CONSTANT: OBJ-SLEEP-CALLBACK 18
284
285 CONSTANT: OBJ-STARTUP-QUOT 20
286 CONSTANT: OBJ-GLOBAL 21
287 CONSTANT: OBJ-SHUTDOWN-QUOT 22
288
289 CONSTANT: JIT-PROLOG 23
290 CONSTANT: JIT-PRIMITIVE-WORD 24
291 CONSTANT: JIT-PRIMITIVE 25
292 CONSTANT: JIT-WORD-JUMP 26
293 CONSTANT: JIT-WORD-CALL 27
294 CONSTANT: JIT-IF-WORD 28
295 CONSTANT: JIT-IF 29
296 CONSTANT: JIT-SAFEPOINT 30
297 CONSTANT: JIT-EPILOG 31
298 CONSTANT: JIT-RETURN 32
299 CONSTANT: JIT-PROFILING 33
300 CONSTANT: JIT-PUSH-IMMEDIATE 34
301 CONSTANT: JIT-DIP-WORD 35
302 CONSTANT: JIT-DIP 36
303 CONSTANT: JIT-2DIP-WORD 37
304 CONSTANT: JIT-2DIP 38
305 CONSTANT: JIT-3DIP-WORD 39
306 CONSTANT: JIT-3DIP 40
307 CONSTANT: JIT-EXECUTE 41
308 CONSTANT: JIT-DECLARE-WORD 42
309
310 CONSTANT: C-TO-FACTOR-WORD 43
311 CONSTANT: LAZY-JIT-COMPILE-WORD 44
312 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
313 CONSTANT: GET-FPU-STATE-WORD 46
314 CONSTANT: SET-FPU-STATE-WORD 47
315 CONSTANT: SIGNAL-HANDLER-WORD 48
316 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
317 CONSTANT: FFI-SIGNAL-HANDLER-WORD 50
318 CONSTANT: FFI-LEAF-SIGNAL-HANDLER-WORD 51
319
320 CONSTANT: REDEFINITION-COUNTER 52
321
322 CONSTANT: CALLBACK-STUB 53
323
324 CONSTANT: PIC-LOAD 54
325 CONSTANT: PIC-TAG 55
326 CONSTANT: PIC-TUPLE 56
327 CONSTANT: PIC-CHECK-TAG 57
328 CONSTANT: PIC-CHECK-TUPLE 58
329 CONSTANT: PIC-HIT 59
330 CONSTANT: PIC-MISS-WORD 60
331 CONSTANT: PIC-MISS-TAIL-WORD 61
332
333 CONSTANT: MEGA-LOOKUP 62
334 CONSTANT: MEGA-LOOKUP-WORD 63
335 CONSTANT: MEGA-MISS-WORD 64
336
337 CONSTANT: OBJ-UNDEFINED 65
338
339 CONSTANT: OBJ-STDERR 66
340
341 CONSTANT: OBJ-STAGE2 67
342
343 CONSTANT: OBJ-CURRENT-THREAD 68
344
345 CONSTANT: OBJ-THREADS 69
346 CONSTANT: OBJ-RUN-QUEUE 70
347 CONSTANT: OBJ-SLEEP-QUEUE 71
348
349 CONSTANT: OBJ-VM-COMPILER 72
350
351 CONSTANT: OBJ-WAITING-CALLBACKS 73
352
353 CONSTANT: OBJ-SIGNAL-PIPE 74
354
355 ! Context object count and identifiers must be kept in sync with:
356 !   vm/contexts.hpp
357
358 CONSTANT: context-object-count 10
359
360 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
361 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
362 CONSTANT: CONTEXT-OBJ-CONTEXT 2
363 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
364
365 ! Runtime errors must be kept in sync with:
366 !   vm/errors.hpp
367
368 CONSTANT: kernel-error-count 20
369
370 CONSTANT: ERROR-EXPIRED 0
371 CONSTANT: ERROR-IO      1
372 CONSTANT: ERROR-NOT-IMPLEMENTED 2
373 CONSTANT: ERROR-TYPE 3
374 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
375 CONSTANT: ERROR-SIGNAL 5
376 CONSTANT: ERROR-ARRAY-SIZE 6
377 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
378 CONSTANT: ERROR-C-STRING 8
379 CONSTANT: ERROR-FFI 9
380 CONSTANT: ERROR-UNDEFINED-SYMBOL 10
381 CONSTANT: ERROR-DATASTACK-UNDERFLOW 11
382 CONSTANT: ERROR-DATASTACK-OVERFLOW 12
383 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 13
384 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 14
385 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 15
386 CONSTANT: ERROR-CALLSTACK-OVERFLOW 16
387 CONSTANT: ERROR-MEMORY 17
388 CONSTANT: ERROR-FP-TRAP 18
389 CONSTANT: ERROR-INTERRUPT 19
390
391 PRIVATE>