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