]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
Merge branch 'master' into experimental (untested!)
[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 and dippers.
56 ! Not declared inline because the compiler special-cases them
57
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.
62     [ call ] dip ;
63
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.
68     [ call ] 2dip ;
69
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.
74     [ call ] 3dip ;
75
76 : dip ( x quot -- x ) swap slip ;
77
78 : 2dip ( x y quot -- x y ) -rot 2slip ;
79
80 : 3dip ( x y z quot -- x y z ) -roll 3slip ;
81
82 ! Keepers
83 : keep ( x quot -- x ) over slip ; inline
84
85 : 2keep ( x y quot -- x y ) [ 2dup ] dip 2dip ; inline
86
87 : 3keep ( x y z quot -- x y z ) [ 3dup ] dip 3dip ; inline
88
89 ! Cleavers
90 : bi ( x p q -- )
91     [ keep ] dip call ; inline
92
93 : tri ( x p q r -- )
94     [ [ keep ] dip keep ] dip call ; inline
95
96 ! Double cleavers
97 : 2bi ( x y p q -- )
98     [ 2keep ] dip call ; inline
99
100 : 2tri ( x y p q r -- )
101     [ [ 2keep ] dip 2keep ] dip call ; inline
102
103 ! Triple cleavers
104 : 3bi ( x y z p q -- )
105     [ 3keep ] dip call ; inline
106
107 : 3tri ( x y z p q r -- )
108     [ [ 3keep ] dip 3keep ] dip call ; inline
109
110 ! Spreaders
111 : bi* ( x y p q -- )
112     [ dip ] dip call ; inline
113
114 : tri* ( x y z p q r -- )
115     [ [ 2dip ] dip dip ] dip call ; inline
116
117 ! Double spreaders
118 : 2bi* ( w x y z p q -- )
119     [ 2dip ] dip call ; inline
120
121 ! Appliers
122 : bi@ ( x y quot -- )
123     dup bi* ; inline
124
125 : tri@ ( x y z quot -- )
126     dup dup tri* ; inline
127
128 ! Double appliers
129 : 2bi@ ( w x y z quot -- )
130     dup 2bi* ; inline
131
132 ! Object protocol
133 GENERIC: hashcode* ( depth obj -- code )
134
135 M: object hashcode* 2drop 0 ;
136
137 M: f hashcode* 2drop 31337 ;
138
139 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
140
141 GENERIC: equal? ( obj1 obj2 -- ? )
142
143 M: object equal? 2drop f ;
144
145 TUPLE: identity-tuple ;
146
147 M: identity-tuple equal? 2drop f ;
148
149 USE: math.private
150 : = ( obj1 obj2 -- ? )
151     2dup eq? [ 2drop t ] [
152         2dup both-fixnums? [ 2drop f ] [ equal? ] if
153     ] if ; inline
154
155 GENERIC: clone ( obj -- cloned )
156
157 M: object clone ;
158
159 M: callstack clone (clone) ;
160
161 ! Tuple construction
162 GENERIC: new ( class -- tuple )
163
164 GENERIC: boa ( ... class -- tuple )
165
166 ! Quotation building
167 : 2curry ( obj1 obj2 quot -- curry )
168     curry curry ; inline
169
170 : 3curry ( obj1 obj2 obj3 quot -- curry )
171     curry curry curry ; inline
172
173 : with ( param obj quot -- obj curry )
174     swapd [ swapd call ] 2curry ; inline
175
176 : prepose ( quot1 quot2 -- compose )
177     swap compose ; inline
178
179 ! Booleans
180 : not ( obj -- ? ) [ f ] [ t ] if ; inline
181
182 : and ( obj1 obj2 -- ? ) over ? ; inline
183
184 : >boolean ( obj -- ? ) [ t ] [ f ] if ; inline
185
186 : or ( obj1 obj2 -- ? ) dupd ? ; inline
187
188 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; inline
189
190 : both? ( x y quot -- ? ) bi@ and ; inline
191
192 : either? ( x y quot -- ? ) bi@ or ; inline
193
194 : most ( x y quot -- z )
195     [ 2dup ] dip call [ drop ] [ nip ] if ; inline
196
197 ! Loops
198 : loop ( pred: ( -- ? ) -- )
199     dup slip swap [ loop ] [ drop ] if ; inline recursive
200
201 : do ( pred body tail -- pred body tail )
202     over 3dip ; inline
203
204 : while ( pred: ( -- ? ) body: ( -- ) tail: ( -- ) -- )
205     [ pick 3dip [ do while ] 3curry ] keep if ; inline recursive
206
207 : until ( pred: ( -- ? ) body: ( -- ) tail: ( -- ) -- )
208     [ [ not ] compose ] 2dip while ; inline
209
210 ! Error handling -- defined early so that other files can
211 ! throw errors before continuations are loaded
212 : throw ( error -- * ) 5 getenv [ die ] or 1 (throw) ;
213
214 ERROR: assert got expect ;
215
216 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
217
218 <PRIVATE
219
220 : declare ( spec -- ) drop ;
221
222 : hi-tag ( obj -- n ) { hi-tag } declare 0 slot ; inline
223
224 : do-primitive ( number -- ) "Improper primitive call" throw ;
225
226 PRIVATE>