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 ;
11 : 2over ( x y z -- x y z x y ) pick pick ; inline
13 : clear ( -- ) { } set-datastack ;
16 GENERIC: call ( callable -- )
18 GENERIC: execute ( word -- )
20 GENERIC: ?execute ( word -- value )
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 ( ? true false -- ) ? call ;
35 : unless ( ? false -- )
36 swap [ drop ] [ call ] if ; inline
39 swap [ call ] [ drop ] if ; inline
42 : if* ( ? true false -- )
43 pick [ drop call ] [ 2nip call ] if ; inline
46 over [ call ] [ 2drop ] if ; inline
48 : unless* ( ? false -- )
49 over [ drop ] [ nip call ] if ; inline
52 : ?if ( default cond true false -- )
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 ( x quot -- x ) over [ call ] dip ; inline
69 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
71 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
75 [ keep ] dip call ; inline
78 [ [ keep ] dip keep ] dip call ; inline
82 [ 2keep ] dip call ; inline
84 : 2tri ( x y p q r -- )
85 [ [ 2keep ] dip 2keep ] dip call ; inline
88 : 3bi ( x y z p q -- )
89 [ 3keep ] dip call ; inline
91 : 3tri ( x y z p q r -- )
92 [ [ 3keep ] dip 3keep ] dip call ; inline
96 [ dip ] dip call ; inline
98 : tri* ( x y z p q r -- )
99 [ [ 2dip ] dip dip ] dip call ; inline
102 : 2bi* ( w x y z p q -- )
103 [ 2dip ] dip call ; inline
105 : 2tri* ( u v w x y z p q r -- )
106 [ 4dip ] 2dip 2bi* ; inline
109 : bi@ ( x y quot -- )
112 : tri@ ( x y z quot -- )
113 dup dup tri* ; inline
116 : 2bi@ ( w x y z quot -- )
119 : 2tri@ ( u v w x y z quot -- )
120 dup dup 2tri* ; inline
123 : 2curry ( obj1 obj2 quot -- curry )
126 : 3curry ( obj1 obj2 obj3 quot -- curry )
127 curry curry curry ; inline
129 : with ( param obj quot -- obj curry )
130 swapd [ swapd call ] 2curry ; inline
132 : prepose ( quot1 quot2 -- compose )
133 swap compose ; inline
138 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
142 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
144 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
146 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
148 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
150 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
152 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
155 UNION: boolean POSTPONE: t POSTPONE: f ;
157 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
159 : not ( obj -- ? ) [ f ] [ t ] if ; inline
161 : and ( obj1 obj2 -- ? ) over ? ; inline
163 : or ( obj1 obj2 -- ? ) dupd ? ; inline
165 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
167 : both? ( x y quot -- ? ) bi@ and ; inline
169 : either? ( x y quot -- ? ) bi@ or ; inline
171 : most ( x y quot -- z ) 2keep ? ; inline
174 : loop ( pred: ( -- ? ) -- )
175 [ call ] keep [ loop ] curry when ; inline recursive
177 : do ( pred body -- pred body )
180 : while ( pred: ( -- ? ) body: ( -- ) -- )
181 swap do compose [ loop ] curry when ; inline
183 : until ( pred: ( -- ? ) body: ( -- ) -- )
184 [ [ not ] compose ] dip while ; inline
187 GENERIC: hashcode* ( depth obj -- code )
189 M: object hashcode* 2drop 0 ; inline
191 M: f hashcode* 2drop 31337 ; inline
193 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
195 : identity-hashcode ( obj -- code )
197 dup tag 1 eq? [ drop 0 ] [
198 dup (identity-hashcode) dup 0 eq? [
199 drop dup compute-identity-hashcode
205 GENERIC: equal? ( obj1 obj2 -- ? )
207 M: object equal? 2drop f ; inline
209 TUPLE: identity-tuple ;
211 M: identity-tuple equal? 2drop f ; inline
213 M: identity-tuple hashcode* nip identity-hashcode ; inline
215 : = ( obj1 obj2 -- ? )
216 2dup eq? [ 2drop t ] [
217 2dup both-fixnums? [ 2drop f ] [ equal? ] if
220 GENERIC: clone ( obj -- cloned )
222 M: object clone ; inline
224 M: callstack clone (clone) ; inline
227 GENERIC: new ( class -- tuple )
229 GENERIC: boa ( ... class -- tuple )
231 ! Error handling -- defined early so that other files can
232 ! throw errors before continuations are loaded
233 GENERIC: throw ( error -- * )
235 ERROR: assert got expect ;
237 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
241 : declare ( spec -- ) drop ;
243 : do-primitive ( number -- ) "Improper primitive call" throw ;