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 : spin ( x y z -- z y x ) swap rot ; inline
13 : 2over ( x y z -- x y z x y ) pick pick ; inline
15 : clear ( -- ) { } set-datastack ;
18 GENERIC: call ( callable -- )
20 GENERIC: execute ( word -- )
22 GENERIC: ?execute ( word -- value )
28 : ? ( ? true false -- true/false )
29 #! 'if' and '?' can be defined in terms of each other
30 #! because the JIT special-cases an 'if' preceeded by
31 #! two literal quotations.
32 rot [ drop ] [ nip ] if ; inline
34 : if ( ? true false -- ) ? call ;
37 : unless ( ? false -- )
38 swap [ drop ] [ call ] if ; inline
41 swap [ call ] [ drop ] if ; inline
44 : if* ( ? true false -- )
45 pick [ drop call ] [ 2nip call ] if ; inline
48 over [ call ] [ 2drop ] if ; inline
50 : unless* ( ? false -- )
51 over [ drop ] [ nip call ] if ; inline
54 : ?if ( default cond true false -- )
55 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
58 ! Not declared inline because the compiler special-cases them
60 : dip ( x quot -- x ) swap [ call ] dip ;
62 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
64 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
66 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
69 : keep ( x quot -- x ) over [ call ] dip ; inline
71 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
73 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
77 [ keep ] dip call ; inline
80 [ [ keep ] dip keep ] dip call ; inline
84 [ 2keep ] dip call ; inline
86 : 2tri ( x y p q r -- )
87 [ [ 2keep ] dip 2keep ] dip call ; inline
90 : 3bi ( x y z p q -- )
91 [ 3keep ] dip call ; inline
93 : 3tri ( x y z p q r -- )
94 [ [ 3keep ] dip 3keep ] dip call ; inline
98 [ dip ] dip call ; inline
100 : tri* ( x y z p q r -- )
101 [ [ 2dip ] dip dip ] dip call ; inline
104 : 2bi* ( w x y z p q -- )
105 [ 2dip ] dip call ; inline
107 : 2tri* ( u v w x y z p q r -- )
108 [ 4dip ] 2dip 2bi* ; inline
111 : bi@ ( x y quot -- )
114 : tri@ ( x y z quot -- )
115 dup dup tri* ; inline
118 : 2bi@ ( w x y z quot -- )
121 : 2tri@ ( u v w x y z quot -- )
122 dup dup 2tri* ; inline
125 : 2curry ( obj1 obj2 quot -- curry )
128 : 3curry ( obj1 obj2 obj3 quot -- curry )
129 curry curry curry ; inline
131 : with ( param obj quot -- obj curry )
132 swapd [ swapd call ] 2curry ; inline
134 : prepose ( quot1 quot2 -- compose )
135 swap compose ; inline
140 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
144 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
146 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
148 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
150 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
152 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
154 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
157 UNION: boolean POSTPONE: t POSTPONE: f ;
159 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
161 : not ( obj -- ? ) [ f ] [ t ] if ; inline
163 : and ( obj1 obj2 -- ? ) over ? ; inline
165 : or ( obj1 obj2 -- ? ) dupd ? ; inline
167 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
169 : both? ( x y quot -- ? ) bi@ and ; inline
171 : either? ( x y quot -- ? ) bi@ or ; inline
173 : most ( x y quot -- z ) 2keep ? ; inline
176 : loop ( pred: ( -- ? ) -- )
177 [ call ] keep [ loop ] curry when ; inline recursive
179 : do ( pred body -- pred body )
182 : while ( pred: ( -- ? ) body: ( -- ) -- )
183 swap do compose [ loop ] curry when ; inline
185 : until ( pred: ( -- ? ) body: ( -- ) -- )
186 [ [ not ] compose ] dip while ; inline
189 GENERIC: hashcode* ( depth obj -- code )
191 M: object hashcode* 2drop 0 ; inline
193 M: f hashcode* 2drop 31337 ; inline
195 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
197 GENERIC: equal? ( obj1 obj2 -- ? )
199 M: object equal? 2drop f ; inline
201 TUPLE: identity-tuple ;
203 M: identity-tuple equal? 2drop f ; inline
205 : = ( obj1 obj2 -- ? )
206 2dup eq? [ 2drop t ] [
207 2dup both-fixnums? [ 2drop f ] [ equal? ] if
210 GENERIC: clone ( obj -- cloned )
212 M: object clone ; inline
214 M: callstack clone (clone) ; inline
217 GENERIC: new ( class -- tuple )
219 GENERIC: boa ( ... class -- tuple )
221 ! Error handling -- defined early so that other files can
222 ! throw errors before continuations are loaded
223 GENERIC: throw ( error -- * )
225 ERROR: assert got expect ;
227 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
231 : declare ( spec -- ) drop ;
233 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
235 : do-primitive ( number -- ) "Improper primitive call" throw ;