]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
Merge remote-tracking branch 'ajvondrak/graphviz'
[factor.git] / core / kernel / kernel.factor
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 IN: kernel
5
6 DEFER: dip
7 DEFER: 2dip
8 DEFER: 3dip
9
10 ! Stack stuff
11 : 2over ( x y z -- x y z x y ) pick pick ; inline
12
13 : clear ( -- ) { } set-datastack ;
14
15 ! Combinators
16 GENERIC: call ( callable -- )
17
18 GENERIC: execute ( word -- )
19
20 GENERIC: ?execute ( word -- value )
21
22 M: object ?execute ;
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 ( ..a ? true: ( ..a -- ..b ) false: ( ..a -- ..b ) -- ..b ) ? call ;
33
34 ! Single branch
35 : unless ( ..a ? false: ( ..a -- ..a ) -- ..a )
36     swap [ drop ] [ call ] if ; inline
37
38 : when ( ..a ? true: ( ..a -- ..a ) -- ..a )
39     swap [ call ] [ drop ] if ; inline
40
41 ! Anaphoric
42 : if* ( ..a ? true: ( ..a ? -- ..b ) false: ( ..a -- ..b ) -- ..b )
43     pick [ drop call ] [ 2nip call ] if ; inline
44
45 : when* ( ..a ? true: ( ..a ? -- ..a ) -- ..a )
46     over [ call ] [ 2drop ] if ; inline
47
48 : unless* ( ..a ? false: ( ..a -- ..a x ) -- ..a x )
49     over [ drop ] [ nip call ] if ; inline
50
51 ! Default
52 : ?if ( ..a default cond true: ( ..a cond -- ..b ) false: ( ..a default -- ..b ) -- ..b )
53     pick [ drop [ drop ] 2dip call ] [ 2nip call ] if ; inline
54
55 ! Dippers.
56 ! Not declared inline because the compiler special-cases them
57
58 : dip ( x quot -- x ) swap [ call ] dip ;
59
60 : 2dip ( x y quot -- x y ) swap [ dip ] dip ;
61
62 : 3dip ( x y z quot -- x y z ) swap [ 2dip ] dip ;
63
64 : 4dip ( w x y z quot -- w x y z ) swap [ 3dip ] dip ; inline
65
66 ! Keepers
67 : keep ( x quot -- x ) over [ call ] dip ; inline
68
69 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
70
71 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
72
73 ! Cleavers
74 : bi ( x p q -- )
75     [ keep ] dip call ; inline
76
77 : tri ( x p q r -- )
78     [ [ keep ] dip keep ] dip call ; inline
79
80 ! Double cleavers
81 : 2bi ( x y p q -- )
82     [ 2keep ] dip call ; inline
83
84 : 2tri ( x y p q r -- )
85     [ [ 2keep ] dip 2keep ] dip call ; inline
86
87 ! Triple cleavers
88 : 3bi ( x y z p q -- )
89     [ 3keep ] dip call ; inline
90
91 : 3tri ( x y z p q r -- )
92     [ [ 3keep ] dip 3keep ] dip call ; inline
93
94 ! Spreaders
95 : bi* ( x y p q -- )
96     [ dip ] dip call ; inline
97
98 : tri* ( x y z p q r -- )
99     [ [ 2dip ] dip dip ] dip call ; inline
100
101 ! Double spreaders
102 : 2bi* ( w x y z p q -- )
103     [ 2dip ] dip call ; inline
104
105 : 2tri* ( u v w x y z p q r -- )
106     [ 4dip ] 2dip 2bi* ; inline
107
108 ! Appliers
109 : bi@ ( x y quot -- )
110     dup bi* ; inline
111
112 : tri@ ( x y z quot -- )
113     dup dup tri* ; inline
114
115 ! Double appliers
116 : 2bi@ ( w x y z quot -- )
117     dup 2bi* ; inline
118
119 : 2tri@ ( u v w x y z quot -- )
120     dup dup 2tri* ; inline
121
122 ! Quotation building
123 : 2curry ( obj1 obj2 quot -- curry )
124     curry curry ; inline
125
126 : 3curry ( obj1 obj2 obj3 quot -- curry )
127     curry curry curry ; inline
128
129 : with ( param obj quot -- obj curry )
130     swapd [ swapd call ] 2curry ; inline
131
132 : prepose ( quot1 quot2 -- compose )
133     swap compose ; inline
134
135 ! Curried cleavers
136 <PRIVATE
137
138 : [curry] ( quot -- quot' ) [ curry ] curry ; inline
139
140 PRIVATE>
141
142 : bi-curry ( x p q -- p' q' ) [ [curry] ] bi@ bi ; inline
143
144 : tri-curry ( x p q r -- p' q' r' ) [ [curry] ] tri@ tri ; inline
145
146 : bi-curry* ( x y p q -- p' q' ) [ [curry] ] bi@ bi* ; inline
147
148 : tri-curry* ( x y z p q r -- p' q' r' ) [ [curry] ] tri@ tri* ; inline
149
150 : bi-curry@ ( x y q -- p' q' ) [curry] bi@ ; inline
151
152 : tri-curry@ ( x y z q -- p' q' r' ) [curry] tri@ ; inline
153
154 ! Booleans
155 UNION: boolean POSTPONE: t POSTPONE: f ;
156
157 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
158
159 : not ( obj -- ? ) [ f ] [ t ] if ; inline
160
161 : and ( obj1 obj2 -- ? ) over ? ; inline
162
163 : or ( obj1 obj2 -- ? ) dupd ? ; inline
164
165 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
166
167 : both? ( x y quot -- ? ) bi@ and ; inline
168
169 : either? ( x y quot -- ? ) bi@ or ; inline
170
171 : most ( x y quot -- z ) 2keep ? ; inline
172
173 ! Loops
174 : loop ( ... pred: ( ... -- ... ? ) -- ... )
175     [ call ] keep [ loop ] curry when ; inline recursive
176
177 : do ( pred body -- pred body )
178     dup 2dip ; inline
179
180 : while ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
181     swap do compose [ loop ] curry when ; inline
182
183 : until ( ..a pred: ( ..a -- ..b ? ) body: ( ..b -- ..a ) -- ..b )
184     [ [ not ] compose ] dip while ; inline
185
186 ! Object protocol
187 GENERIC: hashcode* ( depth obj -- code )
188
189 M: object hashcode* 2drop 0 ; inline
190
191 M: f hashcode* 2drop 31337 ; inline
192
193 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
194
195 : identity-hashcode ( obj -- code )
196     dup tag 0 eq? [
197         dup tag 1 eq? [ drop 0 ] [
198             dup (identity-hashcode) dup 0 eq? [
199                 drop dup compute-identity-hashcode
200                 (identity-hashcode)
201             ] [ nip ] if
202         ] if
203     ] unless ; inline
204
205 GENERIC: equal? ( obj1 obj2 -- ? )
206
207 M: object equal? 2drop f ; inline
208
209 TUPLE: identity-tuple ;
210
211 M: identity-tuple equal? 2drop f ; inline
212
213 M: identity-tuple hashcode* nip identity-hashcode ; inline
214
215 : = ( obj1 obj2 -- ? )
216     2dup eq? [ 2drop t ] [
217         2dup both-fixnums? [ 2drop f ] [ equal? ] if
218     ] if ; inline
219
220 GENERIC: clone ( obj -- cloned )
221
222 M: object clone ; inline
223
224 M: callstack clone (clone) ; inline
225
226 ! Tuple construction
227 GENERIC: new ( class -- tuple )
228
229 GENERIC: boa ( slots... class -- tuple )
230
231 ! Error handling -- defined early so that other files can
232 ! throw errors before continuations are loaded
233 GENERIC: throw ( error -- * )
234
235 ERROR: assert got expect ;
236
237 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
238
239 <PRIVATE
240
241 : declare ( spec -- ) drop ;
242
243 : do-primitive ( number -- ) "Improper primitive call" throw ;
244
245 PRIVATE>