]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
Refactor all usages of >r/r> in core to use dip, 2dip, 3dip
[factor.git] / core / kernel / kernel.factor
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 ;
4 IN: kernel
5
6 DEFER: dip
7 DEFER: 2dip
8 DEFER: 3dip
9
10 ! Stack stuff
11 : spin ( x y z -- z y x ) swap rot ; inline
12
13 : roll ( x y z t -- y z t x ) [ rot ] dip swap ; inline
14
15 : -roll ( x y z t -- t x y z ) swap [ -rot ] dip ; inline
16
17 : 2over ( x y z -- x y z x y ) pick pick ; inline
18
19 : clear ( -- ) { } set-datastack ;
20
21 ! Combinators
22 GENERIC: call ( callable -- )
23
24 DEFER: if
25
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
31
32 : if ( ? true false -- ) ? call ;
33
34 ! Single branch
35 : unless ( ? false -- )
36     swap [ drop ] [ call ] if ; inline
37
38 : when ( ? true -- )
39     swap [ call ] [ drop ] if ; inline
40
41 ! Anaphoric
42 : if* ( ? true false -- )
43     pick [ drop call ] [ 2nip call ] if ; inline
44
45 : when* ( ? true -- )
46     over [ call ] [ 2drop ] if ; inline
47
48 : unless* ( ? false -- )
49     over [ drop ] [ nip call ] if ; inline
50
51 ! Default
52 : ?if ( default cond true false -- )
53     pick [ roll 2drop call ] [ 2nip call ] if ; inline
54
55 ! Slippers
56 : slip ( quot x -- x ) [ call ] dip ;
57
58 : 2slip ( quot x y -- x y ) [ call ] 2dip ;
59
60 : 3slip ( quot x y z -- x y z ) [ call ] 3dip ;
61
62 : dip ( x quot -- x ) swap slip ; inline
63
64 : 2dip ( x y quot -- x y ) -rot 2slip ; inline
65
66 : 3dip ( x y z quot -- x y z ) -roll 3slip ; inline
67
68 ! Keepers
69 : keep ( x quot -- x ) over slip ; inline
70
71 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
72
73 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
74
75 ! Cleavers
76 : bi ( x p q -- )
77     [ keep ] dip call ; inline
78
79 : tri ( x p q r -- )
80     [ [ keep ] dip keep ] dip call ; inline
81
82 ! Double cleavers
83 : 2bi ( x y p q -- )
84     [ 2keep ] dip call ; inline
85
86 : 2tri ( x y p q r -- )
87     [ [ 2keep ] dip 2keep ] dip call ; inline
88
89 ! Triple cleavers
90 : 3bi ( x y z p q -- )
91     [ 3keep ] dip call ; inline
92
93 : 3tri ( x y z p q r -- )
94     [ [ 3keep ] dip 3keep ] dip call ; inline
95
96 ! Spreaders
97 : bi* ( x y p q -- )
98     [ dip ] dip call ; inline
99
100 : tri* ( x y z p q r -- )
101     [ [ 2dip ] dip dip ] dip call ; inline
102
103 ! Double spreaders
104 : 2bi* ( w x y z p q -- )
105     [ 2dip ] dip call ; inline
106
107 ! Appliers
108 : bi@ ( x y quot -- )
109     dup bi* ; inline
110
111 : tri@ ( x y z quot -- )
112     dup dup tri* ; inline
113
114 ! Double appliers
115 : 2bi@ ( w x y z quot -- )
116     dup 2bi* ; inline
117
118 : loop ( pred: ( -- ? ) -- )
119     dup slip swap [ loop ] [ drop ] if ; inline recursive
120
121 : while ( pred: ( -- ? ) body: ( -- ) tail: ( -- ) -- )
122     [ dup slip ] 2dip roll
123     [ [ tuck 2slip ] dip while ]
124     [ 2nip call ] if ; inline recursive
125
126 ! Object protocol
127 GENERIC: hashcode* ( depth obj -- code )
128
129 M: object hashcode* 2drop 0 ;
130
131 M: f hashcode* 2drop 31337 ;
132
133 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
134
135 GENERIC: equal? ( obj1 obj2 -- ? )
136
137 M: object equal? 2drop f ;
138
139 TUPLE: identity-tuple ;
140
141 M: identity-tuple equal? 2drop f ;
142
143 : = ( obj1 obj2 -- ? )
144     2dup eq? [ 2drop t ] [ equal? ] if ; inline
145
146 GENERIC: clone ( obj -- cloned )
147
148 M: object clone ;
149
150 M: callstack clone (clone) ;
151
152 ! Tuple construction
153 GENERIC: new ( class -- tuple )
154
155 GENERIC: boa ( ... class -- tuple )
156
157 ! Quotation building
158 : 2curry ( obj1 obj2 quot -- curry )
159     curry curry ; inline
160
161 : 3curry ( obj1 obj2 obj3 quot -- curry )
162     curry curry curry ; inline
163
164 : with ( param obj quot -- obj curry )
165     swapd [ swapd call ] 2curry ; inline
166
167 : prepose ( quot1 quot2 -- compose )
168     swap compose ; inline
169
170 : 3compose ( quot1 quot2 quot3 -- compose )
171     compose compose ; inline
172
173 ! Booleans
174 : not ( obj -- ? ) [ f ] [ t ] if ; inline
175
176 : and ( obj1 obj2 -- ? ) over ? ; inline
177
178 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
179
180 : or ( obj1 obj2 -- ? ) dupd ? ; inline
181
182 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
183
184 : both? ( x y quot -- ? ) bi@ and ; inline
185
186 : either? ( x y quot -- ? ) bi@ or ; inline
187
188 : most ( x y quot -- z )
189     [ 2dup ] dip call [ drop ] [ nip ] if ; inline
190
191 ! Error handling -- defined early so that other files can
192 ! throw errors before continuations are loaded
193 : throw ( error -- * ) 5 getenv [ die ] or 1 (throw) ;
194
195 ERROR: assert got expect ;
196
197 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
198
199 <PRIVATE
200
201 : declare ( spec -- ) drop ;
202
203 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
204
205 : do-primitive ( number -- ) "Improper primitive call" throw ;
206
207 PRIVATE>