]> gitweb.factorcode.org Git - factor.git/blob - core/kernel/kernel.factor
Fix permission bits
[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 ) >r 3dup r> -roll 3slip ; inline
68
69 ! Cleavers
70 : bi ( x p q -- )
71     >r keep r> call ; inline
72
73 : tri ( x p q r -- )
74     >r >r keep r> keep r> call ; inline
75
76 ! Double cleavers
77 : 2bi ( x y p q -- )
78     >r 2keep r> call ; inline
79
80 : 2tri ( x y p q r -- )
81     >r >r 2keep r> 2keep r> call ; inline
82
83 ! Triple cleavers
84 : 3bi ( x y z p q -- )
85     >r 3keep r> call ; inline
86
87 : 3tri ( x y z p q r -- )
88     >r >r 3keep r> 3keep r> call ; inline
89
90 ! Spreaders
91 : bi* ( x y p q -- )
92     >r dip r> call ; inline
93
94 : tri* ( x y z p q r -- )
95     >r >r 2dip r> dip r> call ; inline
96
97 ! Double spreaders
98 : 2bi* ( w x y z p q -- )
99     >r 2dip r> call ; inline
100
101 ! Appliers
102 : bi@ ( x y quot -- )
103     dup bi* ; inline
104
105 : tri@ ( x y z quot -- )
106     dup dup tri* ; inline
107
108 ! Double appliers
109 : 2bi@ ( w x y z quot -- )
110     dup 2bi* ; inline
111
112 : loop ( pred: ( -- ? ) -- )
113     dup slip swap [ loop ] [ drop ] if ; inline recursive
114
115 : while ( pred: ( -- ? ) body: ( -- ) tail: ( -- ) -- )
116     >r >r dup slip r> r> roll
117     [ >r tuck 2slip r> while ]
118     [ 2nip call ] if ; inline recursive
119
120 ! Object protocol
121 GENERIC: hashcode* ( depth obj -- code )
122
123 M: object hashcode* 2drop 0 ;
124
125 M: f hashcode* 2drop 31337 ;
126
127 : hashcode ( obj -- code ) 3 swap hashcode* ; inline
128
129 GENERIC: equal? ( obj1 obj2 -- ? )
130
131 M: object equal? 2drop f ;
132
133 TUPLE: identity-tuple ;
134
135 M: identity-tuple equal? 2drop f ;
136
137 : = ( obj1 obj2 -- ? )
138     2dup eq? [ 2drop t ] [ equal? ] if ; inline
139
140 GENERIC: clone ( obj -- cloned )
141
142 M: object clone ;
143
144 M: callstack clone (clone) ;
145
146 ! Tuple construction
147 GENERIC: new ( class -- tuple )
148
149 GENERIC: boa ( ... class -- tuple )
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 t ? ; inline
169
170 : and ( obj1 obj2 -- ? ) over ? ; inline
171
172 : >boolean ( obj -- ? ) t f ? ; inline
173
174 : or ( obj1 obj2 -- ? ) dupd ? ; inline
175
176 : xor ( obj1 obj2 -- ? ) [ f swap ? ] when* ; 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 ERROR: assert got expect ;
190
191 : assert= ( a b -- ) 2dup = [ 2drop ] [ assert ] if ;
192
193 <PRIVATE
194
195 : hi-tag ( obj -- n ) 0 slot ; inline
196
197 : declare ( spec -- ) drop ;
198
199 : do-primitive ( number -- ) "Improper primitive call" throw ;
200
201 PRIVATE>