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 : roll ( x y z t -- y z t x ) [ rot ] dip swap ; inline
15 : -roll ( x y z t -- t x y z ) swap [ -rot ] dip ; inline
17 : 2over ( x y z -- x y z x y ) pick pick ; inline
19 : clear ( -- ) { } set-datastack ;
22 GENERIC: call ( callable -- )
24 GENERIC: execute ( word -- )
26 GENERIC: ?execute ( word -- value )
32 : ? ( ? true false -- true/false )
33 #! 'if' and '?' can be defined in terms of each other
34 #! because the JIT special-cases an 'if' preceeded by
35 #! two literal quotations.
36 rot [ drop ] [ nip ] if ; inline
38 : if ( ? true false -- ) ? call ;
41 : unless ( ? false -- )
42 swap [ drop ] [ call ] if ; inline
45 swap [ call ] [ drop ] if ; inline
48 : if* ( ? true false -- )
49 pick [ drop call ] [ 2nip call ] if ; inline
52 over [ call ] [ 2drop ] if ; inline
54 : unless* ( ? false -- )
55 over [ drop ] [ nip call ] if ; inline
58 : ?if ( default cond true false -- )
59 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
62 ! Not declared inline because the compiler special-cases them
64 : dip ( x quot -- x ) swap [ call ] dip ;
66 : 2dip ( x y quot -- x y ) -rot [ call ] 2dip ;
68 : 3dip ( x y z quot -- x y z ) -roll [ call ] 3dip ;
70 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
73 : keep ( x quot -- x ) over [ call ] dip ; inline
75 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
77 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; 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 ( pred: ( -- ? ) body: ( -- ) -- )
187 swap do compose [ loop ] curry when ; inline
189 : until ( pred: ( -- ? ) body: ( -- ) -- )
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 GENERIC: equal? ( obj1 obj2 -- ? )
203 M: object equal? 2drop f ; inline
205 TUPLE: identity-tuple ;
207 M: identity-tuple equal? 2drop f ; inline
209 : = ( obj1 obj2 -- ? )
210 2dup eq? [ 2drop t ] [
211 2dup both-fixnums? [ 2drop f ] [ equal? ] if
214 GENERIC: clone ( obj -- cloned )
216 M: object clone ; inline
218 M: callstack clone (clone) ; inline
221 GENERIC: new ( class -- tuple )
223 GENERIC: boa ( ... class -- tuple )
225 ! Error handling -- defined early so that other files can
226 ! throw errors before continuations are loaded
227 GENERIC: throw ( error -- * )
229 ERROR: assert got expect ;
231 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
235 : declare ( spec -- ) drop ;
237 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
239 : do-primitive ( number -- ) "Improper primitive call" throw ;