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 ;
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 -- )
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 [ roll 2drop call ] [ 2nip call ] if ; inline
55 ! Slippers and dippers.
56 ! Not declared inline because the compiler special-cases them
58 : slip ( quot x -- x )
59 #! 'slip' and 'dip' can be defined in terms of each other
60 #! because the JIT special-cases a 'dip' preceeded by
61 #! a literal quotation.
64 : 2slip ( quot x y -- x y )
65 #! '2slip' and '2dip' can be defined in terms of each other
66 #! because the JIT special-cases a '2dip' preceeded by
67 #! a literal quotation.
70 : 3slip ( quot x y z -- x y z )
71 #! '3slip' and '3dip' can be defined in terms of each other
72 #! because the JIT special-cases a '3dip' preceeded by
73 #! a literal quotation.
76 : dip ( x quot -- x ) swap slip ;
78 : 2dip ( x y quot -- x y ) -rot 2slip ;
80 : 3dip ( x y z quot -- x y z ) -roll 3slip ;
82 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
85 : keep ( x quot -- x ) over slip ; inline
87 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
89 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
93 [ keep ] dip call ; inline
96 [ [ keep ] dip keep ] dip call ; inline
100 [ 2keep ] dip call ; inline
102 : 2tri ( x y p q r -- )
103 [ [ 2keep ] dip 2keep ] dip call ; inline
106 : 3bi ( x y z p q -- )
107 [ 3keep ] dip call ; inline
109 : 3tri ( x y z p q r -- )
110 [ [ 3keep ] dip 3keep ] dip call ; inline
114 [ dip ] dip call ; inline
116 : tri* ( x y z p q r -- )
117 [ [ 2dip ] dip dip ] dip call ; inline
120 : 2bi* ( w x y z p q -- )
121 [ 2dip ] dip call ; inline
123 : 2tri* ( u v w x y z p q r -- )
124 [ 4dip ] 2dip 2bi* ; inline
127 : bi@ ( x y quot -- )
130 : tri@ ( x y z quot -- )
131 dup dup tri* ; inline
134 : 2bi@ ( w x y z quot -- )
137 : 2tri@ ( u v w y x z quot -- )
138 dup dup 2tri* ; inline
141 GENERIC: hashcode* ( depth obj -- code )
143 M: object hashcode* 2drop 0 ;
145 M: f hashcode* 2drop 31337 ;
147 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
149 GENERIC: equal? ( obj1 obj2 -- ? )
151 M: object equal? 2drop f ;
153 TUPLE: identity-tuple ;
155 M: identity-tuple equal? 2drop f ;
158 : = ( obj1 obj2 -- ? )
159 2dup eq? [ 2drop t ] [
160 2dup both-fixnums? [ 2drop f ] [ equal? ] if
163 GENERIC: clone ( obj -- cloned )
167 M: callstack clone (clone) ;
170 GENERIC: new ( class -- tuple )
172 GENERIC: boa ( ... class -- tuple )
175 : 2curry ( obj1 obj2 quot -- curry )
178 : 3curry ( obj1 obj2 obj3 quot -- curry )
179 curry curry curry ; inline
181 : with ( param obj quot -- obj curry )
182 swapd [ swapd call ] 2curry ; inline
184 : prepose ( quot1 quot2 -- compose )
185 swap compose ; inline
188 : not ( obj -- ? ) [ f ] [ t ] if ; inline
190 : and ( obj1 obj2 -- ? ) over ? ; inline
192 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
194 : or ( obj1 obj2 -- ? ) dupd ? ; inline
196 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
198 : both? ( x y quot -- ? ) bi@ and ; inline
200 : either? ( x y quot -- ? ) bi@ or ; inline
202 : most ( x y quot -- z )
203 [ 2dup ] dip call [ drop ] [ nip ] if ; inline
206 : loop ( pred: ( -- ? ) -- )
207 dup slip swap [ loop ] [ drop ] if ; inline recursive
209 : do ( pred body tail -- pred body tail )
212 : while ( pred: ( -- ? ) body: ( -- ) tail: ( -- ) -- )
213 [ pick 3dip [ do while ] 3curry ] keep if ; inline recursive
215 : until ( pred: ( -- ? ) body: ( -- ) tail: ( -- ) -- )
216 [ [ not ] compose ] 2dip while ; inline
218 ! Error handling -- defined early so that other files can
219 ! throw errors before continuations are loaded
220 : throw ( error -- * ) 5 getenv [ die ] or 1 (throw) ;
222 ERROR: assert got expect ;
224 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
228 : declare ( spec -- ) drop ;
230 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
232 : do-primitive ( number -- ) "Improper primitive call" throw ;