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 ;
8 BUILTIN: wrapper { wrapped read-only } ;
15 : 2over ( x y z -- x y z x y ) pick pick ; inline
17 : clear ( -- ) { } set-datastack ;
20 GENERIC: call ( callable -- )
22 GENERIC: execute ( word -- )
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
32 : if ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
35 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
36 swap [ drop ] [ call ] if ; inline
38 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
39 swap [ call ] [ drop ] if ; inline
42 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
43 pick [ drop call ] [ 2nip call ] if ; inline
45 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
46 over [ call ] [ 2drop ] if ; inline
48 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
49 over [ drop ] [ nip call ] if ; inline
52 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
53 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
56 ! Not declared inline because the compiler special-cases them
58 : dip ( x quot -- x ) swap [ call ] dip ;
60 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
62 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
64 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
67 : keep ( ..a x quot: ( ..a x -- ..b ) -- ..b x )
68 over [ call ] dip ; inline
70 : 2keep ( ..a x y quot: ( ..a x y -- ..b ) -- ..b x y )
71 [ 2dup ] dip 2dip ; inline
73 : 3keep ( ..a x y z quot: ( ..a x y z -- ..b ) -- ..b x y z )
74 [ 3dup ] dip 3dip ; inline
76 : 4keep ( ..a w x y z quot: ( ..a w x y z -- ..b ) -- ..b w x y z )
77 [ 4dup ] dip 4dip ; inline
81 [ keep ] dip call ; inline
84 [ [ keep ] dip keep ] dip call ; inline
88 [ 2keep ] dip call ; inline
90 : 2tri ( x y p q r -- )
91 [ [ 2keep ] dip 2keep ] dip call ; inline
94 : 3bi ( x y z p q -- )
95 [ 3keep ] dip call ; inline
97 : 3tri ( x y z p q r -- )
98 [ [ 3keep ] dip 3keep ] dip call ; inline
102 [ dip ] dip call ; inline
104 : tri* ( x y z p q r -- )
105 [ [ 2dip ] dip dip ] dip call ; inline
108 : 2bi* ( w x y z p q -- )
109 [ 2dip ] dip call ; inline
111 : 2tri* ( u v w x y z p q r -- )
112 [ 4dip ] 2dip 2bi* ; inline
115 : bi@ ( x y quot -- )
118 : tri@ ( x y z quot -- )
119 dup dup tri* ; inline
122 : 2bi@ ( w x y z quot -- )
125 : 2tri@ ( u v w x y z quot -- )
126 dup dup 2tri* ; inline
129 : 2curry ( obj1 obj2 quot -- curry )
132 : 3curry ( obj1 obj2 obj3 quot -- curry )
133 curry curry curry ; inline
135 : with ( param obj quot -- obj curry )
136 swapd [ swapd call ] 2curry ; inline
138 : prepose ( quot1 quot2 -- compose )
139 swap compose ; inline
144 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
148 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
150 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
152 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
154 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
156 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
158 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
161 UNION: boolean POSTPONE: t POSTPONE: f ;
163 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
165 : not ( obj -- ? ) [ f ] [ t ] if ; inline
167 : and ( obj1 obj2 -- ? ) over ? ; inline
169 : or ( obj1 obj2 -- ? ) dupd ? ; inline
171 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
173 : both? ( x y quot -- ? ) bi@ and ; inline
175 : either? ( x y quot -- ? ) bi@ or ; inline
177 : most ( x y quot -- z ) 2keep ? ; inline
180 : loop ( ... pred: ( ... -- ... ? ) -- ... )
181 [ call ] keep [ loop ] curry when ; inline recursive
183 : do ( pred body -- pred body )
186 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
187 swap do compose [ loop ] curry when ; inline
189 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
190 [ [ not ] compose ] dip while ; inline
193 GENERIC: hashcode* ( depth obj -- code )
195 M: object hashcode* 2drop 0 ; inline
197 M: f hashcode* 2drop 31337 ; inline
199 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
201 : identity-hashcode ( obj -- code )
203 dup tag 1 eq? [ drop 0 ] [
204 dup (identity-hashcode) dup 0 eq? [
205 drop dup compute-identity-hashcode
211 GENERIC: equal? ( obj1 obj2 -- ? )
213 M: object equal? 2drop f ; inline
215 TUPLE: identity-tuple ;
217 M: identity-tuple equal? 2drop f ; inline
219 M: identity-tuple hashcode* nip identity-hashcode ; inline
221 : = ( obj1 obj2 -- ? )
222 2dup eq? [ 2drop t ] [
223 2dup both-fixnums? [ 2drop f ] [ equal? ] if
226 : same? ( x y quot -- ? ) bi@ = ; inline
228 GENERIC: clone ( obj -- cloned )
230 M: object clone ; inline
232 M: callstack clone (clone) ; inline
235 GENERIC: new ( class -- tuple )
237 GENERIC: boa ( slots... class -- tuple )
239 ! Error handling -- defined early so that other files can
240 ! throw errors before continuations are loaded
241 GENERIC: throw ( error -- * )
243 ERROR: assert got expect ;
245 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
249 : declare ( spec -- ) drop ;
251 : do-primitive ( number -- ) "Improper primitive call" throw ;
253 ! Special object count and identifiers must be kept in sync with:
255 ! basis/bootstrap/image/image.factor
257 CONSTANT: special-object-count 80
259 CONSTANT: OBJ-WALKER-HOOK 3
261 CONSTANT: OBJ-CALLCC-1 4
263 CONSTANT: ERROR-HANDLER-QUOT 5
264 CONSTANT: OBJ-ERROR 6
266 CONSTANT: OBJ-CELL-SIZE 7
270 CONSTANT: OBJ-ARGS 10
271 CONSTANT: OBJ-STDIN 11
272 CONSTANT: OBJ-STDOUT 12
274 CONSTANT: OBJ-IMAGE 13
275 CONSTANT: OBJ-EXECUTABLE 14
277 CONSTANT: OBJ-EMBEDDED 15
278 CONSTANT: OBJ-EVAL-CALLBACK 16
279 CONSTANT: OBJ-YIELD-CALLBACK 17
280 CONSTANT: OBJ-SLEEP-CALLBACK 18
282 CONSTANT: OBJ-STARTUP-QUOT 20
283 CONSTANT: OBJ-GLOBAL 21
284 CONSTANT: OBJ-SHUTDOWN-QUOT 22
286 CONSTANT: JIT-PROLOG 23
287 CONSTANT: JIT-PRIMITIVE-WORD 24
288 CONSTANT: JIT-PRIMITIVE 25
289 CONSTANT: JIT-WORD-JUMP 26
290 CONSTANT: JIT-WORD-CALL 27
291 CONSTANT: JIT-IF-WORD 28
293 CONSTANT: JIT-SAFEPOINT 30
294 CONSTANT: JIT-EPILOG 31
295 CONSTANT: JIT-RETURN 32
296 CONSTANT: JIT-PROFILING 33
297 CONSTANT: JIT-PUSH-IMMEDIATE 34
298 CONSTANT: JIT-DIP-WORD 35
300 CONSTANT: JIT-2DIP-WORD 37
301 CONSTANT: JIT-2DIP 38
302 CONSTANT: JIT-3DIP-WORD 39
303 CONSTANT: JIT-3DIP 40
304 CONSTANT: JIT-EXECUTE 41
305 CONSTANT: JIT-DECLARE-WORD 42
307 CONSTANT: C-TO-FACTOR-WORD 43
308 CONSTANT: LAZY-JIT-COMPILE-WORD 44
309 CONSTANT: UNWIND-NATIVE-FRAMES-WORD 45
310 CONSTANT: GET-FPU-STATE-WORD 46
311 CONSTANT: SET-FPU-STATE-WORD 47
312 CONSTANT: SIGNAL-HANDLER-WORD 48
313 CONSTANT: LEAF-SIGNAL-HANDLER-WORD 49
314 CONSTANT: FFI-SIGNAL-HANDLER-WORD 50
315 CONSTANT: FFI-LEAF-SIGNAL-HANDLER-WORD 51
317 CONSTANT: REDEFINITION-COUNTER 52
319 CONSTANT: CALLBACK-STUB 53
321 CONSTANT: PIC-LOAD 54
323 CONSTANT: PIC-TUPLE 56
324 CONSTANT: PIC-CHECK-TAG 57
325 CONSTANT: PIC-CHECK-TUPLE 58
327 CONSTANT: PIC-MISS-WORD 60
328 CONSTANT: PIC-MISS-TAIL-WORD 61
330 CONSTANT: MEGA-LOOKUP 62
331 CONSTANT: MEGA-LOOKUP-WORD 63
332 CONSTANT: MEGA-MISS-WORD 64
334 CONSTANT: OBJ-UNDEFINED 65
336 CONSTANT: OBJ-STDERR 66
338 CONSTANT: OBJ-STAGE2 67
340 CONSTANT: OBJ-CURRENT-THREAD 68
342 CONSTANT: OBJ-THREADS 69
343 CONSTANT: OBJ-RUN-QUEUE 70
344 CONSTANT: OBJ-SLEEP-QUEUE 71
346 CONSTANT: OBJ-VM-COMPILER 72
348 CONSTANT: OBJ-WAITING-CALLBACKS 73
350 CONSTANT: OBJ-SIGNAL-PIPE 74
352 ! Context object count and identifiers must be kept in sync with:
355 CONSTANT: context-object-count 10
357 CONSTANT: CONTEXT-OBJ-NAMESTACK 0
358 CONSTANT: CONTEXT-OBJ-CATCHSTACK 1
359 CONSTANT: CONTEXT-OBJ-CONTEXT 2
360 CONSTANT: CONTEXT-OBJ-IN-CALLBACK-P 3
362 ! Runtime errors must be kept in sync with:
365 CONSTANT: kernel-error-count 20
367 CONSTANT: ERROR-EXPIRED 0
369 CONSTANT: ERROR-NOT-IMPLEMENTED 2
370 CONSTANT: ERROR-TYPE 3
371 CONSTANT: ERROR-DIVIDE-BY-ZERO 4
372 CONSTANT: ERROR-SIGNAL 5
373 CONSTANT: ERROR-ARRAY-SIZE 6
374 CONSTANT: ERROR-OUT-OF-FIXNUM-RANGE 7
375 CONSTANT: ERROR-C-STRING 8
376 CONSTANT: ERROR-FFI 9
377 CONSTANT: ERROR-UNDEFINED-SYMBOL 10
378 CONSTANT: ERROR-DATASTACK-UNDERFLOW 11
379 CONSTANT: ERROR-DATASTACK-OVERFLOW 12
380 CONSTANT: ERROR-RETAINSTACK-UNDERFLOW 13
381 CONSTANT: ERROR-RETAINSTACK-OVERFLOW 14
382 CONSTANT: ERROR-CALLSTACK-UNDERFLOW 15
383 CONSTANT: ERROR-CALLSTACK-OVERFLOW 16
384 CONSTANT: ERROR-MEMORY 17
385 CONSTANT: ERROR-FP-TRAP 18
386 CONSTANT: ERROR-INTERRUPT 19