1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel.private slots.private classes.tuple.private ;
7 : spin ( x y z -- z y x ) swap rot ; inline
9 : roll ( x y z t -- y z t x ) >r rot r> swap ; inline
11 : -roll ( x y z t -- t x y z ) swap >r -rot r> ; inline
13 : 2over ( x y z -- x y z x y ) pick pick ; inline
15 : clear ( -- ) { } set-datastack ;
18 GENERIC: call ( callable -- )
22 : ? ( ? true false -- true/false )
23 #! 'if' and '?' can be defined in terms of each other
24 #! because the JIT special-cases an 'if' preceeded by
25 #! two literal quotations.
26 rot [ drop ] [ nip ] if ; inline
28 : if ( ? true false -- ) ? call ;
31 : unless ( ? false -- )
32 swap [ drop ] [ call ] if ; inline
35 swap [ call ] [ drop ] if ; inline
38 : if* ( ? true false -- )
39 pick [ drop call ] [ 2nip call ] if ; inline
42 over [ call ] [ 2drop ] if ; inline
44 : unless* ( ? false -- )
45 over [ drop ] [ nip call ] if ; inline
48 : ?if ( default cond true false -- )
49 pick [ roll 2drop call ] [ 2nip call ] if ; inline
52 : slip ( quot x -- x ) >r call r> ; inline
54 : 2slip ( quot x y -- x y ) >r >r call r> r> ; inline
56 : 3slip ( quot x y z -- x y z ) >r >r >r call r> r> r> ; inline
58 : dip ( obj quot -- obj ) swap slip ; inline
60 : 2dip ( obj1 obj2 quot -- obj1 obj2 ) -rot 2slip ; inline
63 : keep ( x quot -- x ) over slip ; inline
65 : 2keep ( x y quot -- x y ) 2over 2slip ; inline
67 : 3keep ( x y z quot -- x y z )
68 >r 3dup r> -roll 3slip ; inline
72 >r keep r> call ; inline
75 >r >r keep r> keep r> call ; inline
79 >r 2keep r> call ; inline
81 : 2tri ( x y p q r -- )
82 >r >r 2keep r> 2keep r> call ; inline
85 : 3bi ( x y z p q -- )
86 >r 3keep r> call ; inline
88 : 3tri ( x y z p q r -- )
89 >r >r 3keep r> 3keep r> call ; inline
93 >r dip r> call ; inline
95 : tri* ( x y z p q r -- )
96 >r >r 2dip r> dip r> call ; inline
99 : 2bi* ( w x y z p q -- )
100 >r 2dip r> call ; inline
103 : bi@ ( x y quot -- )
106 : tri@ ( x y z quot -- )
107 dup dup tri* ; inline
110 : 2bi@ ( w x y z quot -- )
113 : while ( pred body tail -- )
114 >r >r dup slip r> r> roll
115 [ >r tuck 2slip r> while ]
116 [ 2nip call ] if ; inline
119 GENERIC: hashcode* ( depth obj -- code )
121 M: object hashcode* 2drop 0 ;
123 M: f hashcode* 2drop 31337 ;
125 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
127 GENERIC: equal? ( obj1 obj2 -- ? )
129 M: object equal? 2drop f ;
131 TUPLE: identity-tuple ;
133 M: identity-tuple equal? 2drop f ;
135 : = ( obj1 obj2 -- ? )
136 2dup eq? [ 2drop t ] [ equal? ] if ; inline
138 GENERIC: clone ( obj -- cloned )
142 M: callstack clone (clone) ;
145 : new ( class -- tuple )
146 tuple-layout <tuple> ;
148 : boa ( ... class -- tuple )
149 tuple-layout <tuple-boa> ;
152 : 2curry ( obj1 obj2 quot -- curry )
155 : 3curry ( obj1 obj2 obj3 quot -- curry )
156 curry curry curry ; inline
158 : with ( param obj quot -- obj curry )
159 swapd [ swapd call ] 2curry ; inline
161 : prepose ( quot1 quot2 -- compose )
162 swap compose ; inline
164 : 3compose ( quot1 quot2 quot3 -- compose )
165 compose compose ; inline
168 : not ( obj -- ? ) f eq? ; inline
170 : >boolean ( obj -- ? ) t f ? ; inline
172 : and ( obj1 obj2 -- ? ) over ? ; inline
174 : or ( obj1 obj2 -- ? ) dupd ? ; inline
176 : xor ( obj1 obj2 -- ? ) dup not swap ? ; inline
178 : both? ( x y quot -- ? ) bi@ and ; inline
180 : either? ( x y quot -- ? ) bi@ or ; inline
182 : most ( x y quot -- z )
183 >r 2dup r> call [ drop ] [ nip ] if ; inline
185 ! Error handling -- defined early so that other files can
186 ! throw errors before continuations are loaded
187 : throw ( error -- * ) 5 getenv [ die ] or 1 (throw) ;
191 : hi-tag ( obj -- n ) 0 slot ; inline
193 : declare ( spec -- ) drop ;
195 : do-primitive ( number -- ) "Improper primitive call" throw ;
200 GENERIC: delegate ( obj -- delegate )
202 M: tuple delegate 2 slot ;
204 M: object delegate drop f ;
206 GENERIC: set-delegate ( delegate tuple -- )
208 M: tuple set-delegate 2 set-slot ;
210 GENERIC# get-slots 1 ( tuple slots -- ... )
212 GENERIC# set-slots 1 ( ... tuple slots -- )
214 : construct ( ... slots class -- tuple )
215 new [ swap set-slots ] keep ; inline
217 : construct-delegate ( delegate class -- tuple )
218 >r { set-delegate } r> construct ; inline