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
4 classes.tuple.private ;
12 : spin ( x y z -- z y x ) swap rot ; inline
14 : roll ( x y z t -- y z t x ) [ rot ] dip swap ; inline
16 : -roll ( x y z t -- t x y z ) swap [ -rot ] dip ; inline
18 : 2over ( x y z -- x y z x y ) pick pick ; inline
20 : clear ( -- ) { } set-datastack ;
23 GENERIC: call ( callable -- )
25 GENERIC: execute ( word -- )
29 : ? ( ? true false -- true/false )
30 #! 'if' and '?' can be defined in terms of each other
31 #! because the JIT special-cases an 'if' preceeded by
32 #! two literal quotations.
33 rot [ drop ] [ nip ] if ; inline
35 : if ( ? true false -- ) ? call ;
38 : unless ( ? false -- )
39 swap [ drop ] [ call ] if ; inline
42 swap [ call ] [ drop ] if ; inline
45 : if* ( ? true false -- )
46 pick [ drop call ] [ 2nip call ] if ; inline
49 over [ call ] [ 2drop ] if ; inline
51 : unless* ( ? false -- )
52 over [ drop ] [ nip call ] if ; inline
55 : ?if ( default cond true false -- )
56 pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
58 ! Slippers and dippers.
59 ! Not declared inline because the compiler special-cases them
61 : slip ( quot x -- x )
62 #! 'slip' and 'dip' can be defined in terms of each other
63 #! because the JIT special-cases a 'dip' preceeded by
64 #! a literal quotation.
67 : 2slip ( quot x y -- x y )
68 #! '2slip' and '2dip' can be defined in terms of each other
69 #! because the JIT special-cases a '2dip' preceeded by
70 #! a literal quotation.
73 : 3slip ( quot x y z -- x y z )
74 #! '3slip' and '3dip' can be defined in terms of each other
75 #! because the JIT special-cases a '3dip' preceeded by
76 #! a literal quotation.
79 : dip ( x quot -- x ) swap slip ;
81 : 2dip ( x y quot -- x y ) -rot 2slip ;
83 : 3dip ( x y z quot -- x y z ) -roll 3slip ;
85 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
88 : keep ( x quot -- x ) over slip ; inline
90 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
92 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
96 [ keep ] dip call ; inline
99 [ [ keep ] dip keep ] dip call ; inline
103 [ 2keep ] dip call ; inline
105 : 2tri ( x y p q r -- )
106 [ [ 2keep ] dip 2keep ] dip call ; inline
109 : 3bi ( x y z p q -- )
110 [ 3keep ] dip call ; inline
112 : 3tri ( x y z p q r -- )
113 [ [ 3keep ] dip 3keep ] dip call ; inline
117 [ dip ] dip call ; inline
119 : tri* ( x y z p q r -- )
120 [ [ 2dip ] dip dip ] dip call ; inline
123 : 2bi* ( w x y z p q -- )
124 [ 2dip ] dip call ; inline
126 : 2tri* ( u v w x y z p q r -- )
127 [ 4dip ] 2dip 2bi* ; inline
130 : bi@ ( x y quot -- )
133 : tri@ ( x y z quot -- )
134 dup dup tri* ; inline
137 : 2bi@ ( w x y z quot -- )
140 : 2tri@ ( u v w y x z quot -- )
141 dup dup 2tri* ; inline
144 : 2curry ( obj1 obj2 quot -- curry )
147 : 3curry ( obj1 obj2 obj3 quot -- curry )
148 curry curry curry ; inline
150 : with ( param obj quot -- obj curry )
151 swapd [ swapd call ] 2curry ; inline
153 : prepose ( quot1 quot2 -- compose )
154 swap compose ; inline
159 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
163 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
165 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
167 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
169 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
171 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
173 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
176 : not ( obj -- ? ) [ f ] [ t ] if ; inline
178 : and ( obj1 obj2 -- ? ) over ? ; inline
180 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
182 : or ( obj1 obj2 -- ? ) dupd ? ; inline
184 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
186 : both? ( x y quot -- ? ) bi@ and ; inline
188 : either? ( x y quot -- ? ) bi@ or ; inline
190 : most ( x y quot -- z ) 2keep ? ; inline
193 : loop ( pred: ( -- ? ) -- )
194 [ call ] keep [ loop ] curry when ; inline recursive
196 : do ( pred body -- pred body )
199 : while ( pred: ( -- ? ) body: ( -- ) -- )
200 swap do compose [ loop ] curry when ; inline
202 : until ( pred: ( -- ? ) body: ( -- ) -- )
203 [ [ not ] compose ] dip while ; inline
206 GENERIC: hashcode* ( depth obj -- code )
208 M: object hashcode* 2drop 0 ;
210 M: f hashcode* 2drop 31337 ;
212 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
214 GENERIC: equal? ( obj1 obj2 -- ? )
216 M: object equal? 2drop f ;
218 TUPLE: identity-tuple ;
220 M: identity-tuple equal? 2drop f ;
222 : = ( obj1 obj2 -- ? )
223 2dup eq? [ 2drop t ] [
224 2dup both-fixnums? [ 2drop f ] [ equal? ] if
227 GENERIC: clone ( obj -- cloned )
231 M: callstack clone (clone) ;
234 GENERIC: new ( class -- tuple )
236 GENERIC: boa ( ... class -- tuple )
238 ! Error handling -- defined early so that other files can
239 ! throw errors before continuations are loaded
240 GENERIC: throw ( error -- * )
242 ERROR: assert got expect ;
244 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
248 : declare ( spec -- ) drop ;
250 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
252 : do-primitive ( number -- ) "Improper primitive call" throw ;